Plist/NSUserDefault解析


//*****************************Plist解析*****************************
//1.1創建視圖對象

- (void)createButtons

{

    NSArray *arr = @[@"讀取Plist文件",@"寫入Plist文件"];

    for (int i=0; i<2; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom ];

        btn.frame = CGRectMake(40, 100+i*200, 300, 40);

        btn.titleLabel.font = [UIFont systemFontOfSize:26];

        [btn setTitle:arr[i] forState:UIControlStateNormal];

        btn.tag = 100+i;

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

    }

    self.view.backgroundColor = [UIColor lightGrayColor];

}

//1.2定義按鈕點擊事件處理方法

- (void)btnClick:(UIButton *)btn

{

    //2讀取Plist文件

    if (btn.tag == 100) {

        //2.1.取出PLIST文件路徑

        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyList" ofType:@"plist"];

        //2.2讀取文件內容創建字典

        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];

        //2.3取到字典中數據

        NSString *userName = [dict objectForKey:@"UserName"];

        NSString *passWord = [dict objectForKey:@"Password"];

        NSString *nickName = [dict objectForKey:@"NickName"];

      

//2.3.1遍歷子成員數組獲取字典對象 

        for (NSDictionary *dict in array) {

     NSLog(@"dict = %@", dict);

            }

        NSLog(@"userName:%@",userName);

        NSLog(@"passWord:%@",passWord);

        NSLog(@"nickName:%@",nickName);

       

    }else{


    //3.創建寫入Plist文件數據

        //plist文件中的根節點只能是數組或字典,plist文件中存放的數據只能         

                     是NSDATA,NSDATE,NSString,NSNumber,Bool,NSArray,NSDictionary

        //3.1構建寫入plist中的字典或者數組

        NSArray *array = @[@"first",@"second",@"third"];

        NSDictionary *dict = [NSDictionary

        dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",array,@"array", nil];

        //3.2組合字典文件路徑

        NSString *plistPath = [[[NSBundle mainBundle]bundlePathstringByAppendingString:@"/mydata.plist"];

        //3.3寫入plist文件

        BOOL res = [dict writeToFile:plistPath atomically:YES];

        if (res) {

            NSLog(@"寫入成功");

        }else{

            NSLog(@"寫入失敗");

        }

    }

}


//*****************************NSUserDefault文件解析*****************************

- (void)btnClick:(UIButton *)btn

{

    //應用程序的Defaults System

    //1.創建(單例方法,取到應用程序中的默認的Defaults System)

    NSUserDefaults *df = [NSUserDefaults standardUserDefaults];

    if (btn.tag == 101) {

        //1.2設置df

        [df setObject:@"AAA" forKey:@"KEY_AAA"];

        [df setObject:@"BBB" forKey:@"KEY_BBB"];

        

        //1.3同步數據,只有執行了這行代碼,df中寫入數據,纔會真正的執行

        BOOL res = [df synchronize];

        if (res) {

            NSLog(@"同步寫入成功");

        }else{

            NSLog(@"同步寫入失敗");

        }

    }else{

        //2讀取

        NSString *aaa = [df objectForKey:@"KEY_AAA"];

        NSLog(@"%@",aaa);

    }

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章