indexPath
に削除されている行UITableView
を保存しようとしていNSUSerDefaults
ます。
しかし、私は次の例外を取得しています:
Attempt to insert non-property value '<NSIndexPath 0x834cc00> 2 indexes [0,1]
これを解決し、他の方法でアクセスするためにデータをユーザーのデフォルトに保存するにはどうすればよいですか?
indexPath
に削除されている行UITableView
を保存しようとしていNSUSerDefaults
ます。
しかし、私は次の例外を取得しています:
Attempt to insert non-property value '<NSIndexPath 0x834cc00> 2 indexes [0,1]
これを解決し、他の方法でアクセスするためにデータをユーザーのデフォルトに保存するにはどうすればよいですか?
これを2つのfloat値として保存し、後でNSIndexPathを再作成できます。
したがって、保存indexPath.row
してindexPath.column
個別に作成し、次のコマンドで再作成します。
[NSIndexPath indexPathForRow:inSection:]
カテゴリをに追加しますNSIndexPath
。これにより、への/からの変換を可能にする2つのメソッドが追加されます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
私はこれをテストしませんでした、デバッグが必要かもしれません...
NSUserDefaultsに保存できるのは配列、文字列、数値のみですが、NSIndexPathは保存できないため、NSIndexPathを配列に変換するメソッドを作成し、その後NSUserDefaultsに保存する必要があります。