在iOS程序中阅读plist

时间:2016-01-21 07:46:26

标签: ios objective-c uiviewcontroller plist

由于我是初学者iOS,我只是想在我的程序中读取一个简单的属性列表文件(plist),但它向我显示消息“由于未捕获的异常而终止应用程序'NSInvalidArgumentException' ,原因:' - [ViewController dataFilePath]:无法识别的选择器发送到实例0x15638660'“。请帮助我解决我在计划中遇到的问题。

(.h file)
@interface ViewController : UIViewController {
NSString *listPath;
NSMutableArray *array;
}

- (NSString *) dataFilePath;
- (void) writePlist;
- (void) readPlist;

(.m file)
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
UIButton *readBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];        readBtn.frame = CGRectMake(110,110,72,39);
[readBtn setTitle:@"Read" forState:UIControlStateNormal];
[readBtn addTarget:self            action:@selector(readPlist)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:readBtn];

UIButton *writeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
writeBtn.frame = CGRectMake(110,210,72,39);
[writeBtn setTitle:@"Write" forState:UIControlStateNormal];
[writeBtn addTarget:self  action:@selector(writePlist)  forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:writeBtn];

    [super viewDidLoad];

}

- (NSString *)DocDirectories {
NSArray *path =                    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *DocumentDirectory=[path objectAtIndex:0];
return  [DocumentDirectory stringByAppendingString:@"Data.plist"];

}

- (void)writePlist
{
NSMutableArray *anArray = [[NSMutableArray alloc]init];     
[anArray addObject:@"A"];
[anArray addObject:@"B"];
[anArray writeToFile:[self dataFilePath] atomically:YES];
}

- (void)readPlist
{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
    NSLog(@"%@\n", array);
    NSLog(@"%@\n",filePath);
   // [array release];    
}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
}

4 个答案:

答案 0 :(得分:2)

您查看控制器没有方法dataFilePath。

你需要调用函数DocDirectories。

- (void)writePlist
{
    NSMutableArray *anArray = [[NSMutableArray alloc]init];     
    [anArray addObject:@"A"];
    [anArray addObject:@"B"];
    [anArray writeToFile:[self DocDirectories] atomically:YES];
}

希望这会对你有所帮助。

答案 1 :(得分:0)

您的dataFilePath函数在哪里?

您应该将功能dataFilePath替换为DocDirectories

试试这个:

- (NSString *)dataFilePath {
    return [self DocDirectories];}

将此功能添加到viewController.m。此问题是编译器在应用运行时无法找到function:dataFilePath,因为您只在dataFilePath中减速.h你应该在.m文件中实现它。

答案 2 :(得分:0)

检查

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@“Data” ofType:@"plist"]; NSArray *arrData = [NSArray arrayWithContentsOfFile:plistPath];

答案 3 :(得分:0)

Use this  ,you will get

NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]];
    NSLog(@"dictionary = %@", dictionary);
   NSArray *array = [dictionary objectForKey:@"CFBundleSupportedPlatforms"];
   NSLog(@"array = %@", array);