OC对象类型

来源:互联网 发布:web服务器搭建域名 编辑:程序博客网 时间:2024/06/07 00:30

OC中对象类型以及其常用方法

一不可变字符串:NSString

    

1.初始化:initWithString

        NSString *str = [[NSString alloc] initWithString:@"1234assd"];

    

2.遍历构造器:stringWithString

        NSString * str = [NSStringstringWithString:@"1234assd"];

    

3.在字符串后追加字符:stringWithFormat

        str = [NSString stringWithFormat:@"%@xyz",str];


4.获取字符串长度:length

        [str length]


5.判断是否包含前缀(hasPrefix),后缀(hasSuffix)

        [str hasPrefix:@“前缀"]

        [str hasSuffix:@“后缀"]


6.搜索字符串范围:rangeOfString

        [str rangeOfString:@“搜索内容"]

        location表示从哪儿开始搜索,length表示长度


7.截取字符串:

        NSString * str = @"Xu Tian Gang Yi";

       (1).substringToIndex:从头开始截取几个字符;substringFromIndex:从哪儿开始往后截取

            NSLog(@"%@", [str substringToIndex:2]);

            NSLog(@"%@", [[str substringFromIndex:3]substringToIndex:4]);

            NSLog(@"%@", [[str substringFromIndex:8]substringToIndex:4]);

            NSLog(@"%@", [[str substringFromIndex:13]substringToIndex:2]);


        (2).substringWithRange:按区间截取字符串中得内容的

            NSLog(@“%@“, [str substringWithRange:[strrangeOfString:@"Xu"]]);

            NSLog(@"%@", [str substringWithRange:[strrangeOfString:@"Tian"]]);

            NSLog(@"%@", [str substringWithRange:[strrangeOfString:@"Gang"]]);

            NSLog(@"%@", [str substringWithRange:[strrangeOfString:@"Yi"]]);

       

        (3).NSMakeRange(x, y):从下标x 截取到下标y

            NSLog(@“%@“, [str substringWithRange:NSMakeRange(0,2)]);

            NSLog(@"%@", [str substringWithRange:NSMakeRange(3,4)]);

            NSLog(@"%@", [str substringWithRange:NSMakeRange(8,4)]);

            NSLog(@"%@", [str substringWithRange:NSMakeRange(13,2)]);


二可变字符串:NSMutableString


1.拼接:appendString

        [str appendString:”string”];


2.插入:insertString

        [str insertString:”string”atIndex:n];  在位置n 处插入字符串 string。


3.删除:deleteCharactersInRange

        [strdeleteCharactersInRange:string]; 从str中删除string


4.替换:replaceOccurrencesOfString

        [str replaceOccurrencesOfString:str1withString:str2 options:2 range:NSMakeRange(0,str.length)];  将str1 替换为str2


三不可变数组:NSArray


1.初始化:initWithObjects

 NSArray * arr= [[NSArray alloc] initWithObjects:@"Shen Ming Yang", @"Tang ChunXiao", nil];


2.便利构造器:arrayWithObjects

        NSArray * arr2 = [NSArray arrayWithObjects:@"WeiDe Bing", @"Li Pan", nil];


3. 获取元素个数:count

        [arr count]


4.根据索引值获取对象:objectAtIndex

        [arr objectAtIndex:n] 获取下标为 n 的元素


5. 获取对象所在数组中的索引值:indexOfObject

        [arr indexOfObject:string]获取元素string的索引值


三可变数组:NSMutableArray


初始化:arrayWithObjects   遍历构造器:arrayWithObjects

添加:addObject

插入:insertObject

     [mArray insertObject:stringatIndex:n];在下标 n 处插入元素

删除:removeObjectAtIndex                                                                                          [mArrayremoveObjectAtIndex:n];删除下标为 n 的元素


替换:replaceObjectAtIndex 

     [mArrayreplaceObjectAtIndex:nwithObject:string];把下标为n的元素替换为string

交换:exchangeObjectAtIndex

     [mArrayexchangeObjectAtIndex:n withObjectAtIndex:m];交换下标为n和m的元素

四数据对象:NSNumber


初始化:numberWithInt   遍历构造器:initWithObjects


2.  从对象中取值:intValue(数据对象不能直接运算,得先quzhi)。


五不可变字典:NSDictionary


1.  初始化:initWithObjects:    forKeys:     遍历构造器:initWithObjectsAndKeys


2.  获取所有key值:allKeys     获取所有value值:allValues


3.  根据key查询value:objectForKey(私有方法)    valueForKey


六可变字典:NSMutableDictionary


初始化:initWithObjects:   forKeys:


2.  添加键值对:addEntriesFromDictionary  


3.  修改key对应的value值:setValue:       forKey:


4.  删除键值对:removeObjectFoeKey:


七不可变集合:NSSet


初始化:initWithObjects:


2.  获取个数:count


3.  获取集合中的某个元素:member


4.  判断集合中是否包含某个对象:containsObject:


5.  判断两个集合对象是否相等:isEqualToSet:


八可变集合:NSMutableSet


初始化:initWithObjects:  (查文档会发现有很多种遍历构造器)


2.  添加元素:addObject:


3.  删除元素:removeObject:

0 0
原创粉丝点击