NSPropertyListSerialization propertyListWithData产生不兼容的转换警告/错误 [英] NSPropertyListSerialization propertyListWithData produces incompatible conversion warning/error
问题描述
我正在尝试使用此代码读取plist中的数据:
I'm trying to read in data from a plist, using this code:
NSString *error;
NSData * tempData = [[NSData alloc] initWithContentsOfFile:@"Data.plist"];
NSDictionary *temp = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NSPropertyListXMLFormat_v1_0 error:&error];
它会发出警告/错误说明:
It kicks out a warning/error stating:
与'NSPropertyListFormat'类型的参数发送'int'不兼容的整数到指针的转换'(又名'unsigned int *')。
"Incompatible integer to pointer conversion sending 'int' to parameter of type 'NSPropertyListFormat' (aka 'unsigned int *').
我不知道发生了什么。我从代码提示选择中选择了NSPropertyListXMLFormat_v1_0。
I've no idea what's going on. I chose the NSPropertyListXMLFormat_v1_0 from the code-hinting choices.
另外,我不能到目前为止在文档中找到了这个的基本原理:为什么你必须为错误声明一个指针变量,然后使用& error作为错误的参数:。&符号是什么?
Also, I can't find the rationale for this in the documentation so far: why do you have to declare a pointer variable for "error", and then use "&error" as the argument for error:. What is the ampersand for?
推荐答案
当您尝试使用 读取数据时,NSPropertyListSerialization
,您没有指定格式:您要么传入NULL,要么传递变量的内存地址。
When you're trying to read data using NSPropertyListSerialization
, you don't specify a format: You either pass in NULL, or pass the memory address of a variable.
NSError *error;
NSData * tempData = [[NSData alloc] initWithContentsOfFile:@"Data.plist"];
NSPropertyListFormat plistFormat;
NSDictionary *temp = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:&plistFormat error:&error];
&符号表示存储此变量的内存中的地址 - 通过使用它,你'重新赋予该方法写入该内存位置的能力,并替换该变量的原始内容。调用此方法后, plistFormat
和(可能)错误
都将包含新内容: plistFormat
会告诉你 plist所处的格式,而不是相反,而错误
(应该类 NSError
)会告诉你遇到的任何错误。
The ampersand means "the address in memory where this variable is stored" – by using it, you're giving the method the ability to write to that memory location, and replace the original content of the variable. Both plistFormat
and (potentially) error
will contain something new after this method is called: plistFormat
will tell you what format the plist was in, as opposed to the other way around, and error
(which should be of class NSError
) will tell you about any errors that were encountered.
这篇关于NSPropertyListSerialization propertyListWithData产生不兼容的转换警告/错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!