tags:

views:

93

answers:

2

Hi!

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 = [[NSAutoreleasePool 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!

+7  A: 

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.

Kendall Helmstetter Gelner
I think this is right, you can't pass a char* to a method that takes an NSString. There are ways to convert a char* to an NSString though.
Andy White
I just added that to my response...
Kendall Helmstetter Gelner
+5  A: 

%@ 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

Eld