我想在NSUserDefaults中保存id类型对象值。
-(IBAction)sendOrderReady:(id)sender
{
NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
[d setValue:sender forKey:@"sender"];
// [d setObject:sender forKey:@"sender"];
[d synchronize];
}
答案 0 :(得分:4)
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.
因此,如果您的ID对象不是上述任何一个实例,那么您必须将其转换为其中一个,this question is what exactly you're looking for.
答案 1 :(得分:0)
使用NSUserDefaults,您可以保存以下类类型中的对象:
NSData,NSString,NSNumber,NSDate,NSArray,NSDictionary
如果要存储任何其他类型的对象,则需要将其存档或将其包装在NSData的实例中
答案 2 :(得分:-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];