ios - 无法将数据写入 plist 文件

标签 ios objective-c plist

<分区>

网上查了方法,就是更新数据失败。

在pickerView:didSelectRow:inComponent方法中,我测试了cameraCode,在pickerView中选择了不同的行后,数组的key已经改变了,但是这个值还没有写入我的plist文件中,该文件名为MultiSetting.plist,即使我写了声明:

path = [[NSBundle mainBundle] pathForResource:@"MultiSetting" ofType:@"plist"];
[settingInfo writeToFile:path atomically:YES];

有什么可以帮助我提高技能的吗?

这是我的代码:

MultiSettingViewController.h

#import <UIKit/UIKit.h>

@interface MultiSettingViewController : UIViewController

@property (nonatomic) NSInteger *camNum;

@property (nonatomic, retain) IBOutlet UIPickerView *picker;
@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (nonatomic,retain) IBOutlet UISegmentedControl *segmentedControl;
@property (retain, nonatomic) IBOutlet UILabel *regionLabel;
@property (retain, nonatomic) IBOutlet UILabel *placeLabel;

@property (nonatomic, retain) NSArray *placeArray;
@property (strong, nonatomic) NSMutableArray *settingArray;
@property (strong, nonatomic) UIToolbar *doneToolbar;
@property (strong, nonatomic) NSBundle *bundle;
@property (strong, nonatomic) NSString *path;
@property (strong, nonatomic) NSBundle *bundle1;
@property (strong, nonatomic) NSString *path1;


- (IBAction)segmentedControlIndexChanged;
- (void)doneBtn_OnClick:(id)sender;
- (IBAction)textfield_OnClick:(id)sender;

@end

MultiSettingViewController.m

#import "MultiSettingViewController.h"

@implementation MultiSettingViewController

@synthesize camNum, picker, placeArray, settingArray, textField, doneToolbar, segmentedControl, bundle, path, bundle1, path1, regionLabel, placeLabel;

- (void)viewDidLoad{
    [super viewDidLoad];

    //NSLog(@"%i", camNum);

    bundle = [NSBundle mainBundle];
    path = [bundle pathForResource:@"HK" ofType:@"plist"];  placeArray = [[NSArray alloc] initWithContentsOfFile:path];

    bundle = [NSBundle mainBundle];
    path = [bundle pathForResource:@"MultiSetting" ofType:@"plist"];    settingArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

    if (self.segmentedControl.selectedSegmentIndex == 0){
        NSInteger selectedRow = [self.picker selectedRowInComponent:0];

        NSDictionary *placeInfo = [placeArray objectAtIndex:selectedRow];
        [regionLabel setText:[placeInfo objectForKey:@"name"]];
    }

    if(self.doneToolbar == nil){
        self.doneToolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 40)];
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                                 target:self
                                                                                 action:@selector(doneBtn_OnClick:)];
        [self.doneToolbar setItems: [NSArray arrayWithObject:doneBtn]];
    }

    self.textField.inputView = self.picker;
    self.textField.inputAccessoryView = self.doneToolbar;
    self.textField.delegate = self;
    self.picker.delegate = self;
    self.picker.dataSource = self;
    self.picker.hidden = YES;


    self.doneToolbar.barStyle = UIBarStyleBlackTranslucent;
    self.doneToolbar.hidden = YES; }

-(IBAction) segmentedControlIndexChanged{
    switch (self.segmentedControl.selectedSegmentIndex){
        case 0:
            bundle = [NSBundle mainBundle];
            path = [bundle pathForResource:@"HK" ofType:@"plist"];
            placeArray = [[NSArray alloc] initWithContentsOfFile:path];
            [self.picker reloadAllComponents];

            NSInteger selectedRow = [self.picker selectedRowInComponent:0];
            NSDictionary *placeInfo = [placeArray objectAtIndex:selectedRow];
            [regionLabel setText:[placeInfo objectForKey:@"name"]];
            break;
        case 1:
            bundle = [NSBundle mainBundle];
            path = [bundle pathForResource:@"KW" ofType:@"plist"];
            placeArray = [[NSArray alloc] initWithContentsOfFile:path];
            [self.picker reloadAllComponents];

            selectedRow = [self.picker selectedRowInComponent:0];
            placeInfo = [placeArray objectAtIndex:selectedRow];
            [regionLabel setText:[placeInfo objectForKey:@"name"]];
            break;
        case 2:
            bundle = [NSBundle mainBundle];
            path = [bundle pathForResource:@"TW" ofType:@"plist"];
            placeArray = [[NSArray alloc] initWithContentsOfFile:path];
            [self.picker reloadAllComponents];

            selectedRow = [self.picker selectedRowInComponent:0];
            placeInfo = [placeArray objectAtIndex:selectedRow];
            [regionLabel setText:[placeInfo objectForKey:@"name"]];
            break;
        case 3:
            bundle = [NSBundle mainBundle];
            path = [bundle pathForResource:@"TM" ofType:@"plist"];
            placeArray = [[NSArray alloc] initWithContentsOfFile:path];
            [self.picker reloadAllComponents];

            selectedRow = [self.picker selectedRowInComponent:0];
            placeInfo = [placeArray objectAtIndex:selectedRow];
            [regionLabel setText:[placeInfo objectForKey:@"name"]];
            break;
        case 4:
            bundle = [NSBundle mainBundle];
            path = [bundle pathForResource:@"TP" ofType:@"plist"];
            placeArray = [[NSArray alloc] initWithContentsOfFile:path];
            [self.picker reloadAllComponents];

            selectedRow = [self.picker selectedRowInComponent:0];
            placeInfo = [placeArray objectAtIndex:selectedRow];
            [regionLabel setText:[placeInfo objectForKey:@"name"]];
            break;
        case 5:
            bundle = [NSBundle mainBundle];
            path = [bundle pathForResource:@"ST" ofType:@"plist"];
            placeArray = [[NSArray alloc] initWithContentsOfFile:path];
            [self.picker reloadAllComponents];

            selectedRow = [self.picker selectedRowInComponent:0];
            placeInfo = [placeArray objectAtIndex:selectedRow];
            [regionLabel setText:[placeInfo objectForKey:@"name"]];
            break;
        case 6:
            bundle = [NSBundle mainBundle];
            path = [bundle pathForResource:@"LT" ofType:@"plist"];
            placeArray = [[NSArray alloc] initWithContentsOfFile:path];
            [self.picker reloadAllComponents];

            selectedRow = [self.picker selectedRowInComponent:0];
            placeInfo = [placeArray objectAtIndex:selectedRow];
            [regionLabel setText:[placeInfo objectForKey:@"name"]];
            break;
        default:
            break;
    } }

