Last active
December 20, 2015 01:29
-
-
Save brockboland/6049308 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void) initStringValue: (NSString *) newValue { | |
myString = newValue; | |
} | |
-(void) changeStringValue: (NSString *) newValue { | |
// Something needs to change here | |
myString = newValue; | |
} | |
MyClass * objA = [[MyClass alloc] init]; | |
objA.name = @"This is my name"; | |
[anotherObj initStringValue: obj.name]; | |
[anotherObj changeStringValue: @"This is someone else's name"]; | |
// At this point, I would like objA.name to be set to "This is someone else's name". |
Also check out the NSString API:
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
The stringWithString: method applies a copy to the string you're giving as reference:
- (id)stringWithString:(NSString *)aString
Parameters
aString
The string from which to copy characters. This value must not be nil.
Brock I made a small test for something similar, and seems that it should work also for this.
NSString *stringA = @"hola";
NSString __strong **pointToStringA = &stringA; //The strong is because ARC can't determine who handle the memory for this pointer
NSLog(@"%@",stringA);
NSLog(@"%@",*pointToStringA);
stringA = @"boo";
NSLog(@"%@",stringA);
NSLog(@"%@",*pointToStringA);
2013-08-18 17:53:07.036 childViewController[2510:c07] hola
2013-08-18 17:53:07.861 childViewController[2510:c07] hola
2013-08-18 17:53:10.097 childViewController[2510:c07] boo
2013-08-18 17:53:10.694 childViewController[2510:c07] boo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Okay this is the thing.
Let's say we have this code:
and It gets this result:
2013-07-21 14:20:14.905 BrockTest[11678:c07] A Pointer Value:boo
2013-07-21 14:20:15.119 BrockTest[11678:c07] B Pointer Value:boo
2013-07-21 14:20:15.341 BrockTest[11678:c07] A Pointer Address:0x4794
2013-07-21 14:20:15.561 BrockTest[11678:c07] A Pointer Value:0x4794
2013-07-21 14:20:16.061 BrockTest[11678:c07] A Pointer Value:far
2013-07-21 14:20:16.209 BrockTest[11678:c07] B Pointer Value:boo
2013-07-21 14:20:16.399 BrockTest[11678:c07] A Pointer Address:0x47e4
2013-07-21 14:20:16.677 BrockTest[11678:c07] A Pointer Value:0x4794
2013-07-21 14:20:17.391 BrockTest[11678:c07] A Mutable Pointer Value:foo-mutable
2013-07-21 14:20:17.590 BrockTest[11678:c07] B Mutable Pointer Value:foo-mutable
2013-07-21 14:20:18.033 BrockTest[11678:c07] A Mutable Pointer Address:0x753cc30
2013-07-21 14:20:18.503 BrockTest[11678:c07] A Mutable Pointer Value:0x753cc30
2013-07-21 14:20:19.517 BrockTest[11678:c07] A Mutable Pointer Value:ops-I've Mutated. Sexy!
2013-07-21 14:20:20.167 BrockTest[11678:c07] B Mutable Pointer Value:ops-I've Mutated. Sexy!
2013-07-21 14:20:20.743 BrockTest[11678:c07] A Mutable Pointer Address:0x753cc30
2013-07-21 14:20:26.883 BrockTest[11678:c07] A Mutable Pointer Value:0x753cc30
In Cocoa we have inmutable and mutable classes. The first ones doesn't let you to change it's value, pushing you to create a new object if you want to update its value. The seconds, let you to update their value.
The NSString class, is an inmutable object. That means this:
NSString *myFooString = @"foo-string";
The compiler does this internally:
NSString *myFooString = [NSString stringWithString:@"foo-string"];