问题描述
总而言之,我遇到一个问题,我从属性列表中包含的NSArray中读取了我希望成为NSNumber的内容,而不是获取诸如1之类的数字,我得到了看起来像内存地址的内容(即'61879840')。财产清单中的数字显然是正确的。 任何人都知道为什么会发生这种情况以及我可以采取哪些措施来解决这个问题?
In summary, I am having a problem where I read what I expect to be an NSNumber from an NSArray contained in a property list and instead of getting a number such as '1', I get what looks to be a memory address (i.e. '61879840'). The numbers are clearly correct in the property list. Anyone know why this may be happening and what I can do to fix it?
以下是我创建属性列表并阅读它的过程返回。
Below is my process for creating the property list and reading it back.
我创建了一个带有整数数组的简单Objective-C属性列表在一个根数组中:
I have created a simple Objective-C property list with arrays of integers within one root array:
<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对象。已使用以下代码创建并序列化属性列表:
The arrays are NSArray objects and the integers are NSNumber objects. The property list has been created and serialized using the following code:
// 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,所有值和类型都是正确的,让我相信我已经正确创建了列表。
Inspecting the created plist, all values and types are correct, leading me to believe that I've properly created the list.
属性列表作为数据读入,然后转换为NSArray:
The property list is read in as Data and then converted to an 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查看它包含的是我看到错误整数的位置:
Cycling through factorData to see what it contains is where I see the erroneous integers:
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]);
}
}
我看到所有正确数量的值,但是值本身不正确,即:
I see all the correct number of values, but the values themselves are incorrect, i.e.:
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)
推荐答案
尝试记录NSNumber的intValue:
Try logging the intValue of the NSNumber instead:
NSLog(@" %d\n", [(NSNumber *)[factorList objectAtIndex:j] intValue]);
这篇关于从Objective-C中的属性列表(plist)数组未正确返回整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!