ios - 在iOS程序中读取plist

标签 ios objective-c uiviewcontroller plist

因为我是 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/

相关文章:

iOS 使用彩色动画按钮点击不起作用

ios - 在整个应用程序中显示 View Controller

ios - 如何从另一个容器访问容器 View

ios - UIViewController 的 subview 最初在什么时候明确布局?

ios - 使用按钮自定义 CAShapeLayer 动画

ios - 如何将从 Spotify 流式传输的音乐可播放数据推送到不使用 Spotify 提供的 SDK 的设备

ios - 是否有理由选择发件人 : Any when creating an @IBAction in Swift?

ios - Xcode 12 模拟器崩溃

ios - 如何修复 iOS 11 上的 UILabel intrinsicContentSize

objective-c - 我应该购买哪一代 Ipod touch?第二还是第三?