如何保存字符串到NSUserDefaults?
当前回答
在Swift 4.0.3 Xcode 9
============set Data in UserDefaults =========
UserDefaults.standard.set(userName, forKey: "userName")
UserDefaults.standard.synchronize()
============Get Data in UserDefaults =========
let userName = UserDefaults.standard.string(forKey: "userName")
print(userName ?? "Gera")
userNameTextField.text = ""+userName!
其他回答
下面是如何对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") {
}
斯威夫特更新了:
let userID = "BOB"
宣布userDefaults:
let defaults = UserDefaults.standard
defaults.setValue(userID, forKey: "userID")
并得到它:
let userID = defaults.object(forKey: "userID")
NSString *valueToSave = @"someValue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
[[NSUserDefaults standardUserDefaults] synchronize];
然后再拿回来
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"preferenceName"];
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"];
}
}
}
image picker
- (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; }
[nsusudefaults默认:aString forKey:aKey]
推荐文章
- 如何精确地以毫秒为单位记录方法的执行时间?
- 在整个UIWindow中获取UIView的位置
- 如何解散ViewController在Swift?
- 保存字符串到NSUserDefaults?
- 如何将JSON字符串转换为字典?
- 什么是NSLocalizedString等效在Swift?
- 查看保存的NSUserDefaults的简单方法?
- 如何创建一个字符串的格式?
- 如何添加UITableViewCell之间的间距
- 电话:用于文本输入的数字键盘
- 在为设备编译时,Apple Mach-O连接器错误
- 获取最顶端的UIViewController
- 使用isKindOfClass与Swift
- SourceKitService终止
- 如何从iPhone应用程序发送邮件