1

I am trying to save indexPath for the row that is being deleted from UITableView into NSUSerDefaults.

But I am getting following exception:

Attempt to insert non-property value '<NSIndexPath 0x834cc00> 2 indexes [0,1]

How can I resolve this and save data into user defaults for accessing it in other methods?

4

3 回答 3

5

您可以将其保存为两个浮点值并稍后重新创建 NSIndexPath:

所以保存indexPath.rowindexPath.column单独然后重新创建它:

 [NSIndexPath indexPathForRow:inSection:]
于 2012-05-08T03:49:15.800 回答
2

将类别添加到NSIndexPath. 这增加了两种方法,允许转换到/从NSArray. 然后,您可以将其保存为默认值。

@implementation NSIndexPath ( ArrayRepresentation )

-(NSArray*)arrayRepresentation
{
    NSMutableArray * result = [ NSMutableArray arrayWithCapacity:self.length ] ;
    for( int index=0, count = self.length; index < count; ++index )
    {
        [ result addObject:[ NSNumber numberWithUnsignedInteger:[ self indexAtPosition:index ] ] ] ;
    }

    return result ;
}

+(NSIndexPath*)indexPathWithArrayRepresentation:(NSArray*)array
{
    if ( array.count == 0 ) { return nil ; }

    // if array has too many items this will crash your app be careful
    NSUInteger indexes[ array.count ] ;
    for( int index=0, count = array.count; index < count; ++index )
    {
        indexes[ index ] = [ [ array objectAtIndex:index ] unsignedIntegerValue ] ;
    }

    return [ NSIndexPath indexPathWithIndexes:indexes length:array.count ] ;
}

@end

我没有测试这个,可能需要调试......

于 2012-05-08T04:13:16.517 回答
0

您只能将数组、字符串、数字保存到 NSUserDefaults,但不能保存 NSI​​ndexPath,因此您应该创建一个将 NSIndexPath 转换为数组的方法,然后将其保存到 NSUserDefaults。

于 2012-05-08T06:35:03.637 回答