NSPropertyListSerialization propertyListWithData produces incompatible conversion warning/error
Asked Answered
P

1

5

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:

"Incompatible integer to pointer conversion sending 'int' to parameter of type 'NSPropertyListFormat' (aka 'unsigned int *').

I've no idea what's going on. I chose the NSPropertyListXMLFormat_v1_0 from the code-hinting choices.

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?

Palm answered 26/5, 2011 at 5:9 Comment(0)
S
31

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];

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.

Sidwohl answered 26/5, 2011 at 6:20 Comment(1)
Ohh, I see. In the Property List Programming Guide, Apple describes using this Objective-C method to read the data, but the Quick Start code sample was a little more complex, and I missed this. Thanks very much.Palm

© 2022 - 2024 — McMap. All rights reserved.