在iOS程序中阅读plist

问题描述:

因为我是初学者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'”... –

哪里是你的dataFilePath功能?

您应该用DocDirectories替换功能dataFilePath

试试这个:

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

添加此功能,您viewController.m。这问题是编译器无法找到function:dataFilePath当应用程序正在运行,因为你只有在一个.h文件减速dataFilePath,你应该也在.m文件中执行它。

+0

它应该是一个评论 –

+0

@MidhunMP对不起,我是新的*.Thanks提醒 –

+0

我将函数dataFilePath替换为DocDictionaries但它没有工作到目前为止 –

你查看控制器没有方法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的路径。只需记录路径,以便您可以从发现者那里检查。 –

检查此

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

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