【发布时间】:2010-04-07 02:25:18
【问题描述】:
总之,我遇到了一个问题,我从属性列表中包含的 NSArray 中读取了我期望的 NSNumber,而不是获取诸如“1”之类的数字,而是得到了看起来是内存地址的内容(即“61879840”)。财产清单中的数字显然是正确的。 有人知道为什么会发生这种情况以及我可以做些什么来解决它吗?
以下是我创建属性列表并读取它的过程。
创建属性列表
我创建了一个简单的 Objective-C 属性列表,其中包含一个根数组中的整数数组:
<array>
<array>
<integer>1</integer>
<integer>2</integer>
</array>
<array>
<integer>1</integer>
<integer>2</integer>
<integer>5</integer>
</array>
... more arrays with integers ...
</array>
数组是 NSArray 对象,整数是 NSNumber 对象。属性列表已使用以下代码创建和序列化:
// factorArray is an NSArray that contains NSArrays of NSNumbers as described above
// serialize and compress factorArray as a property list, Factors-bin.plist
NSString *error;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Factors-bin.plist"];
NSData *plistData = [NSPropertyListSerialization
dataFromPropertyList:factorArray
format:NSPropertyListBinaryFormat_v1_0
errorDescription:&error];
检查创建的 plist,所有的值和类型都是正确的,这让我相信我已经正确地创建了列表。
读取属性列表
属性列表作为Data读入,然后转换为NSArray:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Factors" ofType:@"plist"];
NSData *plistData = [[NSData alloc] initWithContentsOfFile:path];
NSPropertyListFormat format;
NSString *error = nil;
NSArray *factorData = (NSArray *)[NSPropertyListSerialization
propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&error];
通过 factorData 循环查看它包含的内容是我看到错误整数的地方:
for (int i = 0; i < 10; i++) {
NSArray *factorList = (NSArray *)[factorData objectAtIndex:i];
NSLog(@"Factors of %d\n", i + 1);
for (int j = 0; j < [factorList count]; j++) {
NSLog(@" %d\n", (NSNumber *)[factorList objectAtIndex:j]);
}
}
我看到所有正确数量的值,但值本身不正确,即:
Factors of 3
61879840 (should be 1)
61961200 (should be 3)
Factors of 4
61879840 (should be 1)
61943472 (should be 2)
61943632 (should be 4)
Factors of 5
61879840 (should be 1)
61943616 (should be 5)
【问题讨论】:
标签: objective-c plist integer