zoukankan      html  css  js  c++  java
  • NSDictionary

    向字典2对象中添加整个字典对象3

    [dic2 addEntriesFromDictionary:dic3];

     

          

        /*----------创建字典----------------------*/  

        - (id) initWithObjectsAndKeys;  

          

        NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];  

        NSString *string = [dictionary objectForKey:@"One"];  

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

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

        [dictionary release];  

          

        

    NSMutableDictionary

          

        /*--------------创建可变字典----------------------*/      

        创建  

        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];  

          

        添加字典  

        [dictionary setObject:@"One" forKey:@"1"];  

        [dictionary setObject:@"Two" forKey:@"2"];  

        [dictionary setObject:@"Three" forKey:@"3"];  

        [dictionary setObject:@"Four" forKey:@"4"];  

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

          

        删除指定的字典  

        [dictionary removeObjectForKey:@"3"];  

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

      

    NSValue(对任何对象进行包装)

              /*-------------将NSRect放入NSArray中---------------*/  

        将NSRect放入NSArray中  

        NSMutableArray *array = [[NSMutableArray alloc] init];  

        NSValue *value;  

        CGRect rect = CGRectMake(0, 0, 320, 480);      

        value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];  

        [array addObject:value];  

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

          

        从Array中提取  

        value = [array objectAtIndex:0];  

        [value getValue:&rect];  

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

          

           

    从目录搜索扩展名为jpg的文件

          

        NSFileManager *fileManager = [NSFileManager defaultManager];  

        NSString *home;  

        home = @"../Users/";  

          

        NSDirectoryEnumerator *direnum;  

        direnum = [fileManager enumeratorAtPath: home];  

        NSMutableArray *files = [[NSMutableArray alloc] init];  

          

        枚 举  

        NSString *filename;  

        while (filename = [direnum nextObject]) {  

            if([[filename pathExtension] hasSuffix:@"jpg"]){  

                [files addObject:filename];  

            }  

        }  

          

        快速枚举  

        for(NSString *filename in direnum)  

        {  

            if([[filename pathExtension] isEqualToString:@"jpg"]){  

                [files addObject:filename];  

            }  

        }  

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

          

        枚举  

        NSEnumerator *filenum;  

        filenum = [files objectEnumerator];  

        while (filename = [filenum nextObject]) {  

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

        }  

          

        快速枚举  

        for(id object in files)  

        {  

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

    }  

     

     

     

         

    只判断指针数值,而不是它们所指向的内容

             */  

            if(thing1==thing2){  

                NSLog(@"== same");  

            }  

              

            /*

             compare:比较两个字符串。区分大小写

             compare将接收 对象和传递来的字符串逐个字符的进行比较,它返回一个NSComparisonResult(枚举类 型)来显示结果。

             typedef enum _NSComparisonResult{

             NSOrderedAscending=-1;

             NSOrderedsame;

             NSOrderedDescending;

             } NSComparisonResult;

             */  

            [thing1 compare:thing2];  

            if(NSOrderedSame==[thing1 compare:thing2]){  

                NSLog(@"compare same");   

            }  

              

            compare:options:   

            /***

             -(NSComparisonResult) compare:(NSString *) string  

             options:(unsinged) mask;

              

             options 是一个位掩 码,可以使用|添加选项标记

             选 项:

             NSCaseInsensitiveSearch:分大小字符

            NSLiteralSearch:行完全比分大小

            NSNumbericSearch:字符串的字符个数,而不是字符

             */  

            if([thing1 compare:thing2 options:NSCaseInsensitiveSearch|  

                 NSNumericSearch]==NSOrderedSame){  

                NSLog(@"they match");  

            }

              

            删 除字符串  

            -(void) deleteCharactersInRange:(Rangerange;  

              

            NSMutableString *ms;  

            ms=[NSMutableString stringWithCapacity:50];  

            [ms appendString:@"aabbccdd"];  

            NSRange range;  

            range=[ms rangeOfString:@"cc"];  

            [ms deleteCharactersInRange:range];  

            NSLog(ms);  

            

            ------------切分 数组  

            -componentsSeparatedByString:  

            NSString *ns=@"sdf,dsfs,dfd,fdf,df,dd";  

            NSArray *comArr=[ns componentsSeparatedByString:@","];  

            for(int i=0;i<[comArr count];i++){  

                NSLog(@"componentsSeparatedByString===%@",[comArr objectAtIndex:i]);  

            }  

              

            componentJoinedByString: 合并nsarray中的元素来创建字符串  

            NSString *joinedStr=[comArr componentsJoinedByString:@"-->"];  

            NSLog(@"joined---= %@",joinedStr);  

         

            各种数值,NSNumber NSValue  

            /*

             cocoa 提供了NSNumber类来包装基本数据类型

             +(NSNumber *) numberWithChar:(char) value;

             +(NSNumber *) numberWithInt:(int) value;

             +(NSNumber *) numberWithFloat:(float) value;

             +(NSNumber *) numberWthiBool:(BOOL) value;

              

             -(char) charValue;

             -(int) intVlaue;

             -(float) floatValue;

             -(BOOL) boolValue;

             -(NSString *) stringValue;

              

              

             **/  

            NSNumber *number;  

            number=[NSNumber numberWithInt:3];  

            [mutableDic setObject:number forKey:@"int"];  

              

            int num=[[mutableDic objectForKey:@"int"] intValue];  

            NSLog(@"int object value== %d",num);  

              

              

            NSValue .NSNumber实际上是NSValue的子类,NSValue可以包装任意值  

              

            /**

             +(NSValue *) valueWithBytes:(const void *) value objCType:(const char *) type;

             传递的参数是你想要包装的数值的地址,通常,得到的是你想要存储的变量的地址(在c语言里适用操作符 & ),你也可以提供一个描述这个数据类型的字符串,通常用来说明struct中实体的类型和大小。你不用自己写代码

             来生成这个字符串,@encode编译器指令可以接受数据类型的名称并为你生成合适的字符串

             */  

            NSRect rect= NSMakeRect(1, 2, 30, 40);  

              

            NSValue *value;  

            value=[NSValue valueWithBytes:&rect objCType:@encode(NSRect)];  

            NSMutableArray *mr=[NSMutableArray arrayWithCapacity:50];  

            [mr addObject:value];  

               

            getValue 提取 数据  

            /**

             -(void) getValue:(void *) value; 要传递的是存储这个数值的变量的地址

             */  

              

            /***

            value=[mr objectAtIndex:0];

              

            NSRect r;

            NSLog(@"00000 ===%@",r);

            [value getValue:&r];

            NSLog(@"111== %@",r);

            */

            /**

             +(NSValue *) valueWithPoint:(NSPoint) point;

             +(NSValue *) valueWithSize:(NSSize) size;

             +(NSValue *) valueWithRect:(NSRect) rect;

             -(NSPoint) pointValue;

             -(NSSize) sizeValue;

             -(NSRect) rectValue;

              

             */  

              

            NSNull   

            /*

             *+(NSNull *) null;  

            */  

            [mutableDic setObject:[NSNull null] forKey:@"fax"];  

            id fax;  

            fax=[mutableDic objectForKey:@"fax"];  

            if(fax==[NSNull null]){  

                NSLog(@"pppppppppppppppppp");  

            }  

              

            [pool drain];  

            return 0; 

  • 相关阅读:
    既然选择了远方,就只顾风雨兼程!
    slots
    面向对象
    模块和作用域
    偏函数
    python中decorator
    返回函数
    filter, sort
    map/reduce
    开发步骤
  • 原文地址:https://www.cnblogs.com/zhaozhongpeng/p/4867910.html
Copyright © 2011-2022 走看看