Very basic Objective-C/C Problem
Here's my code:
#import <Foundation/Foundation.h>
void PrintPathInfo() {
const char *path = [@"~" fileSystemRepresentation];
NSLog(@"My home folder is at '%@'", path);
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutor开发者_C百科eleasePool alloc] init];
PrintPathInfo();
[pool drain];
return 0;
}
And here's my problem: Program received signal: “EXC_BAD_ACCESS”.
I really think the problem is my NSLog but I don't know how to solve it.
Could someone help me please? Thanks!
path is not an NSString, which is why that crashes. %@ in a formatting string expects an object, and asks it for a description to get a string to print... because you are using a C style string, you need to use the standard C string formatters OR convert the const char * back to an NSString using the initWithCString:encoding: class method of NSString.
Staying with a const char *, you can use:
NSLog(@"My home folder is at '%s'", path);
which would work.
%@ is for objects. (Like NSString). for const char* you will want the good old %s from the c's printf format codes.
See http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
For the format specifies and their meanings
精彩评论