NSLog(@"%@",2);
Warning:format specifies type id but the argument has type int
Output
Xcode shows a warning, but at runtime, why this code causes crash? thanks!
id
is a pointer to an Objective-C object. Ex. NSString *
or NSDate *
. So when you use %@
, the compiler is expecting a pointer to the memory address where an object is stored.
An int
is a "primitive" value type. It is not an object. It is an actual value stored in memory and is passed directly to the argument in NSLog statement.
The reason it is crashing is because you are passing a value of 2 to a placeholder that is looking for a pointer to a memory address. That's what "bad access" means. The %@
can't find anything because you haven't provided an accurate memory address pointer.
The fix here is to use
NSLog(@"%d",2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With