将文本框数据写入Plist

时间:2012-12-06 09:00:28

标签: iphone ios ipad textbox plist

我有一个带提交按钮的文本框。当我按下提交时,我需要文本框中的数据应该写在plist中。我尝试了下面的代码,但是在plist中没有任何变化。我创建了一个名为sample.plist的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中创建的数组位于

之下
<plist version="1.0">
<dict>
<key>title</key> <array/>
 </dict>
</plist>

请告诉我还需要做什么......我的错是什么

5 个答案:

答案 0 :(得分:2)

尝试:
   首先检查文件是否存在

  bool b=[[NSFileManager defaultManager] fileExistsAtPath:filePath];
 if (!b) 
 {
      NSLog(@"The file does not exist");
      return;
 }

  ........

 [titleArray addObject:[NSString stringWithFormat:@"%@", textbox1.text]];

 [plistDict setObject:titleArray forKey:@"title"];

现在,如果文件不存在,请按照Apple文档https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/CreatePropListProgram/CreatePropListProgram.html以编程方式创建文件

答案 1 :(得分:1)

您可以在我身边使用此工作正常

-(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。

答案 2 :(得分:0)

尝试使用,

[plistDict setObject:titleArray forKey:@"title"];

并检查数组是否具有值或nil对象

答案 3 :(得分:0)

尝试使用此代码..

NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"sample.plist"]) 
{
    if ([manager isWritableFileAtPath:plistPath]) 
    {
        NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
        NSMutableArray *titleArray=[plistDict valueForKey:@"title"];

        [titleArray addObject:textbox1.text];

        [plistDict setValue:titleArray forKey:@"title"];

        [plistDict writeToFile:finalPath atomically:NO];
        [manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]];
    }
}

我希望这可以帮助你...

答案 4 :(得分:0)

这对我有用 我的“Words.plist”

<dict>
    <key>Root</key>
    <array>
        <string>sunday</string>
        <string>monday</string>
        <integer>44</integer>
    </array>
</dict>


NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath];
NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"];
 _myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [_myDictionary objectForKey:@"Root"];
NSLog(@"%@", items);

希望有所帮助:)