【黑马程序员】Foundation框架(三)

来源:互联网 发布:淘宝保证金计划不符合 编辑:程序博客网 时间:2024/05/19 04:56

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

一、NSDictionary的使用


    1、NSDictionary介绍

       NSDictionary为Foundation框架中的字典,它有key(键)和value(值)。
       并且每有一个key都会有一个value与之对应。
       NSDictionary是不可变的,一旦初始化完毕,里面的内容就无法修改。

    2、NSDictionary的创建

      创建一个空的字典:dictionary方法    NSDictionary类方法    无参     返回NSDictionary类型(Id)
      创建只有一对键值的字典:dictionaryWithObject:forKey方法    NSDictionary类方法     参数:值+键     返回NSDictionary类型(Id)
      创建多对键值的字典:dictionaryWithObjectsAndKeys方法    NSDictionary类方法     参数:先值后键(中间用","分隔,结尾用nil结束)    返回NSDictionary类型(Id)  
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSDictionary *dic1 = [NSDictionary dictionary];        NSDictionary *dic2 = [NSDictionary dictionaryWithObject:@"I love you,my boy" forKey:@"gay"];        NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"sorry,I'm  not a gay",@"sr", nil];    }    return 0;}

      快速创建字典
@{@"hl":@"hello",@"wd":@"world"};

    3、键值对集合的特点

       1)字典存储的时候,必须是“键值对”的方式来存储。同时键不能重复。
       2)键值对中存储的数据是无序的。
       3)键值对集合可根据键快速得到值。

    4、NSDictionary常用方法

      得到字典的键值对数目:count方法     NSDictionary对象方法     无参       返回数目NSUInteger类型
      根据键得值:objectForKey方法     NSDictionary对象方法     参数:键      返回值id类型
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"hahahahahaha",@"ha", nil];        NSUInteger i= [dic1 count];        NSString *str = [dic1 objectForKey:@"ha"];        NSLog(@"%@,%ld",str,i);            }    return 0;}

      将字典写入文件:writeToFile:atomically方法    NSDictionary对象方法     返回BOOL类型   
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"hahahahahaha",@"ha", nil];        [dic1 writeToFile:@"/Users/xxxx/Desktop/waou.plist" atomically:YES];    }    return 0;}

   5、NSDictionary与NSArray的对比

     1)NSArray是有序的,NSDictionary是无序的。
     2)NSArray是通过下标访问元素,NSDictionary是通过key访问元素

二、NSMutableDictionary的使用


    1、NSMutableDictionary介绍

      NSMutableDictionary是NSDictionary的子类。它是可变的,随时可以进行添加、删除和更改。
      而NSDictionary是不可变的。

    2、NSMutableDictionary常用方法

      除NSDictionary可以使用的方法外
      添加一个键值对:setObject:forKey方法    NSMutableDictionary对象方法    参数:值+键     返回void
      通过键删除值:removeObjectForKey方法   NSMutableDictionary对象方法    参数:键     返回void
      删除所有键值对:removeAllObjects   NSMutableDictionary对象方法    无参    返回void

三、NSFileManager的使用


    1、NSFileManager介绍

      NSFIleManager是用来管理文件系统的,他可以进行常见文件与文件夹的创建、拷贝、剪切等操作。
      NSFileManager使用了单例模式singleton,使用defaultManager方法可以获得那个单例对象
