obj-c编程10:Foundation库中类的使用(1)[数字,字符串]

    我们知道在mac或iphone上编程最终逃不开os x平台,你无法在windows或linux上开发纯正的apple程序.(so不要舍不得银子买mac啦)虽说linux和windows上有移植的obj-c编译器,但是平台开发框架还是在mac上啊.比如cocoa框架包括Foundation框架,Application Kit框架和Core Data的第三方框架;二cocoa Touch指的则是Foundation,Core Data以及UIKit框架.对于Foundation框架中各个类的使用,可以到apple开发者网站在线查询:https://developer.apple.com/library/mac/navigation/

    接下来我们不按顺序,专挑难点,奇怪点和好玩点(如果有的话)的类来看看,直接用代码说话喽obj-c编程10:Foundation库中类的使用(1)[数字,字符串]

#import<Foundation/Foundation.h>

int main(int argc, char *argv[]){
	@autoreleasepool {
		NSNumber *n;
		NSInteger integer;	//not a class just a typedef

		n = [NSNumber numberWithInteger: 101];
		integer = [n integerValue];
		NSLog(@"%li",(long)integer);

		NSNumber *n1 = [[NSNumber alloc] initWithLong:0x12345678];
		[n1 initWithLong :0xabcd];	//can't change org value 0x12345678!

		integer = [n1 longValue];
		NSLog(@"%lx",(long)integer);

		NSString *str = @"hello apple";
		NSLog(@"str is : %@",str);
		NSLog(@"num is : %@",n);

		NSLog(@"Hello World!");
	}
	return 0;
}

注意代码中%@打印NSNumber型变量的行了吗?有人可能会问这是怎么实现的,我开始也以为%@只能格式化显示NSString类型啊.其实只要类中定义了description方法,就可以以自定义格式显示任何对象的内容啦.这个我们来写个简单类试一下:

#import<Foundation/Foundation.h>

@interface A:NSObject{
	int i;
}
	-(id)init:(int)i_v;
@end

@implementation A
	-(id)init:(int)i_v{
		self = [super init];
		if(self){
			i = i_v;
		}
		return self;
	}

	-(NSString*)description{
		return [NSString stringWithFormat:@"#i is %d#",i];
	}

@end

int main(int argc, char *argv[]){
	@autoreleasepool {
		A *a = [[A alloc] init:99];
		NSLog(@"a is %@",a);
	}
	return 0;
}


注意description方法的实现中stringWithFormat方法的2个参数分割符哦,不是:号而是逗号(,)哦.执行结果如下

wisy@wisy-ThinkPad-X61:~/src/objc_src$ clang -O3 -g0 $OBJ_C_OPT -lobjc -lgnustep-base -o f f.m
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f
2014-07-01 12:42:59.380 f[4179] a is #i is 99#

Foundation中的字符串操作看起就显得那么蛋疼,简单的东西给搞的蛮复杂的,尤其是方法名,貌似还驼峰状,我呵呵了.字符串类分为可变和不可变两种,前者类位NSString,后者类为NSMutableString.顾名思义,不可变字符串不可以修改自身,只能返回一个修改后的新字符串,而可变字符串可以修改自身,包括删除subString,拼接啊,替换啊,皆可,下面上代码:

#import<Foundation/Foundation.h>

int main(int argc, char *argv[]){
	@autoreleasepool {
		NSString *str_no_m = @"hello world";
		NSMutableString *str_m;
		NSRange substr;	//just a struct

		str_m = [NSMutableString stringWithString: str_no_m];
		NSLog(@"%@",str_m);

		[str_m insertString: @" xxx" atIndex: 5];
		NSLog(@"%@",str_m);

		[str_m appendString: @" not_fix!"];
		NSLog(@"%@",str_m);

		[str_m deleteCharactersInRange: NSMakeRange(6,4)];	//(index,len)
		NSLog(@"%@",str_m);

		substr = NSMakeRange(6,6);
		[str_m deleteCharactersInRange: substr];
		NSLog(@"%@",str_m);

		substr = [str_m rangeOfString: @"_"];
		if(substr.location != NSNotFound){
			[str_m deleteCharactersInRange: substr];
		}
		NSLog(@"%@",str_m);

		[str_m setString: @"new string!"];
		NSLog(@"%@",str_m);

		substr = [str_m rangeOfString: @"new"];
		if(substr.location != NSNotFound){
			[str_m replaceCharactersInRange: substr withString: @"old"];
		}
		NSLog(@"%@",str_m);

		[str_m setString: @"1122334411223344"];
		[str_m replaceOccurrencesOfString:@"2" withString:@"X" \
			options:0 range:NSMakeRange(0,[str_m length])];
		NSLog(@"%@",str_m);
	}
	return 0;
}

编译及运行结果如下:

wisy@wisy-ThinkPad-X61:~/src/objc_src$ clang -O3 -g0 $OBJ_C_OPT -lobjc -lgnustep-base -o f f.m
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f
2014-07-01 14:36:49.380 f[5719] hello world
2014-07-01 14:36:49.382 f[5719] hello xxx world
2014-07-01 14:36:49.382 f[5719] hello xxx world not_fix!
2014-07-01 14:36:49.383 f[5719] hello world not_fix!
2014-07-01 14:36:49.383 f[5719] hello not_fix!
2014-07-01 14:36:49.383 f[5719] hello notfix!
2014-07-01 14:36:49.383 f[5719] new string!
2014-07-01 14:36:49.383 f[5719] old string!
2014-07-01 14:36:49.383 f[5719] 11XX334411XX3344

注意其中的replaceOccurrencesOfString方法,其中的options可能的选项有:

Search and Comparison Options

Several of the search and comparison methods take an “options” argument. This is a bit mask that adds further constraints to the operation. You create the mask by combining the following options (not all options are available for every method):

Search option

Effect

NSCaseInsensitiveSearch

Ignores case distinctions among characters.

NSLiteralSearch

Performs a byte-for-byte comparison. Differing literal sequences (such as composed character sequences) that would otherwise be considered equivalent are considered not to match. Using this option can speed some operations dramatically.

NSBackwardsSearch

Performs searching from the end of the range toward the beginning.

NSAnchoredSearch

Performs searching only on characters at the beginning or, if NSBackwardsSearch is also specified, the end of the range. No match at the beginning or end means nothing is found, even if a matching sequence of characters occurs elsewhere in the string.

NSNumericSearch

When used with the compare:options: methods, groups of numbers are treated as a numeric value for the purpose of comparison. For example,Filename9.txt < Filename20.txt < Filename100.txt.

Search and comparison are currently performed as if the NSLiteralSearch option were specified.

至于我用的值0,我猜是默认选项吧?因为书上值为nil,编译有警告,遂换为0.

上一篇:对于容器环境来说 全栈监控究竟意味着什么?


下一篇:如何在 RHEL 上设置 Linux RAID 1