Objective-C中的NSNumber和NSString
在本教程中,我们将讨论在Objective-C中使用的NSNumber和NSString数据类型。
Foundation Framework定义了几个类,这些类提供其他高级语言中提供的标准的,面向对象的数据结构
NSNumber
我们不能在Foundation类中直接使用int和float等原始数据类型。
为此,将NSNumber用作这些原语的对象包装。
它的主要工作是在框中存储和检索原始值。
只需在相应的原始类型前面加上@符号,即可创建BOOL,char,int和double的NSNumber版本。
但是,未签名的int,long和float必须附加U,L或者F修饰符,如下所示。
NSNumber *aBool = @NO; NSNumber *aChar = @'z'; NSNumber *anInt = @2147483647; NSNumber *aUInt = @4294967295U; NSNumber *aLong = @9223372036854775807L; NSNumber *aFloat = @26.99F; NSNumber *aDouble = @26.99;
可以使用@()语法将任意C表达式装箱,从而使我们可以在NSNumber对象中转换基本的算术运算,如下所示:
double x = 24.0; NSNumber *result = @(x * .15); NSLog(@"%.2f", [result doubleValue]);
在上面的代码中," doubleValue"是一种专用方法,用于从NSNumber类型返回原始数据类型。
创建NSNumber后,就无法更改其关联值NSNumber。
他们是一成不变的。
从这个意义上讲,NSNumber实例的行为完全类似于原始值。
当我们需要一个新的double值时,我们将创建一个新的文字,而不更改现有的文字。
Objective-C中比较数字
通常,Objective-C有两种比较方法:
- 指针比较使用==运算符来查看两个指针是否引用相同的内存地址(即,相同的对象)。
这种比较不可能使不同的对象相等 - 值比较使用" isEqualToNumber:"之类的方法来查看两个对象是否表示相同的值。
这种比较可能使不同的对象相等
下面的代码片段显示了一个比较两个NSNumber的示例:
NSNumber *anInt = @27; NSNumber *sameInt = @27U; //Pointer comparison (fails) if (anInt == sameInt) { NSLog(@"They are the same object"); } //Value comparison (succeeds) if ([anInt isEqualToNumber:sameInt]) { NSLog(@"They are the same value"); }
isEqualToNumber:即使两个值存储在不同的对象中,也可以保证两个值相等。
NSString
像NSNumber一样,NSString也是不可变的类型。
它用于表示Objective-C中的文本。
NSString提供了对Unicode的内置支持,这意味着我们可以直接在字符串文字中包含UTF-8字符。
NSString对象的最基本表示方式如下所示:
NSString *sample = @"iOS Tutorials";
stringWithFormat:
类方法对于生成由变量值组成的字符串很有用。
它采用与NSLog()相同类型的格式字符串。
在一个新的空项目中尝试以下代码段!
NSString *message = [NSString stringWithFormat:@"That's a %@ %@ from %d!", make, model, year]; NSLog(@"%@", message);
Objective-C中比较字符串
字符串比较类似于NSNumber比较,只是使用了" isEqualToString:"方法。
对于部分比较,使用了" hasPrefix:"和" hasSuffix:"。
NSString *name = @"Programming Language"; if ([name isEqualToString:@"Programming Language"]) { NSLog(@"The name string holds the text Programming Language"); } if ([name hasPrefix:@"Programming"]) { NSLog(@"The first name of the word is Programming"); } if ([name hasSuffix:@"Language"]) { NSLog(@"The second name of the word is Language"); }
比较方法用于按字母顺序对字符串进行排序,如下例所示:
NSString *otherName = @"Objective-C"; NSComparisonResult result = [name compare:otherName]; if (result == NSOrderedAscending) { NSLog(@"The letter 'P' comes before 'O'"); } else if (result == NSOrderedSame) { NSLog(@"We're comparing the same string"); } else if (result == NSOrderedDescending) { NSLog(@"The letter 'P' comes after 'O'"); }
在以上代码段中,我们显示了三种返回值类型及其说明,如下所示:
- NSOrderedAscending:接收器<参数
- NSOrderedSame:接收者==参数
- NSOrderedDescending:接收者>参数
组合字符串
NSString *first = @"First Name"; NSString *second = @"Second Name"; NSString *result = [first stringByAppendingString:second]; NSLog(@"%@", result); //First NameSecond Name result = [first stringByAppendingFormat:@" %@", second]; NSLog(@"%@", result); //First Name Second Name (note the space)
Objective-C中搜索字符串
NSString的搜索方法返回一个NSRange结构,该结构由一个位置和一个长度字段组成。
位置是比赛开始的索引,长度是比赛中字符的数量。
如果找不到匹配项,则位置将包含NSNotFound。
NSString *text = @"Maserati GranCabrio"; NSRange searchResult = ; if (searchResult.location == NSNotFound) { NSLog(@"Search string was not found"); } else { NSLog(@"'Cabrio' starts at index %lu and is %lu characters long", searchResult.location, //13 searchResult.length); //6 }
在上面的示例中,我们使用了随机字符串,并且在NSLog中打印了起始索引和匹配的子字符串的长度。
(我们在注释中显示了结果。
您可以在XCode Project中尝试该示例。
NSMutableStrings
NSMutableString类是NSString的可变版本。
与不可变字符串不同,可以更改可变字符串的各个字符而无需创建新对象。
上面讨论的方法也适用于NSMutableString。
尽管某些方法,例如stringByAppendingString:仍会返回NSString对象,而不是NSMutableString。
创建可变字符串
stringWithString用于从文字或者现有的NSString对象创建NSMutableString。
NSMutableString *result = [NSMutableString stringWithString:@"Mutable String Text"];
通过setString:方法,我们可以为实例分配一个新值,如下所示:
[result setString:@"Modified String"];