-2

According to this answer, which says

NSString *string = [NSString new]; // A variable string referring a string object;
string = @"john"; // The object, string refers to is now "string"

Theoretically NSString is a class and string is an object which is given memory by [NSString new] code, Along with this I have read somewhere that in Objective-C, We always have pointer objects.

I am trying to understand it in real life example, Suppose I go to Toyota Agency (NSString Class) to purchased Toyota Prius(String object). And now I have my school bag(john) left is the car which is in my basement parking.

  1. So string is a Pointer Object or Pointer variable, Is this my Prius ?
  2. Is it referring to object in memory, memory is garage or Agency ?

Can any one please help to make it clear How an variable, pointer and object are related to each other ?

Community
  • 1
  • 1
Ritesh.mlk
  • 215
  • 1
  • 2
  • 11
  • 2
    Possible duplicate of [Why is NSString stringWithString returning pointer to copied string?](http://stackoverflow.com/questions/11890814/why-is-nsstring-stringwithstring-returning-pointer-to-copied-string) –  Feb 15 '17 at 10:32
  • @Sneak yes it is duplicate of answer which i already mentioned in my question, but not what you are suggesting, anyways thanks. – Ritesh.mlk Feb 15 '17 at 11:04
  • 1
    Your question link is not the same as my comment duplicate. I am not sure what more you need to know, did you read the duplicate link? Everything you need to know is there, unless you want to know something more? –  Feb 15 '17 at 11:27
  • @Sneak I am sorry but the accepted answer of your duplicate link talks about returning pointer to a copied string, but i just want to know about a simple pointer, instead of arguing why not help me and explain string is a pointer or variable and how they both are related to object. – Ritesh.mlk Feb 15 '17 at 11:44
  • 1
    I am not arguing, I am trying to point you to the multiple threads made about this already. However, I took the time to find you a site that explains everything for you about NSString http://rypress.com/tutorials/objective-c/data-types/nsstring and how the memory is accessed. –  Feb 15 '17 at 11:52
  • http://stackoverflow.com/questions/9254502/nsstring-allocation-and-initializing a good thread for you too. –  Feb 15 '17 at 11:57

1 Answers1

1
  1. Object & Variable are (in this context) syntactical sugar, both can be used invariably.
    Yes, that is your Prius
  2. It is referring to the value at memory occupied by the object @"john"

School bag & john are not the same thing.
john is the value stored at the memory location pointed by string.
From what you are stating, school bag left is the car, school bag looks like a attribute of the string object. So,

 Toyota_Agency prius = [Toyota_Agency new];
 prius = @"john";
 prius.schoolBag = @"Some school bag";
ystack
  • 1,785
  • 12
  • 23
  • So can i say prius is "Pointer variable" or "Pointer Object" and lets say we did not allocate memory yet `Toyota_Agency *prius;` Is this just a variable or object as memory is not allocated which it can point to ? – Ritesh.mlk Feb 16 '17 at 03:56
  • Yes, `Toyota_Agency *prius;` currently points to nothing – ystack Feb 16 '17 at 07:07