#pragma mark -
#pragma mark UIPickerViewDataSource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {    return 1; }

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {   return [placeArray count]; }

#pragma mark -
#pragma mark UIPickerViewDelegate methods
- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row             forComponent:(NSInteger)component {     NSDictionary *placeInfo = [placeArray objectAtIndex:row];   return [placeInfo objectForKey:@"name"]; }

- (void)pickerView:(UIPickerView *)pickerView     didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component {   NSDictionary *placeInfo = [placeArray objectAtIndex:row];   NSString *name = [placeInfo objectForKey:@"name"];
    self.placeLabel.text = name;
    NSString *code = [placeInfo objectForKey:@"cameraCode"];

    NSMutableDictionary *settingInfo = [settingArray objectAtIndex:(NSInteger)camNum];
    NSLog(@"%@", [settingInfo objectForKey:@"cameraCode"]);
    [settingInfo setObject:code forKey:@"cameraCode"];
    NSLog(@"%@", [settingInfo objectForKey:@"cameraCode"]);
    path = [[NSBundle mainBundle] pathForResource:@"MultiSetting" ofType:@"plist"];
    [settingInfo writeToFile:path atomically:YES];

    //[self obtainPic:cameraCode];

    /*aTimer = [NSTimer scheduledTimerWithTimeInterval:60.0
     target:self
     selector:@selector(timerFired:)
     userInfo:nil
     repeats:YES];
     */ }

- (void)doneBtn_OnClick:(id)sender{
    [self.textField endEditing:YES];
    self.picker.hidden = YES;
    self.doneToolbar.hidden = YES; }

- (IBAction)textfield_OnClick:(id)sender{
    self.picker.hidden = NO;
    self.doneToolbar.hidden = NO; }

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. }

@end

非常感谢您的真诚帮助!

最佳答案

主包中包含的所有内容都是只读;因此,您的日志中可能会出现 Cocoa 错误 513(“操作无法完成。不允许操作”)。

您还有另外三个可读/写数据存储文件夹:TempCacheDocuments

您可以在运行时在Document 文件夹 中创建文件MultiSetting.plist,然后写入其中。


您可以像这样检索文档目录的路径:

 NSArray * documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString * documentDirectory = [documentPath objectAtIndex:0];

编辑

然后,您可以在其中复制您的 pList(请注意,如前所述,它是只读的,因此您将无法移动它,也无法将其从您的包中删除)。为此,使用 NSFileManagers 的函数 -copyItemAtPath :

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error

关于ios - 无法将数据写入 plist 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17531072/

上一篇:ios - 如何在 AFNETWORKING 中为参数设置多个值

下一篇:iphone - 根据不区分大小写的子字符串拆分字符串

相关文章:

objective-c - 将 twitter 集成到 iOS 5,但保持与旧版 iOS 的向后兼容性

objective-c - 你可以在 iOS 上调整 UIImage 的大小同时保持比例吗?用于生成缩略图

ios - 如何在 block 内为 self 赋值?

ios - 从 plist 刷新数据

iphone - Plist 文件转换 xml 文件在 iphone 中?

ios - 有什么方法可以在 xcode 中以十六进制显示 plist 数值吗?

ios - 从服务器加载的 CollectionView/TableView 中的图像存在问题,不会立即加载到每个单元格中

ios - 在 ios 中配置 InAppPurchase 进行沙盒测试

ios - Facebook FBSDK Corekit 的语义问题

IOS Objective-c - 让联系人不起作用 - 没有请求提示 - 访问被拒绝