1

I'm working through a BNR iOS Programming text and I came across this piece of code:

 NSLog(@"%@", [items objectAtIndex:i]);

I am unsure of what "%@" is used for. I've seen other formats for referencing integers or characters, but I've never seen this.

I even checked this reference here and it had nothing.

Thanks!

  • 1
    Why not look at Apple's docs https://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265 rather than a random blog – mmmmmm May 28 '12 at 21:35
  • 1
    You want the [String Format Specifiers](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html) – jscs May 28 '12 at 21:35

3 Answers3

6

%@ is for printing objective-c objects.

To be a bit more precise. Every object is able to override

-(NSString *)description

This method is called when you use %@. It depends on the object what info of the object it will return in the NSString.

Pfitz
  • 7,336
  • 4
  • 38
  • 51
0

According to Apple:

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.

Reference:

Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
0

I am working through the same text - and coming from a Java background, the "%@" formatter seems equivalent to the "toString" method that Java would have. It is used to display custom information about an object.

There is a good example in the response to this StackOverflow question about the "toString()" equivalent.

Community
  • 1
  • 1
declanh
  • 86
  • 9