OC第六天(原文章误删)(NSString NSArray NSMutableString NSMutableArray )

今天开始正式的接触OC的封装对象,接触了NSString NSArray NSMutableString NSMutableArray 这四个常用对象,也要根据这个对象学会查看

苹果的官方API(话说全英文的看的好辛苦.....)

一 NSString

这个对象,我觉得它比不上java中的String型,居然不能实现字符串的相加 和" = " 赋值,好吧 既然不行那么就看看几个常用的方法,看怎么实现这些功能的

1 初始化方法

– initWithString: – initWithFormat:+ stringWithString: + stringWithFormat:

这没什么好说的,只是注意的是"+方法都是autorelease的 不用手动release;

2 连接字符串

– stringByAppendingFormat:

– stringByAppendingString:

NSString *str1 = [NSString stringWithFormat:@"123"];
NSString *str2 = [NSString stringWithFormat:@"kfs"];
NSString *str3 = [str1 stringByAppendingString:str2];
NSString *str4 = [str1 stringByAppendingFormat:@"%i",100];
NSLog(@"%@,%@",str3,str4);//输出是 123kfs,123100

需要注意的是 这个方法是会产生新对象的,即不会在原字符串上修改

3剪切字符串

  • – substringFromIndex:
  • – substringWithRange:
  • – substringToIndex:

substringFromIndex 是从指定的字符号码开始剪切例如 "wps" 从1开始剪切 新生成的就是"ps';(包含你指定序号的那个字符)

substringToIndex 是剪切到指定字符为止 包含截止字符 如 "wps 剪切到第2个 就是"wp";

substringWithRange: 就要引入一个结构体 NSRange 看如下代码

[plain]  view plain copy
  1. NSString *str1 = [NSString stringWithFormat:@"12345"];  
  2. NSRange ns = NSMakeRange(2, 2);  
  3. NSString *str6 = [str1 substringWithRange:ns];  
  4. NSLog(@"%@",str6);  

NSRange 包含两个变量 第一个指定从哪个字符标号开始,第二个指定到多少后结束(经测试,第二个数字加第一个数字一定要小于整个字符串长度,

即不能剪切大于此字符串的长度的子字符串);

4 替换字符串

– stringByReplacingOccurrencesOfString:withString:

[plain]  view plain copy
  1. NSString *str1 = [NSString stringWithFormat:@"12345"];  
  2. NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"23" withString:@"45"];  
  3. NSLog(@"%@",str2);//输出14545  
由此可见 第一个参数是要被替换的字符串,第二个是替换的字符串;(经测试如果未查询到被替换的字符串,字符串将不会改变)


5 比较字符串

– compare:

– hasPrefix:

– hasSuffix:

– isEqualToString:

– hash

compare : 不等-1,相等为0

[plain]  view plain copy
  1. NSString *str1 = [NSString stringWithFormat:@"12345"];  
  2. NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"23" withString:@"45ds"];  
  3. BOOL a = [str1 compare:str2];  
  4. NSLog(@"%d",a);//返回-1  

-(BOOL)hasPrefix:nsstring 测试字符串是否以 nsstring 开始
-(BOOL)hasSuffix:nsstring 测试字符串是否以 nsstring 结尾
[plain]  view plain copy
  1. NSString *str1 = [NSString stringWithFormat:@"12345"];  
  2. NSString *str2 = [NSString stringWithFormat:@"1545"];  
  3. BOOL a = [str1 hasPrefix:@"18"];  
  4. BOOL b = [str2 hasSuffix:@"45"];  
  5. NSLog(@"%d,%d",a,b);//输出0,1  
– isEqualToString:
[plain]  view plain copy
  1. NSString *str1 = [NSString stringWithFormat:@"12345"];  
  2. NSString *str2 = [NSString stringWithFormat:@"1545"];  
  3. BOOL a = [str1 isEqualToString:str2];  
  4. NSLog(@"%d",a);  

-hash
[plain]  view plain copy
  1. NSString *str1 = [NSString stringWithFormat:@"12345"];  
  2. NSUInteger a = [str1 hash];  
  3. NSLog(@"%lu",a);  

查看字符串的hash码

5 字符串转换

  • – doubleValue
  • – floatValue
  • – intValue
  • – integerValue
  • – longLongValue
  • – boolValue
以上就是将字符串转换为相应的类型 .

二 NSMutableString

继承于NSString 是一个可变字符串,就是说,可以在对象本身(self)上修改了

  1初始化方法

  • + stringWithCapacity:
  • – initWithCapacity:
这个注意的只有两点,1 注意开始一定要赋值 2,用init方法注意回收内存
  2增,删,查,改
  • – appendFormat:
  • – appendString:
  • – deleteCharactersInRange:
  • – insertString:atIndex:
  • – replaceCharactersInRange:withString:
  • – setString:
