TextField and NSString

17 views
Skip to first unread message

Apolo

unread,
Nov 24, 2009, 1:46:43 PM11/24/09
to iPhone Application Development Auditors
Hello guys,

I've got a problem with something easy (i think) but i'm really stuck
on this. This is it:

I have a tabBar with two ViewControllers instantiated at the
appDelegate. I've modifies the method "initWithNibName" in both
ViewControllers because i wanted to share an NSString between both
ViewControllers so i create an instance of NSString at the appDelegate
and pass to the ViewControllers. This instance of NSString on the
AppDelegate is an attribute on the ViewControllers

One of the ViewControllers has a TextField,
What i want to do is take this TextField "value" (just the string) and
replace the value of the attribute that is linked (because is a
pointer).

This is what i'm doing but the attribute value (the pointer) changes
too....
ip_comun = port.text;
port_comun = port.text;

Thanks for your attention.

Sukima

unread,
Nov 25, 2009, 12:56:34 PM11/25/09
to iPhone Application Development Auditors
The pointer is supposed to change. NSString is not a mutable object.
Also NSString is magic. It looks like a real object but at the heart
is not a real object. So in this case your going to change the pointer
each time. If you want a new object each time you want to use the copy
method:

ip_comun = [port.text copy];
port_comun = [port.text copy];

This will create two new object with the same internal value but will
not point to the same NSString as before. This is what is recommended
for NSString. Also it is the usual default with instance variables.
This difference is also why it is recommended to use the self.
notation to keeps things in order.

@property (nonatomic, copy) NSString *myString;
self.myString = herString;

is the same as

myString = [herString copy];

Hope it helps.

Apolo

unread,
Nov 25, 2009, 1:42:46 PM11/25/09
to iPhone Application Development Auditors
Hello Sukima,
Thanks for your answer. You really help me.

Is there a way to change the NSString value without changing the
pointer?
Is there another method to make a link between objects of two
different classes without using pointers.?

Sorry if my questions sounds stupid.
Thanks, again.
Reply all
Reply to author
Forward
0 new messages