2

And also, why is it not necessary for, eg:

printf ("abc")
user2000809
  • 496
  • 2
  • 13

3 Answers3

9

NSLog takes an NSString as argument. @"abc" denotes an NSString because of the @ sign, so that is a valid argument for NSLog. printf is a normal C function that takes a C string, which is simply created using "".

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
  • 1
    `@"abc"` is short for `[NSString stringWithString:@"abc"]` ?? This would be a recursive definition. – Martin R Feb 05 '13 at 19:22
  • @MartinR: You are correct! I believe it is `stringWithCString:encoding`, but feel free to correct me. – Scott Berrevoets Feb 05 '13 at 19:32
  • 1
    `@"abc"` creates a `NSString` object at compile time, it does not call `stringWithCString` or similar at runtime. – Martin R Feb 05 '13 at 19:34
  • 1
    @JamesBoutcher: I think `@"abc"` is called a *string literal*. Boxing is `@(c-string)`. – Martin R Feb 05 '13 at 19:41
  • 2
    `@"this is my string"` isn't _short_ for anything. It creates a literal `NSString` object that the compiler bakes right in to the binary. See, for example "[Does @"some text" give an autoreleased or retain 1 object back?](http://stackoverflow.com/q/6069459)". – jscs Feb 05 '13 at 20:18
2
UPDATE:
 NSLog(@"%@",dictionary) 

Tells the compiler that i got string to fulfill the requirement of string argument.

Update: Sorry I was supposed to write the "NSLog" instead of printf. my mistake!

  • 1
    This does not work at all. Depending on your compiler settings, it will not compile, produce warnings, or give wrong results. `printf` does not accept an `NSString` as format string, and `printf` does not know the `%@` format. – Martin R Feb 06 '13 at 08:15
1

Because it requires NSString. Adding @declares value as type of NSObject (simplification).

GregJaskiewicz
  • 456
  • 3
  • 11