2016-01-21 63 views
0

因爲我是初學者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 
} 
+0

[anArray將writeToFile:[自dataFilePath]原子:YES]; 將其替換爲以下行 [anArray writeToFile:dataFilePath atomically:YES]; –

+0

「[ViewController dataFilePath]:無法識別的選擇器」意味着您的應用程序無法找到名爲dataFilePath的函數。鑑於你有一個用這個名字定義的NSString,我想你的意思是在你的readPlist函數中寫[自我DocDirectories]。 –

+0

當我寫這條指令,錯誤出現「使用未聲明的標識符'dataFilePAth'」... –

回答

0

哪裏是你的dataFilePath功能?

您應該用DocDirectories替換功能dataFilePath

試試這個:

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

添加此功能,您viewController.m。這問題是編譯器無法找到function:dataFilePath當應用程序正在運行,因爲你只有在一個.h文件減速dataFilePath,你應該也在.m文件中執行它。

+0

它應該是一個評論 –

+0

@MidhunMP對不起,我是新的stackoverflow.Thanks提醒 –

+0

我將函數dataFilePath替換爲DocDictionaries但它沒有工作到目前爲止 –

2

你查看控制器沒有方法dataFilePath。

你需要調用函數DocDirectories。

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

希望這會幫助你。

+0

我已經嘗試過,但沒有奏效。 –

+0

@MuhammadZaheer你得到了什麼錯誤?你'DocDirectories'的功能會爲你提供文件目錄+ Data.plist的路徑。只需記錄路徑,以便您可以從發現者那裏檢查。 –

0

檢查此

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

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);