黑马程序员——oc语言学习心得—— Fundation

来源:互联网 发布:详情页美工 编辑:程序博客网 时间:2024/06/01 21:55

黑马程序员——oc语言学习心得—— Fundation

-------Java培训Android培训iOS培训.Net培训、期待与您交流! -------


Foundation框架是Xcode在.h中默认引入的,包含一大堆东西
把自己写的代码发出来供参考  

字符串比较

isEqualToString  区分大小写 返回值bool类型

if ([str1 isEqualToString:str2]) {                        NSLog(@"yes");        }else{                    NSLog(@"no");                }


2, compare 默认区分大小写 NSCaseInsensitiveSearch不区别大小写

SComparisonResult result=[str1 compare:str2 options:NSCaseInsensitiveSearch];                switch (result) {            case NSOrderedAscending:                NSLog(@"a<b,升序");                break;            case NSOrderedDescending:                NSLog(@"a>b,降序");                break;            case NSOrderedSame:                NSLog(@"a==b,相等");                break;            default:                break;        }


        字符串替换

查找子字符串   rangeOfString:   laction字符串首次出现位置 length长度

替换字符串  stringByReplacingOccurrencesOfString

去除首尾空格   stringByTrimmingCharactersInSet



        NSString *str1=@"<strong>你 好* 世界  </strong>";                //从》开始加1 也就是你开始        NSUInteger loc=[str1 rangeOfString:@">"].location+1;                NSUInteger len=[str1 rangeOfString:@"</"].location-loc;                //动态传区间        NSRange range={loc,len};                NSString *str1New=[str1 substringWithRange:range];        NSLog(@"str1New=%@\n",str1New);                NSString *str11=[str1New stringByReplacingOccurrencesOfString:@"*" withString:@"/"];        NSString *str22=[str11 stringByReplacingOccurrencesOfString:@" " withString:@""];        NSLog(@"%@\n",str22);        //将*替换成/ 空格去掉        NSString *str=@"http:**   ww w. baidu. com";                //stringByReplacingOccurrencesOfString 替换字符串        NSString *strNew=[str stringByReplacingOccurrencesOfString:@"*" withString:@"/"];        NSLog(@"strNew=%@",strNew);                strNew=[strNew stringByReplacingOccurrencesOfString:@" " withString:@""];                 NSLog(@"strNew=%@",strNew);                        NSString *str4=@" hellow word ";        //去除首尾空格        str4=  [str4 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];        NSLog(@"str4=%@",str4);


可变字符串

追加 appendFormat

  子字符串rangeOfString:@""options:0后跟条件

range.location!=NSNotFound  表示查找不到



//创建可变字符串        NSMutableString *str=[NSMutableString string];                NSMutableString *str1=[NSMutableString stringWithFormat:@"asdasd"];                [str1 appendFormat:@"123"];                NSString *str3=@"000000";        [str1 appendString:str3];        NSLog(@"str1=%@",str1);                    NSString *str5=@"hello";        //此时options:<#(NSStringCompareOptions)#> 括号内可以设置为0 0表示参数不起作用 因为是位枚举 才可以这么用        NSRange range= [str5 rangeOfString:@"o" options:0 range:NSMakeRange(0, 5)];                if (range.location!=NSNotFound) {                        NSLog(@"%lu%\n%lu",range.location,range.length);        }else        {                    NSLog(@"no");                }

oc与c字符串装换
c---oc    NSStringstringWithUTF8String:s1
oc----c    ss1UTF8String   接收用const类型
 char *s1="asdasd";       // NSLog(@"%@",s1); //错误用法        //装换        NSString *s2=[NSString stringWithUTF8String:s1];        NSLog(@"%@",s2);                        NSString *ss1=@"asdasdasd";       const char *ss2=[ss1 UTF8String]; //此处用const        printf("%s",ss2);


block遍历数组

       

//使用block遍历数组元素   obj 数组元素  idx 元素下标   *stop是否停止       // [array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {       }];                [array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)        {                        if (idx==2)            {                                *stop=YES;                            }else            {                NSLog(@"idx=%ld,obj=%@",idx,obj);                            <span style="font-family: Arial, Helvetica, sans-serif; text-align: center;"> }];</span>


Array增删改查

         NSMutableArray *arr1=[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];        // 增加元素        [arr1 addObject:@"222"];        NSLog(@"arr1=%@",arr1);        //根据数组元素下标增加        [arr1 insertObject:@"333" atIndex:2];         NSLog(@"arr1=%@",arr1);                //修改        [arr1 replaceObjectAtIndex:3 withObject:@"444"];        NSLog(@"arr1=%@",arr1);                // 删除        // 根据元素下标删除        [arr1 removeObjectAtIndex:3];        NSLog(@"arr1=%@",arr1);        // 根据元素内容删除        [arr1 removeObject:@"4"];        NSLog(@"arr1=%@",arr1);        //全部删除//        [arr1 removeAllObjects];//         NSLog(@"arr1=%@",arr1);        //查找        if ([arr1 containsObject:@"333"]) {                        NSLog(@"查找成功!");        }        // 替换exchangeObjectAtIndex        [arr1 exchangeObjectAtIndex:1 withObjectAtIndex:3];        NSLog(@"arr1=%@",arr1);

字典增删改查

   //可变字典才可以改xxxx//        NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"1":@"!!!",@"2":@"222", nil];        NSMutableDictionary *dict=[NSMutableDictionary dictionary];        //增加        [dict setValue:@"333" forKey:@"3"];        [dict setValue:@"312" forKey:@"2"];        [dict setValue:@"3da3" forKey:@"1"];         NSLog(@"%@",dict);        //修改        [dict setObject:@"222" forKey:@"3"];        NSLog(@"%@",dict);        // 查找 allKeys显示所有k        NSArray *arr= [dict allKeys];        if ([arr containsObject:@"1"]) {            NSLog(@"已找到");        }        

常用结构体
        CGPoint c1;        c1.x=100;        c1.y=200;                CGPoint c2={10,10};                CGPoint c3=CGPointMake(10, 20);                                        CGSize s1;        s1.width=200;        s1.height=100;                CGSize s2=CGSizeMake(200, 100);                                CGRect r1;        r1.origin.x=200;        r1.origin.y=100;        r1.size.width=400;        r1.size.height=200;                        CGRect r2={{200,100},{400,200}};        NSLog(@"%@",NSStringFromRect(r2));                CGRect r3=CGRectMake(200, 100, 400, 200);         NSLog(@"%@",NSStringFromRect(r3));



0 0
原创粉丝点击