0

I got started with Objective C, but i'm not able to understand the basic assingment itself. Below is my code

@interface

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    NSString *_str;
}
-(void) print;
-(id) init;
@end

@implementation

#import "Person.h"

@implementation Person
-(id) init{
    self=[super init];
    char c[50];
    scanf("%s",c);
    NSString *xyz=[NSString stringWithUTF8String:c];
    NSLog(@"%p",xyz);
    _str=xyz;
    xyz=@"omg";
    NSLog(@"%p",_str);
    return self;
}
-(void) print{
    NSLog(@"%p",_str);
    NSLog(@"%@",_str);
}
@end

main.m

#import <Foundation/Foundation.h>
#import "Person.h"
#import <stdio.h>
int main(int argc, const char * argv[]) {
    Person *ob=[[Person alloc] init];
    [ob print];
    return 0;
}

Code is very simple:

  1. Get input from console and assign it to local variable xyz
  2. Assign xyz to instance varaible _str
  3. Change the value of local variable xyz

As both xyz and _str are basically pointers, i thought changing value of xyz will change the value of _str too. But I don't see that even-though both variables are pointing to the same location!.Could someone please explain how the assignment is actually working and why updating xyz is not affecting _str.

Output:

My_console_input
2018-05-06 22:28:54.714327+0530 learn_objc1[8109:135801] 0x10051ceb0
2018-05-06 22:28:54.714833+0530 learn_objc1[8109:135801] 0x10051ceb0
2018-05-06 22:28:54.714878+0530 learn_objc1[8109:135801] 0x10051ceb0
2018-05-06 22:28:54.715014+0530 learn_objc1[8109:135801] My_console_input
Program ended with exit code: 0
1
  • What is your goal? If it is to learn to write iOS or macOS apps, there are better starting points for learning objc. This looks like it is from a really old set of learning materials. Commented May 7, 2018 at 13:56

1 Answer 1

1

Let's use an analogy. Think of your variables as people standing in a parking lot. Now think about your string objects as cars in the parking lot.

Since these variables are pointers, imagine the people holding out their arm and pointing to a specific car. You have two people wearing name tags. One name tag says "_str" and the other says "xyz".

The following steps are analogous to your code:

  1. The "xyz" guy is told to point to the car labeled "C".
  2. The "_str" guy is told to point to the same car that the "xyz" guy is pointing to.
  3. The "xyz" guy is told to point to the car labeled "omg".

At this point, the "_str" guy is still pointing to the "C" car. Just because the "xyz" guy is now pointing to a different car doesn't mean that the "_str" guy changed what he is pointing to.

Your confusion with the code seems to be with step 2 of the analogy. The line _str = xyz; simply means that both variables (people) are now pointing to the same object (car). It doesn't mean they are now the same variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.