Iphone笔记(4)

简介: 1 关于字典的常见操作     NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"小明",@"name",@"1392711589",@"tel",@"b...

1 关于字典的常见操作

    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"小明",@"name",@"1392711589",@"tel",@"boy",@"gender" ,nil];

    NSLog(@"dictionary内容=%@",dictionary);

    int count=[dictionary count];

    NSLog(@"[dictionary count]大小=%i",count);

    //得到字典中所有的键,并且遍历

    NSEnumerator *keys=[dictionary keyEnumerator];

    for (NSObject *key in keys) {

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

    }

     

    //得到字典中所有的值,并且遍历

    NSEnumerator *values=[dictionary objectEnumerator];

    for (NSObject *value in values) {

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

    }

   

    //得到字典中的所有键,并且判断是否包含了某一个键

    NSArray *keyArray=[dictionary allKeys];

    if ([keyArray containsObject:@"name"]) {

        NSLog(@"包含了键name");

    }

    //得到字典中的所有值,并且判断是否包含了某一个值

    NSArray *valueArray=[dictionary allValues];

    if ([valueArray containsObject:@"boy"]) {

        NSLog(@"包含了值boy");

    }

 

    //依据键找到值

    NSString *name=[dictionary objectForKey:@"name"];

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

   

   

   

   //以下为可变字典NSMutableDictionary的类似操作

    NSMutableDictionary *mutableDictory=[NSMutableDictionarydictionaryWithCapacity:10];

    [mutableDictory setObject:@"小狗" forKey:@"name"];

    [mutableDictory setObject:@"111" forKey:@"number"];

    //得到字典中所有的键,并且遍历

    NSEnumerator *keyss=[mutableDictory keyEnumerator];

    for (NSObject *key2 in keyss) {

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

    }

    //得到字典中所有的值,并且遍历

    NSEnumerator *valuess=[mutableDictory objectEnumerator];

    for (NSObject *value2 in valuess) {

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

    }

 

2 关于集合的操作

    NSMutableSet *setOne=[NSMutableSet setWithObjects:@"1",@"2",@"3",@"4",nil];

    NSSet *setTwo=[NSSet setWithObjects:@"2",@"3",@"4",@"5",nil];

    NSSet *setThree=[NSSet setWithObjects:@"3",@"4",@"5",@"6",@"7",nil];

    //移除集合中的某个对象

    [setOne removeObject:@"1"];

    //往集合中添加某个对象

    [setOne addObject:@"1"];

    //判断集合中是否包含某个对象

    if ([setOne containsObject:@"1"]) {

         NSLog(@"包含了该对象");

    }

   //判断两个对象是否存在交集

    if ([setTwo intersectsSet:setThree]) {

          NSLog(@"两个集合有交集");

    }

   

    //取得两个集合的交集.

    //注意

    //1 setOne调用intersectSetsetOne中的值已经改变了,保存的是setOnesetTwo中的公共部分

    //2 intersectSetNSMutableSet特有的,NSSet中没有此方法

    //3 intersect横穿,横切.相交,交叉的意思

    [setOne intersectSet:setTwo];

    NSLog(@"-- 00改变后setOne中的值为=%@",setOne);

  

    //取得两个集合的并集

    //即执行unionSet方法后.setOne中保存的是setOnesetThree中的所有元素

    [setOne unionSet:setThree];

    NSLog(@"-- 11改变后setOne中的值为=%@",setOne);

 

    //取得两个集合的差级

    //即把setOnesetTwo的公共部分从setOne中去除

    [setOne minusSet:setTwo];

    NSLog(@"-- 22改变后setOne中的值为=%@",setOne);

 

3 关于文件的操作

    //1 Documents下创建目录并且复制

    NSFileManager *fileManager=[NSFileManager defaultManager];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectoryPath = [paths objectAtIndex:0];

    NSString *path=[documentsDirectoryPath stringByAppendingString:@"/xha"];

         BOOL fileExists = [fileManager fileExistsAtPath: path];

         if (!fileExists) {

              [fileManager createDirectoryAtPath: path withIntermediateDirectories:YES attributes: nil error: nil];

         }

    //修改文件名称--xha修改为了newPath

    NSString *newPah=[documentsDirectoryPath stringByAppendingString:@"/newPath"];

    if ([fileManager moveItemAtPath:path toPath:newPah error:nil]) {

        NSLog(@"重命名成功");

    }

   

    //关于文件的复制

    //判断目录是否存在

    if ([fileManager fileExistsAtPath:newPah]) {

        NSLog(@"该目录存在");

    }

     NSString *newestPah=[documentsDirectoryPathstringByAppendingString:@"/theNewestPah"];

    //判断两个路径下的内容是否一致

    if ([fileManager contentsEqualAtPath:newPah andPath:newestPah]==NO) {

        //获得文件的长度

        NSDictionary *dictionary=[fileManager attributesOfItemAtPath:newPaherror:nil];

        int length=[[dictionary objectForKey:NSFileSize] intValue];

        NSLog(@"原文件长度:length=%i",length);

        //开始复制文件

        if ([fileManager copyItemAtPath:newPah toPath:newestPah error:nil]) {

            NSLog(@"复制成功");

        }

        //复制完成后删除原始文件

        if ([fileManager removeItemAtPath:newPah error:nil]) {

            NSLog(@"原始文件删除成功");

        }   

    }

 

 

    //2 利用NSData作为缓冲,复制文件(也可以这么做)

    NSData *buffer=[fileManager contentsAtPath:path];

    NSString *anotherPath=[documentsDirectoryPathstringByAppendingString:@"/anotherPath"];

    if ([fileManager createFileAtPath:anotherPath contents:buffer attributes:nil]) {

        NSLog(@"文件复制成功");

    }

   

   //3 遍历指定目录下的所有文件

    NSString *path=@"/Users/niko/Desktop/temp008";

    NSFileManager *fileManager=[NSFileManager defaultManager];

    //利用NSFileManager得到指定路径下的目录迭代器NSDirectoryEnumerator

    NSDirectoryEnumerator *directoryEnumerator=[fileManager enumeratorAtPath:path];

    while (path=[directoryEnumerator nextObject]) {

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

    }

 

