XCode 内需要基本的 Plist 帮助

Basic Plist help needed within XCode

我对创建应用程序还很陌生,还没有完全弄清楚如何在 XCode 中使用 plist 函数。

我的问题是我在视图控制器中有 3 种不同的输入法,用户将从这些输入法中 select 值,它们是步进器、选择器视图和记录当前日期的日期,我想保存到 plist,以便用户可以在另一个视图控制器中的 table 视图中查看这些条目。

我之前没有真正使用过 plist,因此我的问题可能听起来很愚蠢,但无论如何我都需要一些帮助。

到目前为止,我已经设置了输入,但它们并没有真正做任何事情,我知道这个问题很基础,但我正在努力寻找不太技术性的信息。

我可以 post 我的代码,如果这会有所帮助的话。

任何帮助将不胜感激。

@property (weak, nonatomic) IBOutlet UILabel *balesFedLabel;
@property (weak, nonatomic) IBOutlet UIStepper *balesFedStepper;
@property (weak, nonatomic) IBOutlet UIPickerView *fieldPickerView;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UITextField *sheepGroup;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *backButton;

//Actions
- (IBAction)stepperValueChange:(id)sender;
- (IBAction)saveButton:(id)sender;
- (IBAction)textFieldDoneEditing:(id)sender;


@property NSArray *dataSource;
@property NSString *tempFieldSelection;
@property(nonatomic) UIKeyboardAppearance keyboardAppearanceDark;

@end

@implementation ViewController

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

NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSString *dateFormatString = [NSDateFormatter dateFormatFromTemplate:@"dd/MM/yyyy" options:0 locale:gbLocale];

NSLog(@"dataFormatString: %@", dateFormatString);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:dateFormatString];

NSString *stringFromDate = [dateFormatter stringFromDate:[NSDate date]];
self.dateLabel.text = stringFromDate;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {[self.view  endEditing:YES];
}

- (void)setupArray {
_dataSource = [[NSArray alloc] initWithObjects:@"Cow Pasture", @"Top Lot",    @"East Lot", @"West Lot", @"Front Meadow", @"Big Meadow", nil];

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [_dataSource count];
}

- (UIView *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row    forComponent:(NSInteger)component {
return [_dataSource objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.tempFieldSelection = [_dataSource objectAtIndex:[pickerView selectedRowInComponent:0]];
}


- (IBAction)stepperValueChange:(id)sender {double baleStepperValue =     self.balesFedStepper.value;
self.balesFedLabel.text = [NSString stringWithFormat:@"%.f", baleStepperValue];
}

- (IBAction)textFieldDoneEditing:(id)sender {[sender resignFirstResponder];
}

- (IBAction)saveButton:(id)sender {
NSLog(@"Bales Fed: %@", self.balesFedLabel.text);
NSLog(@"Sheep Group: %@", self.sheepGroup.text);
NSLog(@"Current Field: %@", self.tempFieldSelection);
NSLog(@"Last Date Fed: %@", self.dateLabel.text);
}

即使我不建议使用 .plist 文件,这里也是使用它的代码:

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"YOUR_FILE_NAME" ofType:@"plist"];
NSDictionary *plistDic = [NSDictionary dictionaryWithContentsOfFile:plistPath];

使用 NSMutableDictionary 并在 属性 中将其传递到目的地 UIViewController

例如在您的源视图控制器中:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"Bales Fed"] = self.balesFedLabel.text;
dict[@"Sheep Group"] = self.sheepGroup.text;
dict[@"Current Field"] = self.tempFieldSelection;
dict[@"Last Date Fed"] = self.dateLabel.text;

只需将 dict 传递给目标视图控制器。

如果你想使用 plist 这些是 NSDictionary 中可用的两种方法 class:

- writeToFile:atomically: 将字典写入文件(plist格式) 和 class 方法 dictionaryWithContentsOfFile: 或实例方法 initWithContentsOfFile: 从磁盘检索字典。

在你的例子中,将字典写入 plist 格式的文件:

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myData.plist"];
[dict writeToFile:filePath atomically:NO];

并在目标视图控制器中使用此代码从磁盘检索 plist:

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myData.plist"];
NSDictionary *myData = [NSDictionary dictionaryWithContentsOfFile:filePath];

来自第一种方法的 Apple 文档:

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

If the dictionary’s contents are all property list objects, the file written by this method can be used to initialize a new dictionary with the class method dictionaryWithContentsOfFile: or the instance method initWithContentsOfFile:.

我也推荐你阅读指南Property List Programming Guide

由于您只是将少量用户输入的数据从一个视图控制器传递到另一个视图控制器,因此您不需要使用 PLists、CoreData 或 NSUserDefaults。这些都适用于持久性数据,但从您的描述来看,这听起来像是短暂的东西。

只需将用户数据存储在参数中,然后使用 prepareForSegue 方法将它们转发到目的地 ViewController。有关详细信息,请参阅 this SO answer