1

我有一个带有提交按钮的文本框。当我按下提交时,我需要将文本框中的数据写入 plist 中。我尝试了下面的代码,但 plist 中没有任何变化。我创建了一个带有名称的 plist 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 中创建的数组如下

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

请告诉我还需要做什么..我的错在哪里

4

5 回答 5

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"];

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

于 2012-12-06T09:11:11.223 回答
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。

于 2012-12-06T09:35:41.230 回答
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]];
    }
}

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

于 2012-12-06T09:49:20.970 回答
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);

希望能帮助到你 :)

于 2013-06-12T14:31:34.750 回答
0

尝试使用,

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

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

于 2012-12-06T09:07:16.367 回答