gpt4 book ai didi

iphone - 将文本框数据写入 Plist

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:48:10 26 4
gpt4 key购买 nike

我有两个文本框,我想在我按下提交时将这个文本框的数据存储到 Plist 中。到目前为止我已经完成了代码编写,但问题是如何写入文本框的数据??就像我有 textbox1 和 textbox2 一样。想将数据存储到 plist 中

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Comments" ofType:@"plist"];
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath];


NSMutableDictionary *newComment = [NSMutableDictionary dictionary];
[newComment setValue:commentTitle.text forKey:@"title"];
[newComment setValue:comment forKey:@"comment"];


[plistArray addObject:newComment];
[plistArray writeToFile:filePath atomically:NO];

请告诉我正确的方法

最佳答案

让你的文本框成为textbox1和textbox2

- (IBAction) saveData
{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Comments.plist"];

// set the variables to the values in the text fields
self.title = textbox1.text;
self.comment = textbox2.text;



// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: title, comment, nil] forKeys:[NSArray arrayWithObjects: @"title ", @"comment", nil]];

NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(@"Error in saveData: %@", error);
[error release];
}
}

关于iphone - 将文本框数据写入 Plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13738054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com