Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 非常基本的目标C/C问题_Objective C - Fatal编程技术网

Objective c 非常基本的目标C/C问题

Objective c 非常基本的目标C/C问题,objective-c,Objective C,这是我的密码: #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

这是我的密码:

#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;
}
我的问题是: 程序收到信号:“EXC\U坏访问”

我真的认为问题是我的NSLog,但我不知道如何解决它


有人能帮我吗?谢谢

路径不是NSString,这就是它崩溃的原因。%@在格式化字符串中,字符串需要一个对象,并要求它提供描述以获取要打印的字符串。。。因为您使用的是C样式的字符串,所以需要使用标准的C字符串格式化程序,或者使用NSString的initWithCString:encoding:class方法将const char*转换回NSString

使用常量字符*,您可以使用:

 NSLog(@"My home folder is at '%s'", path);

这会起作用。

路径不是NSString,这就是它崩溃的原因。%@在格式化字符串中,字符串需要一个对象,并要求它提供描述以获取要打印的字符串。。。因为您使用的是C样式的字符串,所以需要使用标准的C字符串格式化程序,或者使用NSString的initWithCString:encoding:class方法将const char*转换回NSString

使用常量字符*,您可以使用:

 NSLog(@"My home folder is at '%s'", path);

这会起作用。

%@用于对象。就像一根绳子。对于const char*,您需要从c的printf格式代码中获得良好的旧%s


对于指定的格式及其含义,

%@用于对象。就像一根绳子。对于const char*,您需要从c的printf格式代码中获得良好的旧%s


对于指定的格式及其含义

我认为这是正确的,不能将char*传递给采用NSString的方法。但是,有一些方法可以将char*转换为NSString。我认为这是正确的,不能将char*传递给接受NSString的方法。不过,有一些方法可以将char*转换为NSString。