不可变字符串NSString
1. 利用系统的类 初始化一个对象
-
创建一个NSString对象, 调用其自定义初始化方法, 并对其进行初始化
NSString *str = [[NSString alloc] initWithString: @"123"];
相当于<code class="hljs rust has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">NSString *<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span> = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"123"</span>; NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">str</span>); </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
打印结果为123
-
用系统的便利构造器方法初始化对象 用格式化初始方法
NSString *str2 = [NSString stringWithFormat:@"aaa%@", str];
NSLog(@"%@", str2);
打印结果为aaa123
2. 利用创建的对象 调用系统的方法
先定义一个字符串对象如下:
NSString *str = @“hello world”;
下面介绍一些NSString类中常用的一些方法及解释
1.获得字符串长度
-
@property (readonly) NSUInteger length;
length是NSString类的属性, 它保存着字符串对象的长度.
可以用点语法获得, 也可以调用get方法获得它的值.<code class="hljs go has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">NSUInteger <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">len</span> = [str length]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 用get方法获得字符串对象的长度</span> NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%lu"</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">len</span>);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
2. 获取子字符串(字符串截取)
注: 字符串中的每个字符都有下标, 下标从0开始, 从左往右第一个字符的下标是0, 最后一个字符的下标是长度-1
-
- (NSString *)substringFromIndex:(NSUInteger)from;
解释: 从下标from开始(包含下标from所在的字符), 截取到最后
用法如下:<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *strFrom = [str substringFromIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>]<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, str1);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li></ul>
结果为world
-
- (NSString *)substringToIndex:(NSUInteger)to;
解释: 从第一个字符开始, 截取到下表为to的位置(不包含下标to所在的字符)
用法如下:<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *strTo = [str substringToIndex:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>];<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, strTo);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li></ul>
结果为hello
-
- (NSString *)substringWithRange:(NSRange)range;
解释: 从指定的范围range中截取一段字符-
p.s. NSRange的定义如下
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">typedefstruct _<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span> { NSUInteger location; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 表示范围的开始 </span> NSUInteger length; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 表示范围的长度 </span> } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 系统创建NSRange变量的方法</span> NS_INLINE <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span> NSMakeRange(NSUInteger loc, NSUInteger len) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span> r; r<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.length</span> = len; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> r; }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">9</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">10</span></li></ul>
可以通过
NSRange range = {2, 2};
或者NSRange range = NSMakeRange(2, 2);
来创建并初始化NSRange对象
第一个参数是开始的地点, 第二个参数是长度
range 表示下表从2开始, 长度为2的范围.
用法如下:<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span> range = NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str1 = [str substringWithRange:range]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, str1);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
结果为ll
-
-
- (NSArray *)componentsSeparatedByString:(NSString *)separator;
解释:将一个字符串作为分隔符, 遇到该分隔符就将字符串截为分隔符前, 和分隔符后的两段. 且两段字符串均不包含分隔符. 截取结果保存在数组里.
用法如下:
截取字符串“20|http://www.baidu.com”中 “|” 前⾯和后⾯的字符串<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">NSString *<span class="hljs-type" style="box-sizing: border-box;">string</span> = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"20|http://www.baidu.com"</span>; NSArray *<span class="hljs-constant" style="box-sizing: border-box;">result</span> = [<span class="hljs-type" style="box-sizing: border-box;">string</span> componentsSeparatedByString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"|"</span>]; NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, <span class="hljs-constant" style="box-sizing: border-box;">result</span>);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
结果为:
(
20,
“http://www.baidu.com”
)
3.拼接字符串
-
- (NSString *)stringByAppendingString:(NSString *)aString;
解释: 将字符串aString拼接到当前字符串的结尾
用法如下:
<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">NSString *<span class="hljs-type" style="box-sizing: border-box;">string</span> = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"!!"</span>; NSString *<span class="hljs-constant" style="box-sizing: border-box;">result</span> = [str1 stringByAppendingString:<span class="hljs-type" style="box-sizing: border-box;">string</span>]; NSLog(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, <span class="hljs-constant" style="box-sizing: border-box;">result</span>);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
结果为hello world!!
或者:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *result = [str1 stringByAppendingString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"!!"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, result);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
结果也是hello world!!
-
-(NSString*)stringByAppendingFormat:(NSString*)format, ...NS_FORMAT_FUNCTION(1,2);
解释:格式化拼接字符串
用法如下:<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *newString = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"aa"</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *result = [str1 stringByAppendingFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, newString]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, result);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
结果为:hello worldaa
或者:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *result = [str1 stringByAppendingFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"aa"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, result);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
4.替换字符串
-
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0);
解释: 给一个目标字符串和一个目的字符串, 在本字符串中搜索, 出现目标字符串相同的字符串, 就替换成目的字符串
用法如下:<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *result = [str1 stringByReplacingOccurrencesOfString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"he"</span> withString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"eh"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, result);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
结果为:ehllo world
-
- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0);
解释: 按范围替换字符串
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span> range = NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *result = [str1 stringByReplacingCharactersInRange:range withString:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"1"</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, result);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
结果为:he1o world
5.字符串相等
-
- (BOOL)isEqualToString:(NSString *)aString;
解释: 判断两个字符串是否相同 如果相同返回1, 不相同返回0
用法如下:<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str1 = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Hello World"</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *str2 = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Hello world"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([str1 isEqualToString: str2]) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"相同"</span>); } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"不相同"</span>); }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">7</span></li></ul>
结果是: 不相同
6.字符串比较
-
- (NSComparisonResult)compare:(NSString *)string;
解释如下:用本字符串和目标字符串string进行比较 如果两个字符串相同, 返回0, 本字符串比目标字符串小则返回-1, 反之返回1-
NSComparisonResult的定义如下
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> NS_ENUM(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSInteger</span>, NSComparisonResult) { NSOrderedAscending = -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>L, NSOrderedSame, NSOrderedDescending };</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li></ul>
NS_ENUM是OC中的枚举方法.括号中的两个参数NSInteger和NSComparisonResult分别表示枚举方法中数据的类型, 和枚举方法的方法名
-
7.判断包含某个前缀(后缀)
- (BOOL)hasPrefix:(NSString *)aString;
功能: 判断字符串是否带有前缀aString
解释: 从本字符串和目标字符串两者各自的第一个字符同时开始进行遍历,并一一比较, 直到目标字符串遍历完毕. 若没有出现不同的字符, 则返回1, 出现了不同字符, 返回0
- (BOOL)hasSuffix:(NSString *)aString;
功能: 判断字符串是否含有后缀aString
解释: 从本字符串和目标字符串的最后一个字符开始从后往前进行遍历,并一一比较, 直到目标字符串遍历完毕. 若没有出现不同的字符, 则返回1, 出现了不同字符, 返回0
8.字符串字母大小写转换
- 转换为大写
@property (readonly, copy) NSString *uppercaseString;
功能: 将字符串所有字母转化为大写
- 转换为小写
@property (readonly, copy) NSString *lowercaseString;
功能: 将字符串所有字母转化为小写
- 每个单词的首字母变大写 除首字母外的字符小写
@property (readonly, copy) NSString *capitalizedString;
功能: 自动识别字符串中的每个单词, 并把每个单词的首字母大写, 其余字母小写
9.不可变字符串如果重新赋值的话, 改变的不是其本身 而是进行了重指向 指向了一个新的字符串
可变字符串NSMutableString
可变字符串NSMutableString是继承于NSString的一个类
但是可变字符串不能使用定义声明方式如下
NSMutableString *str = @"123";
因为
NSString *str1 = @"123";
相当于字符串对象str 指向了常量区中的字符串 @”123”
str1是不可变字符串的对象, 所以指向一个不可变的数据没有问题.
但是 由于可变字符串NSMutableString 是可变的
若是让其指向 常量区中的常字符串 @“123” 结果就是可变字符串对象str指向了一个不可变的数值
这显然不符合可变字符串的定义
所以OC禁止可变字符串使用这种便利方式初始化字符串
但是NSMutableSring依然可以使用继承自NSString的字符串方法如下:
NSMutableString *str1 = [[NSMutableString alloc] initWithString:@"sss"];
也可以调用自身的便利构造器方法:
NSMutableString *str = [NSMutableString stringWithString:@"sgr"];
可变字符串的常用方法:
1.拼接字符串
<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">// 在字符串的末尾添加字符串aString - (void)appendString:(NSString *)aString; // 在字符串的末尾格式化添加字符串 - (void)appendFormat:(NSString *)format, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> NS_FORMAT_FUNCTION(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>); </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">6</span></li></ul>
2.插入字符
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;
在字符串的下标为loc处插入字符串aString 原来loc+1处的开始的字符串自动添加到插入的字符串aString后面.
3.替换字符
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;
在指定范围range内的字符串全部移除, 将字符串aString自动加入被移除的位置, 断处自动相连.
4.删除字符
- (void)deleteCharactersInRange:(NSRange)range;
将范围range内的全部字符删除, 剩余的字符串自动按顺序连接起来
注意的是可变字符串的方法返回值都是void,它改变的都是自身保存的字符串 所以不需要返回一个新的字符串
不可变数组NSArray
NSArray 只能存对象 为了操作方便 一般也是存同一类型的对象
数组也称为一种容器
常用的初始化方法:
<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">+ (instancetype)array; + (instancetype)arrayWithObjects:(id)firstObj, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> NS_REQUIRES_NIL_TERMINATION; + (instancetype)arrayWithArray:(NSArray *)array; - (instancetype)initWithObjects:(id)firstObj, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">...</span> NS_REQUIRES_NIL_TERMINATION; - (instancetype)initWithArray:(NSArray *)array; // 读取一个文件, 数据保存在一个数组里并返回 + (NSArray *)arrayWithContentsOfFile:(NSString *)path; // 从一个网址获得结果, 数据保存在数组里并返回 + (NSArray *)arrayWithContentsOfURL:(NSURL *)url;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">9</span></li></ul>
创建一个不可变数组的对象:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *array = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> alloc] initWithObjects:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"str1"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"str2"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"str3"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *array2 = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> arrayWithObjects:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"str1"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"str2"</span>, @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"str3"</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@\n%@"</span>,array, array2);</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
语法糖 快速构建数组的方法
NSArray *array3 = @[@"str1", @"str2", @"str3"];
打印数组元素的方法
可以通过NSLog(@"%@", array);
打印出array数组中的所有元素 但是不能输出中文. 因为NSArray 中的description不支持输出中文, 但是会输出中文的unicode编码
也可以通过下标打印一个元素
NSLog(@"%@", array2[0]);
// 打印出array2数组的第一个元素 此时第一个元素中是一个字符串对象, NSString 字符串中的description是支持打印出中文的
获得数组元素的首元素和最后一个元素
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> firstObject NS_AVAILABLE(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>_6, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>_0); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">readonly</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> lastObject;</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
切记 若要对一个不可变数组在不创建另一个不可变数组的情况下直接进行增删改查是做不到的
可变数组NSMutableArray
常用初始化方法:
+ (instancetype)array;
值得注意的是, 所有可变数组的操作. 都必须在可变数组初始化以后才可以进行.
常用其他方法
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)addObject:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>)anObject; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 往数组内添加元素的方法</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 插入元素的方法, 插入后原来此位置的元素自动后移</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)insertObject:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>)anObject atIndex:(NSUInteger)index; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 删除指定元素的方法, 删除下标所在的元素, 删除后, 后面的元素自动前移</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)removeObjectAtIndex:(NSUInteger)index; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 将不可变数组的全部元素添加到可变数组中</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)addObjectsFromArray:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSArray</span> *)otherArray; - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)removeAllObjects; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 删除数组中所有的元素(清空)</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">8</span></li></ul>
数值对象类NSNumber
初始化方法:
<code class="hljs erlang has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-pp" style="box-sizing: border-box;"><span style="font-size:18px;">- <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSNumber</span> *)</span>initWithChar:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(char)</span>value NS_DESIGNATED_INITIALIZER; + <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(<span class="hljs-variable" style="box-sizing: border-box;">NSNumber</span> *)</span>numberWithChar:<span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(char)</span>value;</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li></ul>
NSNumber 的作用是将基本类型的变量转变成对象, 方便存放在OC的数组里面
它支持所有的基本数据类型, 所有基本数据类型的变量都可以和NSNumber互相转化
另外, 利用这一点, 也可以完成将基本数据类型之间互相转换.
比如:
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:18px;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> a = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5.6</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *num = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithFloat:a]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> b = [num intValue];</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:18px;">3</span></li></ul>
当然 通常情况下这种功能并没有什么卵用.
NSValue
支持将结构体变量转化成对象类型.
利用NSNumber 和 NSValue 将基本数据类型保存到NSMutableArray中并遍历数组, 获得结果
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background-image: initial; background-attachment: initial; background-color: transparent; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="font-size:18px;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> num1 = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">float</span> num2 = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.8</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">double</span> num3 = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4.0</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">BOOL</span> num4 = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSRange</span> range = NSMakeRange(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 将基本数据类型转化成NSNumber对象</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *intValue = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithInt:num1]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *floatValue = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithFloat:num2]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *doubleValue = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithDouble:num3]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *boolValue = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithBool:num4]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 将结构体变量转化成NSValue对象</span> NSValue *rangeValue = [NSValue valueWithRange:range]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> *array = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> array]; [array addObject:intValue]; [array addObject:floatValue]; [array addObject:doubleValue]; [array addObject:boolValue]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 由于数组中可以保存一切对象, 所以可以将NSNumber 和NSValue对象一起保存在数组中</span> [array addObject:rangeValue]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 遍历数组</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < [array count]; i++) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@"</span>, array[i]); }</span></code><ul class="pre-numbering" style="font-size: 14px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li></ul>