-2

I want to save id type object value in NSUserDefaults.

-(IBAction)sendOrderReady:(id)sender
{
NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
[d setValue:sender forKey:@"sender"];
// [d setObject:sender forKey:@"sender"];
[d synchronize];
}
Ahmad dar
  • 81
  • 1
  • 14

3 Answers3

4

from the NSUserDefaults class description

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

so if your id object isn't one of the above instances, then you will have to convert it to one of them, and this question is what exactly you're looking for.

Community
  • 1
  • 1
John Stallings
  • 581
  • 2
  • 6
0

With NSUserDefaults you can save objects from the following class types:

NSData, NSString, NSNumber, NSDate, NSArray, NSDictionary

If you want to store any other type of object then you need to archive it or wrap it in an instance of NSData

Simpalm
  • 1
  • 5
-1
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@"iPhone" forKey:@"keyToLookupString"];

// saving an NSInteger
[prefs setInteger:442 forKey:@"integerKey"];

// saving a Double
[prefs setDouble:5.15 forKey:@"doubleKey"];

// saving a Float
[prefs setFloat:123.45678 forKey:@"floatKey"];

// This is suggested to synch prefs, but it is not needed
[prefs synchronize];
codercat
  • 22,873
  • 9
  • 61
  • 85