从 plist 获取数据到 NSMutableArray 并将 NSMutableArray 写入同一 plist iPhone

发布于 2025-01-03 10:20:39 字数 435 浏览 0 评论 0原文

使用此代码,我尝试从 Templates.plist 文件获取数据,但我在数组中获取空​​值。

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

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

这是我的 plist 文件:

Plist file

另外我想知道如何向此 plist 文件添加更多字符串并删除此 plist 中的字符串。

Using this code I am trying to get the data from Templates.plist file but I am getting null value in array.

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

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

This is my plist file:

Plist file

Also I want to know how can I add more strings to this plist file and delete a string from this plist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩起发的微风 2025-01-10 10:20:39

首先,你不能写入 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];

First off you cannot write to anything in you mainBundle. For you to write to you plist you need to copy it to the documents directory. This is done like so:

- (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]);
}

So calling this function will check if the file exists in the documents directory. If it doesn't it copies the file to the documents directory. If the file exists it just returns. Next you just need to access the file to be able to read and write to it. To access you just need the path to the documents directory and to add your file name as a path component.

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

To get the data from the plist:

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

To write the file back to the documents directory.

    [array writeToFile:filePath atomically: YES];
谁许谁一生繁华 2025-01-10 10:20:39
-(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];

   }
-(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];

   }
晨曦÷微暖 2025-01-10 10:20:39

要将 plist 放入可变数组中,请使用此类方法:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];

然后,从数组中添加/删除字符串。要将数组保存回 plist,请使用:

[array writeToFile:path atomically:YES];

To get a plist into a mutable array, use this class method:

NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:path];

Then, add/delete strings from the array. To save the array back to a plist, use:

[array writeToFile:path atomically:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文