将文本框数据写入Plist [英] Writing Textbox data to Plist
本文介绍了将文本框数据写入Plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带提交按钮的文本框。当我按下提交时我需要文本框中的数据应该写在plist中。我尝试了下面的代码,但是在plist中没有任何变化。我创建了一个名为 sample.plist
的plist。
I have a textbox with submit button . I need when i press to submit and data in the textbox should be written in the plist . I tried the below code , but nothing is been changing in the plist .I have created a plist with name sample.plist
.
-(void) SubmitAction {
NSString *path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *finalPath = [path stringByAppendingPathComponent:@"sample.plist"];
NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
NSMutableArray *titleArray=[plistDict valueForKey:@"title"];
[titleArray addObject:textbox1.text];
[plistDict setValue:titleArray forKey:@"title"];
[plistDict writeToFile:finalPath atomically:NO];
}
在plist中创建的数组低于
The array created in the plist is below
<plist version="1.0">
<dict>
<key>title</key> <array/>
</dict>
</plist>
请告诉我还需要做什么..我的错是什么
please tell what else i need to do ..where is my fault
推荐答案
你可以在我身边使用这个工作正常
You can use this working fine on my side
-(void)writeToPlist
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"XYZ.plist"];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"XYZ.plist"];
if([fileManager fileExistsAtPath:documentPlistPath]){
NSMutableDictionary *documentDict = [NSMutableDictionary
dictionaryWithContentsOfFile:documentPlistPath];
NSMutableArray *valArray = [NSMutableArray arrayWithArray:[self readFromPlist]];
int index = [valArray count];
[valArray insertObject:@"lastObject" atIndex:index];
[documentDict setObject:valArray forKey:@"title"];
success =[documentDict writeToFile:documentPlistPath atomically:NO];
} else {
NSError *error;
BOOL written = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath
error:&error];
if (written) {
NSMutableDictionary *documentDict = [NSMutableDictionary
dictionaryWithContentsOfFile:documentPlistPath];
NSMutableArray *valArray = [NSMutableArray arrayWithArray:[self readFromPlist]];
int index = [valArray count];
[valArray insertObject:@"lastObject" atIndex:index];
[documentDict setObject:valArray forKey:@"title"];
success =[documentDict writeToFile:documentPlistPath atomically:NO];
}else {
NSLog(@"Plist couldnot be copied from bundle to directory!!!");
}
}
}
-(NSArray*)readFromPlist
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"XYZ.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];
NSArray *valueArray = [dict objectForKey:@"title"];
return valueArray;
}
用textbox.text替换@lastobject;以及用您的sample.plist替换XYZ.plist。
replace @"lastobject" with your textbox.text;and replace XYZ.plist with your sample.plist.
这篇关于将文本框数据写入Plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文