我在使用 CFPreferencesCopyAppValue() 方法从/向返回的 NSArray 访问/设置对象时遇到问题。在这种情况下,我的应用程序崩溃了,而当我自己分配/初始化它时,一切正常。
CFArrayRef cfArray;
if ((cfArray = (CFArrayRef)CFPreferencesCopyAppValue(CFSTR("buttonsOrder"), appID))) {
NSArray *castedArray = [(NSArray *)cfArray retain];
NSLog(@"castedArray : %@", castedArray);
buttonsOrder = [castedArray mutableCopy];
NSLog(@"buttonsOrder : %@", buttonsOrder);
CFRelease(cfArray);
[castedArray release];
castedArray = nil;
}
else {
buttonsOrder = [[NSMutableArray alloc] init];
for (NSMutableDictionary *info in togglesInfo) {
[buttonsOrder addObject:[info objectForKey:@"buttonIdentifier"]];
}
}
PS:NSLog() 向我展示了 CFArray 被很好地返回并被强制转换为 NSArray 然后 NSMutableArray 也很好。
任何想法 ?
编辑 :
这是我修改数组的方式:
- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSUInteger fromIndex = [fromIndexPath row];
NSUInteger toIndex = [toIndexPath row];
if (fromIndex == toIndex)
return;
NSString *movedButtonId = [[[buttonsOrder objectAtIndex:fromIndex] retain] autorelease];
[buttonsOrder removeObjectAtIndex:fromIndex];
[buttonsOrder insertObject:movedButtonId atIndex:toIndex];
}