因为我是 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
}
最佳答案
您的 View Controller 没有方法 dataFilePath。
您需要调用函数 DocDirectories。
- (void)writePlist
{
NSMutableArray *anArray = [[NSMutableArray alloc]init];
[anArray addObject:@"A"];
[anArray addObject:@"B"];
[anArray writeToFile:[self DocDirectories] atomically:YES];
}
希望对您有所帮助。
关于ios - 在iOS程序中读取plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34918031/