4 关于NSFileHandle的操作

  总框: 利用NSFileHandle可以让我们更近距离地操作文件

 

    //1 利用NSFileHandle复制.rtf文档

    NSFileHandle *readedFileHandle;

    NSFileHandle *writedFileHandle;

    NSData *buffer;

    //建立目标文件

    [[NSFileManager defaultManager]createFileAtPath:@"/Users/niko/Desktop/readme2.rtf"contents:nil attributes:nil];

    //得到原文件的NSFileHandle

    readedFileHandle=[NSFileHandlefileHandleForReadingAtPath:@"/Users/niko/Desktop/readme1.rtf"];

    //得到目标文件的NSFileHandle

    writedFileHandle=[NSFileHandlefileHandleForWritingAtPath:@"/Users/niko/Desktop/readme2.rtf"];

    if (readedFileHandle!=nil&&writedFileHandle!=nil) {

        //将目标文件的NSFileHandle长度剪短为0.即将文件长度设置为0,类似于将文件初始化,避免残留数据

        [writedFileHandle truncateFileAtOffset:0];

        //将原数据转存到buffer

        buffer=[readedFileHandle readDataToEndOfFile];

        //将缓冲去区的数据写到目标文件

        [writedFileHandle writeData:buffer];

        //关闭NSFileHandle

        [readedFileHandle closeFile];

        [writedFileHandle closeFile];

    }

 

 

    //2将一个txt中的内容追加到另外一个txt文件的末尾

    //注意:

    //(1) 利用rtf是不可以实现的!!!因为它是富文本形式

    //(2) 可以利用seekToEndOfFile返回的数值判读目前文件的大小

    NSFileHandle *readedFileHandle;

    NSFileHandle *writedFileHandle;

    NSData *buffer;

   

    //得到原文件的NSFileHandle

    readedFileHandle=[NSFileHandlefileHandleForReadingAtPath:@"/Users/niko/Desktop/readme1.txt"];

    //得到目标文件的NSFileHandle

    writedFileHandle=[NSFileHandlefileHandleForWritingAtPath:@"/Users/niko/Desktop/readme2.txt"];

    //得到原文件的NSFileHandle

    if (readedFileHandle!=nil && writedFileHandle!=nil) {

        //将原数据转存到buffer

        buffer=[readedFileHandle readDataToEndOfFile];

        //将目的.txt的输入位置设置为文件末尾

        [writedFileHandle seekToEndOfFile];

        int i1=[writedFileHandle seekToEndOfFile];

        NSLog(@"追加前长度=%i",i1);

        //将缓冲区的数据写到目标文件

        [writedFileHandle writeData:buffer];

        int i2=[writedFileHandle seekToEndOfFile];

        NSLog(@"追加后长度=%i",i2);

        //关闭NSFileHandle

        [writedFileHandle closeFile];

        [readedFileHandle closeFile];

    }

         

 

 

 关于Resources的操作

    //1 获取Resources文件夹的路径

    NSString *resourcesPath1 = [[NSBundle mainBundle] resourcePath];

    //2 获取Resources文件夹下具体资源文件的路径

    NSString *resourcesPath2=[[NSBundle mainBundle] pathForResource:@"readme2"ofType:@"txt"];

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

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

 

6 清除.svn的命令

find . -name ".svn" | xargs rm -rf

相关文章
|
Web App开发 缓存 开发工具
|
iOS开发
Iphone笔记(3)
1 关于属性的.和setXXX( ) 比如一个类在.h文件中有一个属性@property (nonatomic, retain) UIImage *photo; 在其.
904 0
|
iOS开发
Iphone笔记(2)
关于IB的连接 1 给xib设定类,即指定它是哪个类的视图 2 把控件和.h文件中的IBOutlet类型对应的控制相连 3 把控件的点击方法和.
761 0
|
XML 数据格式 iOS开发
Iphone笔记(1)
    1 自定义UITableViewCell 1.(1)要注意给此Cell设置大小.      [self setFrame:CGRectMake(0, 0, 320, 120)]; 1.
789 0
|
iOS开发
iphone开发笔记——Cocos2d CCLayer中的touch
废话不多说,cocos2d 自带有两套协议 分别是CCTargetedTouchDelegate 和 CCStandardTouchDelegate 这两个都是处理touch inside 但是针对点不同, CCTargeted...这个协议针对单点触控,用户直接操作UITouch 而Standard 传入的是一个NSSet  里面是 当前屏幕触摸的每个触摸点的UITouch ,恩
1282 0
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
155 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
229 0