2016-03-19 22 views
1

(çökme) kodlayan sonra nesnenin özelliklerini erişilemiyor, bu şimdiye kaydetmeden nasıl:bunu ben <code>NSUserDefaults</code> kullanarak kaydetmek istediğiniz Ben özel bir sınıf Oject sahip

Shift.m

-(void)encodeWithCoder:(NSCoder*)encoder 
{ 
    [encoder encodeObject:self.date forKey:@"date"]; 
    [encoder encodeObject:self.startTime forKey:@"startTime"]; 
    [encoder encodeObject:self.endTime forKey:@"endTime"]; 
} 
- (id)initWithCoder:(NSCoder *)decoder { 
    if(self = [super init]) { 
     self.date = [decoder decodeObjectForKey:@"date"]; 
     self.startTime = [decoder decodeObjectForKey:@"startTime"]; 
     self.endTime = [decoder decodeObjectForKey:@"endTime"]; 
    } 
    return self; 
} 

MyTableViewController.m:

-(void)saveCustomObject:(id)object forKey:(NSString*)key 
{ 
    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object]; 
    [[NSUserDefaults standardUserDefaults] setObject:encodedObject forKey:key]; 
} 
-(NSArray*)getCustomObjectForKey:(NSString*)key 
{ 
    NSData *encodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:key]; 

    NSArray *shifts=[NSArray arrayWithObjects:[NSKeyedUnarchiver unarchiveObjectWithData:encodedObject], nil]; 

    return shifts; 
} 
o ayıklarken iyi çalışıyor gibi görünüyor

ama wh Ben böyle tableView: cellForRowAtIndexPath: örneğin, nesne özelliklerden birini erişmeye çalıştığınızda tr:

Shift *currentShift=[self.shifts objectForIndex:indexPath.row]; 
NSLog(@"%@",currentShift.startTime.description); 

O çarpışma mesajla çöker:

2016-03-19 09:04:05.913 MyApp[9654:4249448] -[__NSArrayM startTime]: unrecognized selector sent to instance 0x7ff81b449c10 
2016-03-19 09:04:05.924 MyApp[9654:4249448] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM startTime]: unrecognized selector sent to instance 0x7ff81b449c10' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010e3e9e65 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x000000010de62deb objc_exception_throw + 48 
    2 CoreFoundation      0x000000010e3f248d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x000000010e33f90a ___forwarding___ + 970 
    4 CoreFoundation      0x000000010e33f4b8 _CF_forwarding_prep_0 + 120 
    5 Mehuyavut count      0x000000010d95a8e8 -[shiftsTableViewViewController tableView:cellForRowAtIndexPath:] + 600 
    6 UIKit        0x000000010e8efe43 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 766 
    7 UIKit        0x000000010e8eff7b -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74 
    8 UIKit        0x000000010e8c4a39 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2996 
    9 UIKit        0x000000010e8f901c -[UITableView _performWithCachedTraitCollection:] + 92 
    10 UIKit        0x000000010e8dfedc -[UITableView layoutSubviews] + 224 
    11 UIKit        0x000000010e84d4a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 
    12 QuartzCore       0x000000011231959a -[CALayer layoutSublayers] + 146 
    13 QuartzCore       0x000000011230de70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 
    14 QuartzCore       0x000000011230dcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    15 QuartzCore       0x0000000112302475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 
    16 QuartzCore       0x000000011232fc0a _ZN2CA11Transaction6commitEv + 486 
    17 QuartzCore       0x000000011233037c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 
    18 CoreFoundation      0x000000010e315367 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    19 CoreFoundation      0x000000010e3152d7 __CFRunLoopDoObservers + 391 
    20 CoreFoundation      0x000000010e30af2b __CFRunLoopRun + 1147 
    21 CoreFoundation      0x000000010e30a828 CFRunLoopRunSpecific + 488 
    22 GraphicsServices     0x0000000111ba6ad2 GSEventRunModal + 161 
    23 UIKit        0x000000010e796610 UIApplicationMain + 171 
    24 Mehuyavut count      0x000000010d95b5af main + 111 
    25 libdyld.dylib      0x0000000110b2592d start + 1 
    26 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

herhangi biri bilir neden?

Teşekkür ederiz!

cevap

0

Size 1 Shift nesnesini vermek için Shift *currentShift=[self.shifts objectForIndex:indexPath.row] nesnesini istediğiniz gibi görünüyor, ancak gerçekten ne yaptığını bir NSArray döndürüyor. Temel olarak NSArray'da startTime yöntemini çağırırsınız ve mevcut olmadığından programınız başarısız olur.

Bunu yapan satır NSLog(@"%@",currentShift.startTime.description) gibi görünüyor. NSLog yorumunu yaparak onaylayın. Sana bir çarpma yapmamalı.

Bu hipotez doğruysa, [currentShift isKindOfClass:[NSArray class]]'un doğru olup olmadığını kontrol edin. Eğer durum bu ise

, bu çizgide olması bir hata:

NSArray *shifts=[NSArray arrayWithObjects:[NSKeyedUnarchiver unarchiveObjectWithData:encodedObject], nil];

Ayrıca gerçekten yok senin - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section nasıl göründüğünü göstermektedir. Orada uygun sayıda giriş yapmalısınız.

0

self.shiftsNSArray kullanıyorsunuz, ancak sözlükler için kullanılabilir olan objectForKey kullanıyorsunuz. Diziler için uygun değil. Bunun yerine objectAtIndex istediğini düşünüyorum.

+0

'objectForKey:' ile 'objectAtIndex 'arasında nerede değişiklik yapmalıyım? –

+0

indexpath.row önce Tahmin edeceğim ... –

+0

3 parçacık eklediniz. Bunlardan biri 'objectForKey:' '. Bunu değiştir. –

İlgili konular