2

I am trying to save NSData in NSUserDefaults in a loop, but I'm constantly getting an error.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *index = [NSString stringWithFormat:@"alltweetsoftrend%d", i];
[defaults setObject:tweetData forKey:index];

[defaults synchronize];

Where i is index of the loop, it means key is not null in any case. I have also checked my NSData(tweetData) and this also is not null. I am just getting this error in saving.

cnotethegr8
  • 7,342
  • 8
  • 68
  • 104

1 Answers1

2

There's nothing inherently wrong with your code. There's a slight chance the NSData is too large to store, but I wouldn't really expect a SIGABRT in that case. The most likely issue is that the NSData was created with [NSData dataWithBytesNoCopy:length:] and the buffer that the NSData pointed to was deallocated. There could be other issues that cause the problem as well, such as using a shared NSData global variable between threads, incorrect typecasting from CFDataRefs, etc, etc.

Try writing a different NSData and see if it works. For example: NSData *tweetData = [NSData dataWithBytes:"Hello" length:6];. I bet that works, so the problem has to do with how your tweetData was created and sent into this code.

EricS
  • 9,650
  • 2
  • 38
  • 34