0

このコードを使用して Templates.plist ファイルからデータを取得しようとしていますが、配列に null 値を取得しています。

//from plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"Templates" ofType:@"plist"];
NSMutableArray *arry=[[NSMutableArray alloc]initWithContentsOfFile:path];

NSLog(@"arrayArray:::: %@", arry);

これは私のplistファイルです:

plistファイル

また、この plist ファイルに文字列を追加し、この plist から文字列を削除する方法を知りたいです。

4

3 に答える 3

3

まず、mainBundle には何も書き込めません。plist に書き込むには、それをドキュメント ディレクトリにコピーする必要があります。これは次のように行われます。

- (void)createEditableCopyOfIfNeeded 
{
     // First, test for existence.
     BOOL success;

     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSError *error;
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Template.plist"];
     success = [fileManager fileExistsAtPath:writablePath];

     if (success) 
         return;

     // The writable file does not exist, so copy from the bundle to the appropriate location.
     NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Template.plist"];
     success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error];
     if (!success) 
         NSAssert1(0, @"Failed to create writable file with message '%@'.", [error localizedDescription]);
}

したがって、この関数を呼び出すと、ドキュメント ディレクトリにファイルが存在するかどうかがチェックされます。そうでない場合は、ファイルをドキュメント ディレクトリにコピーします。ファイルが存在する場合は、単に返されます。次に、ファイルにアクセスして読み書きできるようにするだけです。アクセスするには、ドキュメント ディレクトリへのパスが必要で、ファイル名をパス コンポーネントとして追加するだけです。

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"Template.plist"];

plist からデータを取得するには:

NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];

ファイルをドキュメント ディレクトリに書き戻します。

    [array writeToFile:filePath atomically: YES];
于 2012-02-08T17:03:57.520 に答える
2
-(NSMutableArray *)start1

 {

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Templates.plist"];



if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]){

    plistArr = [NSMutableArray arrayWithContentsOfFile: plistPath];

}
else {
    plistArr = [[NSMutableArray alloc] init];

}
return plistArr;
}


- (void)createNewRecordWithName:(NSMutableDictionary *)dict

 {

plistArr=[self start1];
[plistArr addObject: dict];
//[dict release];
[self writeProductsToFile:plistArr];

 }

  - (void)writeProductsToFile:(NSMutableArray *)array1 {

   NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Template.plist"];

[array1 writeToFile:plistPath atomically:YES];

   }
于 2012-02-08T12:36:15.193 に答える
0

plist を変更可能な配列に取得するには、次のクラス メソッドを使用します。

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];

次に、配列から文字列を追加/削除します。配列を plist に保存するには、次を使用します。

[array writeToFile:path atomically:YES];
于 2012-02-08T15:05:51.443 に答える