[plain]  view plain copy
  1. NSMutableString *mString= [NSMutableString stringWithCapacity:10];  
  2. NSString *ns = @"kkk";  
  3. [mString appendFormat:@"aaa"];  
  4. NSLog(@"%@",mString);  
  5. [mString appendString:ns];  
  6. NSLog(@"%@",mString);  
  7. [mString deleteCharactersInRange:NSMakeRange(3, 2)];  
  8. NSLog(@"%@",mString);  
  9. [mString insertString:@"bbb" atIndex:2];  
  10. NSLog(@"%@",mString);  
  11. [mString replaceCharactersInRange:NSMakeRange(2, 2) withString:@"ccc"];  
  12. NSLog(@"%@",mString);  
  13. [mString setString:@"123"];  
  14. NSLog(@"%@",mString);  
//输出
2013-08-08 08:53:34.509 OurClass[443:303] aaa
2013-08-08 08:53:34.511 OurClass[443:303] aaakkk
2013-08-08 08:53:34.512 OurClass[443:303] aaak
2013-08-08 08:53:34.512 OurClass[443:303] aabbbak
2013-08-08 08:53:34.512 OurClass[443:303] aacccbak
2013-08-08 08:53:34.513 OurClass[443:303] 123

三 NSArray 

NSArray,我开始以为它就是java中的list,可是发现在对数据的操作上 它比java弱爆了,只能存储对象,基本数据不能放,对象存储后不能在原array 上改变,删除,增加;不过相比java它在另一个方面却是做的很好.

1 创建 初始化方法

  • + array
  • + arrayWithArray:
  • + arrayWithContentsOfFile:
  • + arrayWithContentsOfURL:
  • + arrayWithObject:
  • + arrayWithObjects:
  • + arrayWithObjects:count:
  • – initWithArray:
  • – initWithArray:copyItems:
  • – initWithContentsOfFile:
  • – initWithContentsOfURL:
  • – initWithObjects:
  • – initWithObjects:count:
没什么可说的 记住
  • + arrayWithObject:
  • + arrayWithObjects:
这两个 ,还有的关于file url 的 那些以后也会常用

2 查询

  • – containsObject:
  • – count
  • – getObjects:range:
  • – lastObject
  • – objectAtIndex:
containsObject 是是否测试包含某个对象
count 是查询总共有多少对象
 – getObjects:range: 获取查询在这个range的对象
– lastObject  : 获取最后一个对象
 3 获取指定对象
  • – indexOfObject:
  • – indexOfObject:inRange:
就是根据指定的下标获取对象
 4 指定对象使用方法

– makeObjectsPerformSelector:

这个就是OC中我觉得Array比java中的list好的一点
像java 如果我们想让一个list里面的对象调用方法,只能写循环了
oc里就能用makeObjectsPerformSelector 直接调用 需要注意的是这个方法必须是
array的对象所具有的
[plain]  view plain copy
  1. NSArray *nsa = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];  
  2. [nsa makeObjectsPerformSelector:@selector(length)];  

5 对比
  • – firstObjectCommonWithArray:
  • – isEqualToArray:
第一个是比较第一个对象的相同
第二个是比较整个array的相同与否

四 NSMutableArray
可变数组 这个就和java中的list很像了 可以自由的增删改

Creating and Initializing a Mutable Array

  • + arrayWithCapacity:
  • – initWithCapacity:

Adding Objects

  • – addObject:
  • – addObjectsFromArray:
  • – insertObject:atIndex:
  • – insertObjects:atIndexes:

Removing Objects

  • – removeAllObjects
  • – removeLastObject
  • – removeObject:
  • – removeObject:inRange:
  • – removeObjectAtIndex:
  • – removeObjectsAtIndexes:
  • – removeObjectIdenticalTo:
  • – removeObjectIdenticalTo:inRange:
  • – removeObjectsInArray:
  • – removeObjectsInRange:

Replacing Objects

  • – replaceObjectAtIndex:withObject:
  • – setObject:atIndexedSubscript:
  • – replaceObjectsAtIndexes:withObjects:
  • – replaceObjectsInRange:withObjectsFromArray:range:
  • – replaceObjectsInRange:withObjectsFromArray:
  • – setArray:

Filtering Content

  • – filterUsingPredicate:

Rearranging Content

  • – exchangeObjectAtIndex:withObjectAtIndex:
  • – sortUsingDescriptors:
  • – sortUsingComparator:
  • – sortWithOptions:usingComparator:
  • – sortUsingFunction:context:
  • – sortUsingSelector:
没时间了.....上面的方法几个常用的是
  • + arrayWithCapacity:
  • – addObject:
  • – insertObject:atIndex:
  • – removeAllObjects
  • – removeLastObject
  • – removeObject:
  • – replaceObjectAtIndex:withObject:
就是类似的增删改 这些都是在自身上修改的.

这就是昨天所学的了,基本上都是一些用法,和java有区别 但是实际上的各种需求也都实现了,
面向对象啊.好好看API
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值