OC对象类型

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

一 不可变字符串:NSString

     

1.初始化:initWithString

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

     

2.遍历构造器:stringWithString

          NSString * str = [NSString stringWithString:@"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:[str rangeOfString:@"Xu"]]);

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

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

               NSLog(@"%@", [str substringWithRange:[str rangeOfString:@"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

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


4.替换:replaceOccurrencesOfString

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


三 不可变数组:NSArray


1. 初始化:initWithObjects

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


2. 便利构造器:arrayWithObjects

          NSArray * arr2 = [NSArray arrayWithObjects:@"Wei De 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:string atIndex:n];在下标 n 处插入元素

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


替换:replaceObjectAtIndex 

      [mArray replaceObjectAtIndex:n withObject:string];把下标为n的元素替换为string

交换:exchangeObjectAtIndex

      [mArray exchangeObjectAtIndex: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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值