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;

 }

@ "lastobject"をtextbox.textに置き換え、XYZ.plistをsample.plistに置き換えます。

于 2012-12-06T09:35:41.230 に答える
0

使ってみてください

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

配列に値またはnilオブジェクトがあるかどうかを確認します

于 2012-12-06T09:07:16.367 に答える
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 に答える