首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS文件系统中的数据归档

iOS文件系统中的数据归档
EN

Code Review用户
提问于 2014-12-11 14:04:25
回答 1查看 375关注 0票数 1

这是我第一次将数据归档到文件系统中,我想要检查我的代码。这个简单的测试项目如预期的那样工作,但我想看看我所做的工作是否遵循了最佳实践。打开应用程序,放入文本字段,按下按钮,字符串保存并显示在标签中。当app重新打开时,保存的字符串将出现在标签中。

Cars.h

代码语言:javascript
复制
@interface Car : NSObject <NSCoding>
@property (nonatomic, copy) NSString *year;
@property (nonatomic, copy) NSString *make;
@property (nonatomic, copy) NSString *model;
@end

Cars.m

代码语言:javascript
复制
NSString *const kYearKey = @"YearKey";
NSString *const kMakeKey = @"MakeKey";
NSString *const kModelKey = @"ModelKey";

@implementation Car

-(void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.year forKey:kYearKey];
[aCoder encodeObject:self.make forKey:kMakeKey];
[aCoder encodeObject:self.model forKey:kModelKey];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self != nil) {
    _year = [aDecoder decodeObjectForKey:kYearKey];
    _make = [aDecoder decodeObjectForKey:kMakeKey];
    _model = [aDecoder decodeObjectForKey:kModelKey];
}
return  self;
}
@end

ViewController.m

代码语言:javascript
复制
@interface ViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *yearTextField;
@property (weak, nonatomic) IBOutlet UITextField *makeTextField;
@property (weak, nonatomic) IBOutlet UITextField *modelTextField;
@property (weak, nonatomic) IBOutlet UILabel *yearLabel;
@property (weak, nonatomic) IBOutlet UILabel *makeLabel;
@property (weak, nonatomic) IBOutlet UILabel *modelLabel;
@property (strong, nonatomic) NSString *yearString;
@property (strong, nonatomic) NSString *makeString;
@property (strong, nonatomic) NSString *modelString;

- (IBAction)doneButton:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self loadCarInfo];
}

-(void)loadCarInfo {

NSString *path = [self archivePath];
Car *returnedCarInfo = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

NSString *year = [[NSString alloc] initWithFormat:@"%@", returnedCarInfo.year];
NSString *make = [[NSString alloc] initWithFormat:@"%@", returnedCarInfo.make];
NSString *model = [[NSString alloc] initWithFormat:@"%@", returnedCarInfo.model];

_yearLabel.text = year;
_makeLabel.text = make;
_modelLabel.text = model;
}

-(BOOL)saveCarInfo {

NSString *path = [self archivePath];

Car *myCar = [[Car alloc] init];
myCar.year = _yearString;
myCar.make = _makeString;
myCar.model = _modelString;

//archive the object to the file & return YES on success
return [NSKeyedArchiver archiveRootObject:myCar toFile:path];

}

- (IBAction)doneButton:(id)sender {

_yearString = [[NSString alloc] initWithString:_yearTextField.text];
_makeString = [[NSString alloc] initWithString:_makeTextField.text];
_modelString = [[NSString alloc] initWithString:_modelTextField.text];

BOOL success = [self saveCarInfo];
if (success) {
    [self loadCarInfo];
}
}

-(NSString *)archivePath {

NSArray *documentDirectories= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                 NSUserDomainMask, YES);
//get the document directory from that list
NSString *documentDirectory = [documentDirectories firstObject];

return [documentDirectory stringByAppendingPathComponent:@"savedText.txt"];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField {

[textField resignFirstResponder];

return YES;
}

@end
EN

回答 1

Code Review用户

发布于 2014-12-11 15:25:52

密码看起来没问题。我只想就两件事发表意见:

  • 当您只保留对Car对象的引用时,为什么在视图控制器中保留单个字段作为属性?
  • 您不需要将插座连接到所有标签,除非您计划在代码中更改它们(例如,本地化或自定义它们)
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/73330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档