因爲我是初學者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'」... –