Reputation: 1630
I have a binary file that contains a serialised plist. The file was generated in PHP by the statements:
namespace CFPropertyList;
require_once(__DIR__.'/../classes/CFPropertyList/CFPropertyList.php');
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
$plist_filename = __DIR__ . '/simple_array.plist';
$plist = new CFPropertyList();
$td = new \CFPropertyList\CFTypeDetector();
$guessedStructure = $td->toCFType($a);
$plist->add($guessedStructure);
$plist->saveBinary($plist_filename);
Binary contents of simple_array.plist file are:
6270 6c69 7374 3030 d301 0203 0405 0651 6151 6251 6355 6170 706c 6556 6261 6e61 6e61 a307 0809 5178 5179 517a 080f 1113 151b 2226 282a 0000 0000 0000 0101 0000 0000 0000 000a 0000 0000 0000 0000 0000 0000 0000 002c
I can open simple_array.plist in Xcode (by double clicking on it) without a problem.
I can also re-open it later in PHP using the following statements also without a problem:
$plist = new CFPropertyList( $plist_filename, CFPropertyList::FORMAT_BINARY );
The problem is when I try to open the file in Objective C. These are the statements I am using:
NSData *tempData = [NSData dataWithContentsOfFile:path_to_plist_file];
NSArray *fileInfoArray = [NSKeyedUnarchiver unarchiveObjectWithData:tempData];
fileInfoArray
is nil
!
These last two statements open other plist files that were generated in Objective C but not the file generated by PHP (on Linux).
Any idea what is going on here? This webpage http://nshipster.com/nscoding/ talks about the importance of NSCoding for NSKeyedArchiver... could that be relevant and if so how do I apply in PHP?
Upvotes: 1
Views: 124
Reputation: 438072
You can read the plist into a dictionary with:
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfURL:fileURL];
Or you can use NSPropertyListSerialization
:
NSPropertyListFormat format;
NSError *error;
NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:data options:0 format:&format error:&error];
And with your data, both of those worked for me.
For more information, see the Property List Programming Guide.
Upvotes: 3