4
NSMutableArray *mutableArray = [NSMutableArray array];
Prostate *prostate = [Prostate new];
prostate.prostateCheckTimeString = self.prostateCheckTime.text;
prostate.prostateCubeString =self.prostateCube.text;
prostate.prostateAntigemString =self.prostateAntigem.text;
prostate.peeSpeedMaximumString =self.peeSpeedMaximum.text;
prostate.peeRemenderString =self.peeRemender.text;
[mutableArray addObject:prostate];
NSArray *tempArray = [[NSArray alloc]initWithArray:self.mutableArray];


[Global saveArrayToUserDefault:prostateArray saveValue:tempArray];
NSArray *array = [Global getArrayFromUserDefault:prostateArray];

//Below is my global object

**+(void)saveArrayToUserDefault:(NSString *)userDefaultKey saveValue:(NSArray *)value**
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:value forKey:prostateArray];
[userDefaults synchronize];
}

**+(NSArray *)getArrayFromUserDefault:(NSString *)userDefaultKey**
{
NSArray *temp;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
temp = [userDefaults objectForKey:userDefaultKey];
return temp;
}

-----------------------------------Description ---------------------------------------

I had Been Search for a while , I know NSUserDefault can only store NSArray , I tried to convert NSMutableArray to NSArray , but still got error

-----------------------------------Error ---------------------------------------------

Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType') Attempt to set a non-property-list object ( ""

Stephen Chen
  • 3,027
  • 2
  • 27
  • 39
  • The problem isn't the mutable array. The problem is you can't store custom objects in `NSUserDefaults`. You have to encode your `Prostate` objects properly. – rmaddy Nov 21 '14 at 04:06
  • 1
    Please [do a search on the error](http://stackoverflow.com/search?q=%5Bios%5D+Attempt+to+set+a+non-property-list+object). – rmaddy Nov 21 '14 at 04:08
  • you can not save any array type of object directly to the User default. Please check this Link . http://stackoverflow.com/a/2315972/2098690 you can use object mapper for this to set Archivable object https://github.com/roomorama/RMMapper – Esha Nov 21 '14 at 05:29

2 Answers2

3

Implement the <NSCoding> protocol in your entity class which you wanna save

-(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withArray:(NSMutableArray *)myArray {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
    [defaults setObject:data forKey:keyName];
    [defaults synchronize]; }

-(NSArray *)readArrayWithCustomObjFromUserDefaults:(NSString*)keyName {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:keyName];
    NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    [defaults synchronize];
    return myArray;  }
Jignesh Agola
  • 320
  • 1
  • 11
0

Prostate is your custom object and NSUserDefaults does not allow you to save it. So you have to Archive it before saving to NSUserDefaults and Unarchive it when reading back from NSUserDefaults.

So your methods to save and read user defaults data should look like:

-(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withArray:(NSMutableArray *)myArray {
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
     [defaults setObject:data forKey:keyName];
     [defaults synchronize]; 
}

-(NSArray *)readArrayWithCustomObjFromUserDefaults:(NSString*)keyName {
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
     NSData *data = [defaults objectForKey:keyName];
     NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
     return myArray;  
}
Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
  • You've left out most of the important code needed to make this work. As is, this code will crash with exceptions about the `Prostate` class not conforming to the `NSCoding` protocol. – rmaddy Nov 21 '14 at 15:33