[NSFileManager defaultManager];

    2、NSFileManager常用方法

      判断指定文件或者文件夹是否存在(方法1):fileExistsAtPath方法    NSFileManager对象方法    参数:指定的文件或文件夹地址    返回BOOL(1存在 0不存在)
      判断指定文件或者文件夹是否存在(方法2):fileExistsAtPath:isDirectory方法        NSFileManager对象方法    
                                                                                参数:指定的文件或文件夹地址+用来接收是否是文件夹的BOOL变量的地址(1是 0不是)  返回BOOL(1存在 0不存在)
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSFileManager *fil = [NSFileManager defaultManager];        BOOL isYes = [fil fileExistsAtPath:@"/User/xxxx/Desktop/oc.txt"];        if (isYes) {            NSLog(@"此文件存在");        }                BOOL isfolder;        BOOL isYes1 = [fil fileExistsAtPath:@"/User/xxxx/Desktop/oc.txt" isDirectory:&isfolder];        if (isYes1) {            NSLog(@"此文件存在");            if (isfolder) {                NSLog(@"是个文件夹");            }        }    }    return 0;}

      判断指定文件或文件夹是否可读:isReadableFileAtPath方法    NSFileManager对象方法    参数:指定的文件或文件夹地址    返回BOOL(1可读 0不可读)
      判断指定文件或文件夹是否可写:isWritableFileAtPath方法   NSFileManager对象方法    参数:指定的文件或文件夹地址    返回BOOL(1可写 0不可写)
      判断指定文件或文件夹是否可删除:isDeletableFileAtPath方法   NSFileManager对象方法    参数:指定的文件或文件夹地址    返回BOOL(1可删 0不可删)
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSFileManager *fil = [NSFileManager defaultManager];        BOOL isYes = [fil isWritableFileAtPath:@"/User/xxxx/Desktop/oc.txt"];  //可读性        if (isYes) {            NSLog(@"可写");        }        BOOL isYes1 = [fil isReadableFileAtPath:@"/User/xxxx/Desktop/oc.txt"]; //可写性        if (isYes1) {            NSLog(@"可读");        }        BOOL isYes2 = [fil isDeletableFileAtPath:@"/User/xxxx/Desktop/oc.txt"]; //可删性        if (isYes2) {            NSLog(@"可删");        }    }    return 0;}

      获得指定文件或文件夹的属性:attributesOfItemAtPath:error方法   NSFileManager对象方法    参数:指定文件或文件夹地址+错误返回信息(nil)   返回:NSDictionary类型
      查找指定路径下的所有路径,深度查找,不限于当前层,也会查找package中的内容:
      subpathsAtPath方法    NSFileManager对象方法     参数:指定的路径     返回:NSArray类型
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSFileManager *fil = [NSFileManager defaultManager];        NSDictionary *dic = [fil attributesOfItemAtPath:@"/User/xxxx/Desktop/oc.txt" error:nil];  //获取属性        NSArray *arr = [fil subpathsAtPath:@"/User/xxxx/Desktop/oc.txt"];  //深度查找路径    }    return 0;}

      获得指定目录下的所有子内容:contentsOfDirectoryAtPath:error方法    NSFileManager对象方法   参数:指定的目录+错误信息(nil)     返回NSArray
      获得文件内容:contentsAtPath方法    NSFileManager对象方法     参数:文件的地址    返回NSData类型
      创建文件夹:createDirectoryAtPath:withIntermediateDirectories:attributes:error方法    NSFileManager对象方法     
                           参数:路径+如没有之前的路径是否创建+文件属性(nil)+错误信息(nil)    返回BOOL
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSFileManager *fil = [NSFileManager defaultManager];        NSArray *arr = [fil contentsOfDirectoryAtPath:@"/User/xxxx/Desktop/" error:nil]; //指定路径的子文件        NSData *data1 = [fil contentsAtPath:@"/User/xxxx/Desktop/oc.txt"];  //获取文件内容        BOOL isYes = [fil createDirectoryAtPath:@"/User/xxxx/Desktop/new" withIntermediateDirectories:YES attributes:nil error:nil];//创建文件夹        if (isYes) {            NSLog(@"创建文件夹成功");        }    }    return 0;}

      拷贝文件(有同名无法拷贝):copyItemAtPath:toPath:error方法    NSFileManager对象方法     参数:原地址+拷贝地址+错误信息(nil)   返回(1成功 0失败)
      移动文件(剪切):moveItemAtPath:toPath:error方法    NSFileManager对象方法     参数:原地址+移动地址+错误信息(nil)   返回BOOL(1成功 0失败)    
      删除文件:removeItemAtPath:error方法   NSFileManager对象方法     参数:文件地址+错误信息(nil)  返回BOOL(1成功 0失败)
      创建文件:createFileAtPath:contents:attributes  NSFileManager对象方法    参数:文件地址+文件内容(NSData类型)+文件属性(nil)  返回BOOL(1成功 0失败)
      NSString转换成NSData类型方法:dataUsingEnoding:NSUTF8StringEncoding方法   NSString对象方法  无参  返回NSData类型
#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {        NSFileManager *fil = [NSFileManager defaultManager];        BOOL isYes = [fil copyItemAtPath:@"/User/xxxx/Desktop/oc.txt" toPath:@"/User/xxxx/Desktop/new/oc.txt" error:nil];  //拷贝文件        if (isYes) {            NSLog(@"拷贝成功");        }        BOOL isYes1 = [fil moveItemAtPath:@"/User/xxxx/Desktop/oc.txt" toPath:@"/User/xxxx/Desktop/text/oc.txt" error:nil];//移动文件        if (isYes1) {            NSLog(@"移动成功");        }        BOOL isYes2 = [fil removeItemAtPath:@"/User/xxxx/Desktop/new/oc.txt" error:nil];//删除文件        if (isYes2) {            NSLog(@"删除成功");        }        NSString *str = @"hello world";//创建文件        NSData *da = [str dataUsingEncoding:NSUTF8StringEncoding];        BOOL isYes3 = [fil createFileAtPath:@"/User/xxxx/Desktop/oc1.txt" contents:da attributes:nil];        if (isYes3) {            NSLog(@"创建成功");        }    }    return 0;}

    3、NSFileManager文件下载思路

      1)发送请求给服务器,要求下载某个文件。
      2)服务器发出相应,返回文件数据。
      3)手机客户端利用NSData来存放服务器返回的文件数据
      4)利用NSFileManager将NSData里面的文件数据写到新的文件中(createFileAtPath)
      
      


      
       
0 0