Reputation: 546
I am trying to init properties firstName
and lastName
in XYZPerson
from my int main()
in different ways. (I'm learning OC and exploring different ways of initializing values)
However, NSLog
always returns null
for firstName
. I know there are many questions similar to mine, but almost all of them are leaning towards a specific issue but not language grammar itself.
#import <Foundation/Foundation.h>
#import "XYZShoutingPerson.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
XYZPerson *somePerson = [[XYZPerson alloc] init];
NSString *firstName = [somePerson firstName];
[somePerson setFirstName:@"Jonny"];
[somePerson setLastName:@"Appleseed"];
NSLog(@"%@ %@", firstName, [somePerson lastName]); // prints ```(null) Appleseed```
}
return 0;
}
@interface XYZPerson : NSObject
@property NSString *firstName;
@property NSString *lastName;
- (id)init;
- (id)initWithFirstName:(NSString *)aFirstName lastName:(NSString *)aLastName;
- (void)saySomething:(NSString *)greeting;
@end
I am just starting to learn Objective-C. I know this is most likely to be a very silly mistake, so pls don't be too harsh on me... Thanks!
Upvotes: 0
Views: 293
Reputation: 53010
Yeah, that worked! But I wonder why storing it as local property before setting it doesn't work? Doesn't the address
firstName
points to remains the same?
It does, which is why your code doesn't work...
The following rounds a few edges but hopefully gets the concepts across.
Let's take a step back from properties and just look at variables, the properties you have are just variables with a little code around them which has no impact here on what's going on.
A variable is just a named box, and that box is capable of storing a (computer) representation, aka value, of something with a particular type. E.g the declaration:
int height;
Is an instruction to use an available[1] box that is capable of holding a representation of something with type int
and to name that box height
.
When you assign to a variable what happens is whatever is already in the box is thrown away[2] and then a copy of the value being assigned is placed in the box.
When you read from a variable a copy of what is in the box is made and returned to you.
Now it doesn't matter what the type of value is stored in the box, the above description of the process remains true.
In your case what is stored in your boxes are values of type NSString *
, that is a reference or pointer to something which represents (in whatever way the computer does so) a string, essentially that reference is the representation of the string.
Now consider the assignment:
height = 42;
This always assigns the representation of 42
into the variable/box named height
– an integer literal always represents the same value [3].
The same is true for a string literal, so the literal @"Jonny"
always represents the string "Jonny".
Now let's look at your code and describe what it is doing in terms of (the underlying) variables (the properties are just wrappers for):
NSString *firstName = [somePerson firstName];
Copy the current value in somePerson
's firstName
variable into the local variable firstName
. From what you report at this point in your code somePerson
's firstName
contains the representation of null
so that is what is copied.
[somePerson setFirstName:@"Jonny"];
Copy the representation of @"Jonny"
into somePerson
's firstName
variable throwing away whatever is already there – in your case a representation of null
[somePerson setLastName:@"Appleseed"];
Copy the representation of @"Appleseed"
into somePerson
's lastName
variable
NSLog(@"%@ %@", firstName, [somePerson lastName]);
Copy the representations in the local variable firstName
– a representation of null
– and in somePerson
's lastName
– a representation of @"Appleseed"
– and pass them to NSLog
to convert into printable text and output.
Your misunderstanding might stem from hearing about the type NSMutableString
. One of these is represented by a box whose contents can be modified (aka "mutated") rather than simply replaced. The representation of an NSMutableString
is a reference/pointer to its box, just as an NSString
, but in its case what is in the box can be modified while the reference/pointer will stay the same as the name of the box itself doesn't change.
Hope that helped more than it confused! (It is easier to explain using pictures/a whiteboard.)
[1] How the computer finds an available box is interesting but not important here.
[2] "thrown away" can be quite involved and you'll discover mare as you learn, there is something called "garbage collection" which you'll find out about under the term "ARC" in Objective-C. But again that is not important to your question...
[3] Those old/learned enough to know about the early days of FORTRAN who wish to pipe up and say "Ah, but..." at this point go stand in the corner ;-)
Upvotes: 1