plist读写,NSArray,NSData,NSnumber,字典等简使用

ios的文件操作分为沙盒,plist,NSUserDefaults三种简单的操作,本文记录plist和NSUserDefaults的使用案例及介绍,Android的应用程序文件是可以提供给第三方程序访问数据的,但是ios程序是不可以的,plist类似于android的SharedPreferences ,android的文件操作也是通过健值对读取的,下面将先介绍ios的几种简单的数据读取;

 

1,NSString字符串

        //NSString
        NSString *s1=@"nihao";
        NSLog(@"字符串%@",s1);

 运行结果:2015-11-29 15:27:47.900 StringCZ[693:25540] 字符串nihao

 

2,NSArray数组

        //NSArray
        NSArray *array=[NSArray arrayWithObjects:s1,@"21",@"22", nil];
        NSLog(@"%@",array);
        

        //遍历数组
        for (int n=0; n<[array count]; n++) {
            //遍历指定位置的值objectAtIndex
            NSLog(@"%@",[array objectAtIndex:n]);
        }

 运行结果:

2015-11-29 15:27:47.901 StringCZ[693:25540] (

    nihao,

    21,

    22

)

2015-11-29 15:27:47.901 StringCZ[693:25540] nihao

2015-11-29 15:27:47.901 StringCZ[693:25540] 21

 

2015-11-29 15:27:47.901 StringCZ[693:25540] 22

 

 

3,不可变字典(类似于java的String ,可变字典类似于java的Stringbuffer,ios可变的标示是Mutable

        //类方法创建对象不需要释放
        NSDictionary *dic1=[NSDictionary dictionaryWithObject:@"C++" forKey:@"C"];
        NSLog(@"获取C对应的值=%@",[dic1 objectForKey:@"C"]);
        
        
        //数组中添加字符串,整型数据,NSArray
        NSNumber *number=[NSNumber numberWithInt:100];
        NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:@"C++",@"C",number ,@"num",array,@"arr", nil];
        
        NSLog(@"数组中dic2%@",dic2);

 

运行结果:

2015-11-29 15:27:47.901 StringCZ[693:25540] 获取C对应的值=C++
2015-11-29 15:27:47.901 StringCZ[693:25540] 数组中dic2{
    C = "C++";
    arr =     (
        nihao,
        21,
        22
    );
    num = 100;
}

 

 

4,可变字典添加和移除数据(类似于java 的Map)

        //初始化可变字典
        NSMutableDictionary *dic =[[NSMutableDictionary alloc]init];
        //添加数据
        [dic setValue:@"Java" forKey:@"qq"];
        [dic setValue:@"Android" forKey:@"A"];
        [dic setValue:@"Object-c" forKey:@"O"];
        
        NSLog(@"获取NSMutableDictionary的值=%@",dic);
        
        //移除qq
        [dic removeObjectForKey:@"qq"];
         NSLog(@"获取移除qq的NSMutableDictionary的值=%@",dic);

 

运行结果:

2015-11-29 15:27:47.901 StringCZ[693:25540] 获取NSMutableDictionary的值={

    A = Android;

    O = "Object-c";

    qq = Java;

}

2015-11-29 15:27:47.901 StringCZ[693:25540] 获取移除qqNSMutableDictionary的值={

    A = Android;

    O = "Object-c";

 

}

 

 

5,NSData数据的读取(java文件一班通过流写入,写出,ios通过NSData字节操作)

        //  NSData数据的读取
        NSData *data1=[@"nihao" dataUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"data1=%@",data1);
       
        NSString *str2=[[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding];
        
        NSLog(@"data2=%@",data1);

 

运行结果:

2015-11-29 15:27:47.902 StringCZ[693:25540] data1=<6e696861 6f>

 

2015-11-29 15:27:47.902 StringCZ[693:25540] data2=<6e696861 6f>

 

 

6,plist文件的读取

1,创建一个plistdemo的plist文件

2,bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.

我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle

 

 
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
        NSLog(@"plist=%@", data);

 运行结果:

plist={

    age = 18;

    "c_key1" = "add some content";

    name = wj;

 

}

 

 

7,plist文件写入

 NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

    
    [data setObject:@"add some content1" forKey:@"c_key1"];
    [data setObject:@"add some content" forKey:@"c_key1"];

    
    [data writeToFile:plistPath atomically:YES];
        NSLog(@"plist=%@", data);
    
    NSString * strKey=[data objectForKey:@"age"];
    NSLog(@"plist文件的key取values=%@",strKey);

 

 

运行结果:

2015-11-29 15:17:02.943 txl[656:20688] plist={

    age = 18;

    "c_key1" = "add some content";

    name = wj;

}

2015-11-29 15:17:02.944 txl[656:20688] plist文件的keyvalues18

 

8,NSUserDefaults读写数据

 

      //存储数据
        NSUserDefaults *nd=[NSUserDefaults standardUserDefaults];//单例模式创建
        [nd setObject:self.tf1.text forKey:userNameKey];
        [nd setObject:self.tf2.text forKey:userPwdKey];
        [nd setBool:true forKey:onOffKey];
        
        //同步数据
        [nd synchronize];

 

读取数据:

    //读取数据
    NSUserDefaults *defaults= [NSUserDefaults standardUserDefaults];
    self.tf1.text=[defaults valueForKey:userNameKey];
    self.tf2.text = [defaults valueForKey:userPwdKey];
    [defaults boolForKey:onOffKey];
    

 

 

你可能感兴趣的:(数组,字典,plist)