因为我是初学者iOS
,我只想在我的程序中读取一个简单的属性列表文件(plist
),但它向我显示消息“由于未捕获的异常NSInvalidArgumentException',原因: ' - [ViewController dataFilePath]:无法识别的选择器发送到实例0x15638660'“。请帮助我解决我在计划中遇到的问题。在iOS程序中阅读plist
(.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
}
[anArray将writeToFile:[自dataFilePath]原子:YES]; 将其替换为以下行 [anArray writeToFile:dataFilePath atomically:YES]; –
“[ViewController dataFilePath]:无法识别的选择器”意味着您的应用程序无法找到名为dataFilePath的函数。鉴于你有一个用这个名字定义的NSString,我想你的意思是在你的readPlist函数中写[自我DocDirectories]。 –
当我写这条指令,错误出现“使用未声明的标识符'dataFilePAth'”... –