这是我第一次将数据归档到文件系统中,我想要检查我的代码。这个简单的测试项目如预期的那样工作,但我想看看我所做的工作是否遵循了最佳实践。打开应用程序,放入文本字段,按下按钮,字符串保存并显示在标签中。当app重新打开时,保存的字符串将出现在标签中。
Cars.h
@interface Car : NSObject <NSCoding>
@property (nonatomic, copy) NSString *year;
@property (nonatomic, copy) NSString *make;
@property (nonatomic, copy) NSString *model;
@endCars.m
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;
}
@endViewController.m
@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发布于 2014-12-11 15:25:52
密码看起来没问题。我只想就两件事发表意见:
Car对象的引用时,为什么在视图控制器中保留单个字段作为属性?https://codereview.stackexchange.com/questions/73330
复制相似问题