标签:
1)了解Foundation框架:
Foundation框架它提供了字符串、集合、I/O等很多和其他语言一样的标准库一样的功能。
Foundation本身就是一个巨大的库,这里无法罗列出Foundation提供的每个雷和方法。
2)Foundation框架使用字符串:
NSString 的快捷语法:
1 NSString *someString = @"this is a string";
本质上,编译器一旦遇到@并紧跟着包含在“”之中的字符串就会创建一个静态的包含所提供字符串的NSString对象。
在比较一个字符串和另一个实例时,可以用-isEqual:
1 NSString *string1 = @"this is a string"; 2 NSString *string2 = @"this is a string";//和string1相同 3 NSString *string3 = [NSString stringWithString:string:string1]; 4 5 assert(string 1 == string2);//真 6 assert(string 1 isEqual:string2);//真 7 assert(string 1 == string3);//假
NSLog的格式化字符串:
NSLog(@"The age of the employee named %@ is %ld",[employee name],[employee age]);
使用格式化字符串:
NSString *str; NSString *cardName = @"Ace"; NSString *cardSuit = @"Spades"; str = [NSString stringWithFormat:@"The winning card is %@ of %@.",]cardName,cardSuit; //现在str是The winning card is Aceof Spades. str = [NSString stringwithFormat:@"You have %ld gold",[player goldAmount]]; //现在str是You have 1000 gold str = [NSString stringwithFormat:@"Your change is: $%.2f.",change]; //现在str是Your change is: $2.43.
OC中常见的格式说明符:
int %d
unsigned int %u
long %ld
unsigned long %lu
float %f
NSObject子类实例 %@
NSMutableString(可变字符串)
NSMutableString继承与NSString,所以我们可以使用NSString字符串的创建方式来创建NSMutableString。
NSString和NSMutableString的区别:
NSString是一个常量字符串,里面的值并不能更改,而NSMutableString是一个可变的字符串,字符串的值可以更改。
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) { 4 @autoreleasepool { 5 6 7 NSMutableString *ms1 = [NSMutableString stringWithString:@"Foundation Learing"] ; 8 [ms1 appendString:@" Class"] ; 9 NSLog(@"%@" , ms1) ; 10 11 NSRange range = [ms1 rangeOfString:@" Class"] ; 12 [ms1 deleteCharactersInRange:range] ; 13 NSLog(@"%@" , ms1) ; 14 15 16 } 17 return 0; 18 } 19 20 //Foundation Learing Class 21 //Foundation Learing
标签:
原文地址:http://www.cnblogs.com/yyyyyyyyqs/p/4617524.html