【问题标题】:Integers not properly returned from a property list (plist) array in Objective-C整数未从 Objective-C 中的属性列表 (plist) 数组中正确返回
【发布时间】: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


    【解决方案1】:

    尝试记录 NSNumber 的 intValue:

    NSLog(@"  %d\n", [(NSNumber *)[factorList objectAtIndex:j] intValue]);
    

    【讨论】:

    • 哇。这完全有效 - 非常感谢!我猜想阅读一些关于 NSNumber 的文档,我认为它充当整数。菜鸟失误!
    • NSNumber 是原始类型的对象包装器,因此 NSNumber 本身只是一个对象引用。您必须在其中获取原始值。
    • @Gaurav:您也可以使用NSLog(@"%@", [factorList objectAtIndex:j]) 打印 NSNumber 对象本身,这将达到相同的目标。
    • 非常感谢您!我在做 int _myInt = (int)myArray[@"myKey"];一旦我将其更改为 int _myInt = [myArray[@"myKey] intValue]; 效果很好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 2011-02-17
    相关资源
    最近更新 更多