Search code examples
objective-cpointersobjectnsstringnslog

Objective-C: NSLog passing value (pass-by-reference versus pass-by-value)


I have a question about the use of the NSLog function in Objective-C while developing for iOS. I am used to C++ syntax so this is a bit new.

In the first case we see that the value of the pointer is passed in the function: NSLog with value of pointer

In the second case we see that the pointer itself is passed in the function: NSLog with pointer adres

Does this mean that the %@ parameter takes a pointer to the object, and all other (%i, %c, %d, %f, u) take a value. I guess it has something to do with primitive datatypes versus an object?


Solution

  • Does this mean that the %@ parameter takes a pointer to the object

    Yes, it takes a pointer to an Objective-C object, which can be of type NSString or any other NSObject derived type; in the latter case, the description method is used to convert the object to a string. You can think of it as the equivalent to %s for Objective-C strings.

    This is how the %@ placeholder is described in Apple does:

    Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.

    Have a look at this Apple document for more details and also "String Format Specifiers".