Integers not properly returned from a property lis

2019-03-03 21:22发布

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.

Creating the property list

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>

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

Inspecting the created plist, all values and types are correct, leading me to believe that I've properly created the list.

Reading the property list

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

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)

1条回答
Rolldiameter
2楼-- · 2019-03-03 22:11

Try logging the intValue of the NSNumber instead:

NSLog(@"  %d\n", [(NSNumber *)[factorList objectAtIndex:j] intValue]);
查看更多
登录 后发表回答