Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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中NSLog的形成_Objective C_Xcode4 - Fatal编程技术网

关于Objective-C中NSLog的形成

关于Objective-C中NSLog的形成,objective-c,xcode4,Objective C,Xcode4,这可以成功地暗示。如果最后一句是: const char *string ="Hi there,this is a C string"; NSData *data=[NSData dataWithBytes:string length:strlen(string)+1]; NSLog(@"data is %@",data); NSLog(@"%lu byte string is '%s'",[data length],[data byte

这可以成功地暗示。如果最后一句是:

const char *string ="Hi there,this is a C string";
NSData *data=[NSData dataWithBytes:string 
                            length:strlen(string)+1];
NSLog(@"data is %@",data);
NSLog(@"%lu byte string is '%s'",[data length],[data bytes]);
它将警告转换指定的类型为“int”,但参数的类型为“NSUInteger”(也称为“usigned long”)


为什么
%d
不能?

整数基本上是一个无符号的长整数,所以使用%lu代替。

%d表示“int”。NSUInteger不是“int”,因此%d不起作用。您必须将格式说明符与类型匹配。如果指定了错误的类型,您的程序可能会崩溃,或者更有可能会打印垃圾。

但问题是“%d”不能。它是教科书编写的。我不知道您正在阅读的教科书,但在ObjC中,您需要在格式说明符中传递正确的“长/短”和“有符号/无符号”。%lu正确(长无符号)。%d(缩写,签名)不是。谢谢你的回答。也许这本教科书需要改进,或者翻译需要改进。谢谢!请看一下%d代表整数。%f代表双打。此处记录了所有的说明符:
NSLog(@"%d byte string is '%s'",[data length],[data bytes]);