2014-04-21 20 views
7
NSUserDefaults *defaultDefects = [NSUserDefaults standardUserDefaults]; 
    NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); 

NSMutableArray *loadDefects = [defaultDefects objectForKey:@"defaultDefects"]; 
    NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); 
if (loadDefects == nil) { 
    loadDefects = [NSMutableArray array]; 
} 
    NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); 
//PROBLEM HERE 
[loadDefects addObject:[NSNumber numberWithDouble:self.defectPositionX ]]; 
[loadDefects addObject:[NSNumber numberWithDouble:self.defectPositionY ]]; 
NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); 


[defaultDefects setObject:loadDefects forKey:@"defaultDefects"]; 
NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); 

[defaultDefects synchronize]; 
NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); 


ViewControllerImage *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerImage"]; 
secondViewController.thicknessValue1 = self.thicknessValue1; 
secondViewController.capWidthValue1 = self.capWidthValue1; 
NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); 

[self presentViewController:secondViewController animated:YES completion:nil]; 
NSLog(@"%f %f", self.defectPositionX, self.defectPositionY); 

Kayıtlar bumutasyona yöntemi - Hata

2014-04-21 17:03:02.838 U[8958:60b] 169.728546 274.674475 
2014-04-21 17:03:02.840 U[8958:60b] 169.728546 274.674475 
2014-04-21 17:03:02.842 U[8958:60b] 169.728546 274.674475 

tam hata raporu bu

* nedeniyle yakalanmamış istisna 'NSInternalInconsistencyException', nedeni Uygulamayı Sonlandırma: '- [ __NSCFArray insertObject: atIndex:]: değiştirilemeyen nesneye gönderilen mutasyon yöntemi ' * İlk çağrı bloğunu at: (0x2e8e2f03 0x39077ce7 0x2e8e2e45 0x2e85642b 0xb93d3 0x311476c7 0x31147663 0x311 47633 0x31132d7b 0x3114fa3d 0x31146c7d 0x31141ca7 0x31116e75 0x31115541 0x2e8adfe7 0x2e8ad4af 0x2e8abc9f 0x2e8167a9 0x2e81658b 0x337836d3 0x31175891 0xb7851 0x39575ab7) libC++ abi.dylib: Ben öyle henüz Değişken Array olarak buna sahip nasıl karıştı

NSException

Çeşidi yakalanmamış istisna sonlandırma onu çağırmak anlamsız anlamını değiştiremem. Bu hataya ilk kez rastladım ve bunu kavramak için uğraşıyorum.

Uygulamayı çalıştırırken ve verileri değiştirilebilen diziye kaydettiğimde, bazen çalışıyor ve bazen çöküyor ...?

cevap

29

Bir NSMutableArray atanmasıyla ilgili sorun, yalnızca varsayılanDefects'e, belirli bir anahtar için bir NSMutableArray atanmışsa çalışacaktır.

Not: NSUserDefaults, her zaman değişmez bir nesne döndürür.

yerine bu

NSMutableArray *loadDefects = [[defaultDefects objectForKey:@"defaultDefects"]mutableCopy]; 

bu bir değişken kopyasını garanti yapın.

+2

NSUserDefaults * her zaman * değişmez nesneleri döndürür. Değişken bir nesne arşivlenmiş olsa bile. – LombaX

+0

Bu doğru, cevap ve açıklama için teşekkürler! –

+0

Not. Teşekkürler @LombaX –

İlgili konular