保存string到NSUserDefaults?
如何将string保存到NSUserDefaults
?
NSString *valueToSave = @"someValue"; [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"]; [[NSUserDefaults standardUserDefaults] synchronize];
以后再拿回来
NSString *savedValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"preferenceName"];
更确切地说
-(void)saveToUserDefaults:(NSString*)myString { NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; if (standardUserDefaults) { [standardUserDefaults setObject:myString forKey:@"timestamps"]; [standardUserDefaults synchronize]; } }
这里是如何做与Swift一样的;
var valueToSave = "someValue" NSUserDefaults.standardUserDefaults().setObject(valueToSave, forKey: "preferenceName")
稍后再回来;
if let savedValue = NSUserDefaults.standardUserDefaults().stringForKey("preferenceName") { // Do something with savedValue }
在Swift 3.0中
var valueToSave = "someValue" UserDefaults.standard.set(valueToSave, forKey: "preferenceName") if let savedValue = UserDefaults.standard.string(forKey: "preferenceName") { }
像这样的东西:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; // saving an NSString [prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];
然后检索:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; // getting an NSString NSString *myString = [prefs stringForKey:@"keyToLookupString"];
你应该真的看看苹果的NSUserDefaults类参考,也可能是本教程: iPhone编程教程 – 保存/检索数据使用NSUserDefaults
不要忘记这个陈述,因为否则它可能不总是工作:
[standardUserDefaults synchronize];
[[NSUserDefaults standardUserDefaults] setValue:aString forKey:aKey]
一个好的做法是使用一个常量来避免在没有存储和读取相同的密钥的情况下出现的错误
NSString* const TIME_STAMPS_KEY = @"TIME_STAMPS_KEY";
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; // saving an NSString [prefs setObject:@"TextToSave" forKey:@"keyToLookupString"]; // saving an NSInteger [prefs setInteger:42 forKey:@"integerKey"]; // saving a Double [prefs setDouble:3.1415 forKey:@"doubleKey"]; // saving a Float [prefs setFloat:1.2345678 forKey:@"floatKey"]; // This is suggested to synch prefs, but is not needed (I didn't put it in my tut) [prefs synchronize];
检索
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; // getting an NSString NSString *myString = [prefs stringForKey:@"keyToLookupString"]; // getting an NSInteger NSInteger myInt = [prefs integerForKey:@"integerKey"]; // getting an Float float myFloat = [prefs floatForKey:@"floatKey"];
的firstView
{ NSMutableArray *array; } - (void)viewDidLoad { [super viewDidLoad]; array = [[NSMutableArray alloc]init]; array = [[NSUserDefaults standardUserDefaults]objectForKey:@"userlist"]; NSLog(@"%lu",(unsigned long)array.count); if (array>0) { for (int i=0; i<array.count; i++) { NSDictionary *dict1 = @{@"Username":[[array valueForKey:@"Username"] objectAtIndex:i],@"Mobilenumber":[[array valueForKey:@"Mobilenumber"] objectAtIndex:i],@"Firstname":[[array valueForKey:@"Firstname"] objectAtIndex:i],@"Lastname":[[array valueForKey:@"Lastname"] objectAtIndex:i],@"dob":[[array valueForKey:@"dob"] objectAtIndex:i],@"image":[[array valueForKey:@"image"] objectAtIndex:i]}; NSLog(@"%@",dict1); NSArray *array1 = [[NSArray alloc]initWithObjects:dict1, nil]; [[NSUserDefaults standardUserDefaults] setObject:array1 forKey:@"UserList"]; } } }
ImagePicker
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *chosenImage = info[UIImagePickerControllerEditedImage]; self.imaGe.image = chosenImage; [picker dismissViewControllerAnimated:YES completion:NULL]; }
-
(IBAction)submitBton:(id)sender {
NSMutableArray *array2 = [[NSMutableArray alloc]initWithArray: [[NSUserDefaults standardUserDefaults]objectForKey: @"userlist"]]; UIImage *ima = _imaGe.image; NSData *imagedata = UIImageJPEGRepresentation(ima,100); NSDictionary *dict = @{@"Username":_userTxt.text,@"Lastname":_lastTxt.text,@"Firstname":_firstTxt.text,@"Mobilenumber":_mobTxt.text,@"dob":_dobTxt.text,@"image":imagedata}; [array2 addObject:dict]; [[NSUserDefaults standardUserDefaults]setObject:array2 forKey:@"userlist"]; NSLog(@"%@",array2); [self performSegueWithIdentifier:@"second" sender:self]; }
-
(IBAction)chooseImg:(id)sender {
UIImagePickerController * picker = [[UIImagePickerController
alloc] init]; picker.delegate = self; picker.allowsEditing = YES; picker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:picker animated:YES completion:NULL];}
-
第二个视图 {NSMutableArray * arr; }
- (void)viewDidLoad { [super viewDidLoad]; arr =[[NSMutableArray alloc]init]; arr = [[NSUserDefaults standardUserDefaults]objectForKey:@"userlist"]; } #pragma mark- TableView DataSource -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return arr.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId = @"tablecell"; TableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId]; cell.userLbl.text =[[arr valueForKey:@"username"] objectAtIndex:indexPath.row]; cell.ageLbl.text =[[arr valueForKey:@"dob"] objectAtIndex:indexPath.row]; cell.profileImg.image =[UIImage imageNamed:[[arr valueForKey:@"image"] objectAtIndex:indexPath.row]]; return cell; }
-(void)saveToUserDefaults:(NSString*)string_to_store keys:(NSString *)key_for_the_String { NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; if (standardUserDefaults) { [standardUserDefaults setObject:string_to_store forKey:key_for_the_String]; [standardUserDefaults synchronize]; } }
并通过以下方式呼叫:
[self saveToUserDefaults:@"string_to_store" : @"key_for_the_string"];
检索string使用:
NSString * stored_string = [[NSUserDefaults standardUserDefaults] stringforkey:key_for_the_String]
为了节省使用这个:
[[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"KTerminated"]; [[NSUserDefaults standardUserDefaults]synchronize];
回溯使用这个:
[[[NSUserDefaults standardUserDefaults]stringForKey:@"KTerminated"] isEqualToString:@"YES"];
这里对于Swift,我创build了两个函数,它们调用Preference中的键保存和返回值
可能是这个对你有帮助的
//保存对象
static func setObject(value:AnyObject ,key:String) { let pref = NSUserDefaults.standardUserDefaults() pref.setObject(value, forKey: key) pref.synchronize() } static func getObject(key:String) -> AnyObject { let pref = NSUserDefaults.standardUserDefaults() return pref.objectForKey(key)! }
更新Swift 3
func setObject(value:AnyObject ,key:String) { let pref = UserDefaults.standard pref.set(value, forKey: key) pref.synchronize() } func getObject(key:String) -> AnyObject { let pref = UserDefaults.standard return pref.object(forKey: key)! as AnyObject }