Pointers on Objective-c

孤人 提交于 2019-12-01 07:47:50

All the usage of objects in objective c is done through object references, e.g. pointers.

  • The Objective C syntax lets you treat objects without dereferencing them, this is different than C / C++ for that matter.
  • str is an object reference of type NSString (as myFraction) and @"Programming can be a headache" is an object reference of type NSString as well, so you can assign it to str.

Additional to Binyamin

  • Everything inside the brackets [ ] is objective-C and not simple C (so [*object message] is not common to use)
  • [object message], you send the "message" to your object, and the object responds with something (he can return something or he can do something without anything in return).
  • Everything with a * on the left is pointer. So *str is a pointer. And where is it point? to an object NSString. The @"blabla" returns the adress of one CONSTANT string that has generated directly by the compiler.
  • NSLog (@"%@\n", str); here the %@ calls the +Description class method of NSString object called str. By default the description of an NSString Object returns the value of the string (the text). %@ is not a simple replace like the %d with numbers (int,double etc). All objects have +Description method that inherit from the NSObject (note is Class method and not instant).

description

Returns a string that represents the contents of the receiving class.

  • (NSString *)description
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!