Why do I have to write *myPointerVar only SOMETIMES in Objective-C?

后端 未结 3 853
北恋
北恋 2020-12-10 21:04

That\'s an issue I still don\'t understand.

Sometimes I have to write:

NSString* myVariable;
myVariable = @\"Hey!\";

Then, for exam

3条回答
  •  时光说笑
    2020-12-10 21:31

    taken from the learning objective c link via the developers portal for iphone devs:

    Objective-C supports both strong and weak typing for variables containing objects. Strongly typed variables include the class name in the variable type declaration. Weakly typed variables use the type id for the object instead. Weakly typed variables are used frequently for things such as collection classes, where the exact type of the objects in a collection may be unknown. If you are used to using strongly typed languages, you might think that the use of weakly typed variables would cause problems, but they actually provide tremendous flexibility and allow for much greater dynamism in Objective-C programs.

    The following example shows strongly and weakly typed variable declarations:

    MyClass *myObject1; // Strong typing

    id myObject2; // Weak typing

    Notice the * in the first declaration. In Objective-C, object references are pointers. If this doesn’t make complete sense to you, don’t worry—you don’t have to be an expert with pointers to be able to start programming with Objective-C. You just have to remember to put the * in front of the variable names for strongly-typed object declarations. The id type implies a pointer.

提交回复
热议问题