Skip to content

Instantly share code, notes, and snippets.

@brockboland
Last active December 20, 2015 01:29
Show Gist options
  • Save brockboland/6049308 to your computer and use it in GitHub Desktop.
Save brockboland/6049308 to your computer and use it in GitHub Desktop.
-(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".
@brockboland
Copy link
Author

C and Objective-C coders: it's been too many years since I've really dealt with pointers.

Let's say Object A has a string, and it passes that string to Object B when initializing B. If the string on B changes, I want that change reflected in A.

Right now, B just changes its pointer to the new string (right?), but I want it to actually change the value at that pointer address.

Little help?

@rodchile
Copy link

Looking to it.

@rodchile
Copy link

Okay this is the thing.
Let's say we have this code:

self.myAString = @"boo";
self.myBString = self.myAString;

NSLog(@"A Pointer Value:%@", self.myAString);
NSLog(@"B Pointer Value:%@", self.myBString);
NSLog(@"A Pointer Address:%p", self.myAString);
NSLog(@"A Pointer Address:%p", self.myBString);

self.myAString = @"far";
NSLog(@"A Pointer Value:%@", self.myAString);
NSLog(@"B Pointer Value:%@", self.myBString);
NSLog(@"A Pointer Address:%p", self.myAString);
NSLog(@"A Pointer Address:%p", self.myBString);

self.myMutableAString = [NSMutableString stringWithString:@"foo-mutable"];
self.myMutableBString = self.myMutableAString;

NSLog(@"A Mutable Pointer Value:%@", self.myMutableAString);
NSLog(@"B Mutable Pointer Value:%@", self.myMutableBString);
NSLog(@"A Mutable Pointer Address:%p", self.myMutableAString);
NSLog(@"A Mutable Pointer Address:%p", self.myMutableAString);

[self.myMutableAString setString:@"ops-I've Mutated. Sexy!"];
NSLog(@"A Mutable Pointer Value:%@", self.myMutableAString);
NSLog(@"B Mutable Pointer Value:%@", self.myMutableBString);
NSLog(@"A Mutable Pointer Address:%p", self.myMutableAString);
NSLog(@"A Mutable Pointer Address:%p", self.myMutableBString);

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"];

@rodchile
Copy link

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.

@rodchile
Copy link

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