【问题标题】:Appending Data to .plist file programmatically以编程方式将数据附加到 .plist 文件
【发布时间】:2015-04-13 01:49:03
【问题描述】:

我正在使用一个名为 name.plist 的 pList。我正在尝试在 plist 中添加新名称值,但没有一种方法对我有用。 我正在尝试什么:

#import "ViewController.h"
@interface ViewController ()

{
NSMutableArray *plist_names;
}
@end

@implementation ViewController

- (void)viewDidLoad 
{
[super viewDidLoad];


NSArray *folder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString *dir = [folder objectAtIndex:0];

NSString *path = [[NSString alloc]initWithString:[dir stringByAppendingPathComponent:@"name.plist"]];

NSFileManager *fileManager = [NSFileManager defaultManager];


if([fileManager fileExistsAtPath:path]==NO)
{
   plist_names = [[NSMutableArray alloc]init];
}
else
{
     plist_names = [[NSMutableArray alloc]initWithContentsOfFile:path];
}


//NSLog(@"%@",[plist_names objectAtIndex:0]);  //App Crashes

NSLog(@"%@",plist_names);  //Blank Console





}
@end

我无法弄清楚我错过了什么。我正在开发 Xcode6 并为 ios8 开发。

查看的问题: Adding dictionaries to Plist programmatically Appending data to PLIST - iPhone 还有更多

【问题讨论】:

  • 我认为您不能以编程方式在 .plist 文件中添加任何值/数据。你只能手动完成。但是您可以从 .plist 文件中获取数据并将它们存储到 NSDic/Array 中,然后您可以对其进行修改。
  • @Romance 我在网上搜索了很多方法来添加。但是当我开始这样做时,我不适合我
  • 不,您不能以编程方式更改捆绑包中的任何内容。
  • 你不知道 NSUserDefaults 是一个 plist 吗?
  • 我没听懂你在说什么? @BogdanMatveev

标签: ios objective-c iphone plist


【解决方案1】:

我终于做到了::

首先打开 AppDelegate.m 并在 DidFinishLoading 部分写下这段代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
// Override point for customization after application launch.

NSArray *folder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString *dir = [folder objectAtIndex:0];

NSString *path =[dir stringByAppendingPathComponent:@"name.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if([fileManager fileExistsAtPath:path]==NO)
{
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"plist"];
    [fileManager copyItemAtPath:bundlePath toPath:path error:nil];
}

return YES;
}

现在 ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];

NSArray *folder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString *dir = [folder objectAtIndex:0];

NSString *path = [dir stringByAppendingPathComponent:@"name.plist"];

NSMutableDictionary *plist_names = [[NSMutableDictionary alloc]initWithContentsOfFile:path];

NSMutableArray *names = [plist_names objectForKey:@"Names"];

NSLog(@"%@",[names objectAtIndex:1]);

[names addObject:@"jkl"];

NSLog(@"%@",[names objectAtIndex:2]);

 [ plist_names setObject:names forKey:@"Names"];


[plist_names writeToFile:path atomically:YES];
}

【讨论】:

    【解决方案2】:

    您可以将文件写入应用程序包,但实际上您不应该这样做。您将破坏代码签名,并且您的应用将不再启动。您应该使用已建立的约定,例如写入应用程序的 Library 或 Documents 文件夹。看看这个 SO 的方法:Get file path by file name from documents directory ios

    【讨论】:

    • 我该怎么办?我是否必须将 plist 文件的副本复制到文档目录
    • 我怎样才能做到这一点
    • 它与您在上面发布的代码相似,但路径不同。试一试,看看它是否有效,如果没有,请发布代码说明原因
    • 我用新代码更新了我的问题,但我仍然没有得到输出。 @GaryRiches
    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多