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

Foundation库的内容不可谓不多,就算很精简的说篇幅也受不了啊!笨猫一向反对博客文章一下子拖拖拉拉写一大坨!KISS哦!so将上一篇文章再分一篇来说,于是有了这篇,可能还会有(3)哦...

我发现NSString有这样一个类方法蛮有趣的:stringWithContentsOfURL.看名称就知道是直接将get网页的内容,放在字符串这个类而不是以网络扩充的方式添加这个方法还是很有意思哦:

#import<Foundation/Foundation.h>

int main(int argc, char *argv[]){
	@autoreleasepool {
		//NSString *str_url = @"http://www.baidu.com";
		NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
		NSMutableString *str_url_get;

		str_url_get = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding \
			error:NULL];
		NSLog(@"get : %@\n",str_url_get);
	}
	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 18:34:41.008 f[6877] get : <!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv=
"content-type" content="text/html;charset=utf-8"><link rel="dns-prefetch" href="//s1.bdstatic.com"/>
<link rel="dns-prefetch" href="//t1.baidu.com"/><link rel="dns-prefetch" href="//t2.baidu.com"/>
<link rel="dns-prefetch" href="//t3.baidu.com"/><link rel="dns-prefetch" href="//t10.baidu.com"/>
<link rel="dns-prefetch" href="//t11.baidu.com"/><link rel="dns-prefetch" href="//t12.baidu.com"/>
<title>百度一下,你就知道</title><style >html,body{height:100%}html{overflow-y:auto}#wrapper{
position:relative;_position:;min-height:100%}#content{padding-bottom:100px;text-align:center}
#ftCon{height:100px;position:absolute;bottom:44px;text-align:center;width:100%;margin:0
auto;z-index:0;overflow:hidden}#ftConw{width:720px;margin:0 auto}body{font:12px arial;text-align:
;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}
td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}.bg{background-image:url
(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_e540198d.png);background-repeat:
no-repeat;_background-image:url(http://s1.bdstatic.com/r/www/cache/static/global/img/icons_
d2618985.gif)}.bg_tuiguang_browser{width:16px;height:16px;background-position:-600px 0;display:
inline-block;vertical-align:text-bottom;font-style:normal;overflow:hidden;margin-right:5px}.

接下来聊聊Foundation库(以下均简称为F库)中的数组类,F库中的数组也分可变和不可变的,分别对应的类为NSArray和NSMutableArray类,后者是前者的子类.对于添加到可变数组对象的项必须都是对象而不能是C型的变量哦,这就要求我们"打包"了啊:

#import<Foundation/Foundation.h>

int main(int argc, char *argv[]){
	@autoreleasepool {
		NSMutableArray *ary = [NSMutableArray array];
		NSNumber *num;

		for(int i = 0;i < 10;++i){
			num = [NSNumber numberWithInteger:i];
			[ary addObject: num];
		}

		NSLog(@"show whole array:");
		NSLog(@"%@",ary);
	}
	return 0;
}

我们还可以把数字存入文件中,或者从文件中导入数组,下面只写了存入文件的代码:

		if(![ary writeToFile:@"/home/wisy/src/objc_src/data.db" atomically:NO])
			NSLog(@"write ary to file failed!");

我们知道在F库中有一些常见的类型,他们其实是结构,而结构变量是不能直接放在NSArry数组里的,这就要求打包和解包(wrapping和unwrapping),NSValue类可以完成此项任务:

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

#import <Foundation/Foundation.h>
//#import <CGGeometry.h>

//typedef float CGFloat;

typedef struct CGPoint{
	CGFloat x;
	CGFloat y;
}CGPoint;

int main(int argc, char *argv[]){
	@autoreleasepool {
		NSMutableArray *ary = [NSMutableArray array];
		NSNumber *num;

		for(int i = 0;i < 10;++i){
			num = [NSNumber numberWithInteger:i];
			[ary addObject: num];
		}

		NSLog(@"show whole array:");
		NSLog(@"%@",ary);

		if(![ary writeToFile:@"/home/wisy/src/objc_src/data.db" atomically:NO])
			NSLog(@"write ary to file failed!");

		CGPoint p = {.x = 100,.y = 200};
		NSValue *pobj;
		pobj = [NSValue valueWithPoint:p];
		[ary addObject:pobj];

	}
	return 0;
}

以上代码无法编译通过,我的linux上找不到CGGeometry.h这个头文件啊!虽然我定义了struct CGPoint但似乎没用啊:

f.m:29:34: error: sending 'CGPoint' (aka 'struct CGPoint') to parameter of
      incompatible type 'NSPoint' (aka 'struct _NSPoint')
                pobj = [NSValue valueWithPoint:p];
                                               ^
/usr/include/GNUstep/Foundation/NSValue.h:73:39: note: passing argument to
      parameter 'point' here
+ (NSValue*) valueWithPoint: (NSPoint)point;

Wait!怎么是NSPoint!?书上不是说CGPoint结构吗?抱着试一下的心态改为前者,编译通过,运行结果如下:

NSPoint p = {.x = 100,.y = 200};
NSValue *pobj;
pobj = [NSValue valueWithPoint:p];
[ary addObject:pobj];

NSLog(@"========================\n%@",ary);
2014-07-01 21:21:17.301 f[7802] ========================
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "{x = 100; y = 200}")

哦也!F库中还有字典和集合2中类,也分为可变和不可变的.方法大同小异,这里就不一一写代码了,否则又变的一坨坨长长的了obj-c编程10:Foundation库中类的使用(2)[字符串,数组]

上一篇:Visual Studio 2015的坑:中文字符串编译后成乱码


下一篇:DBUtils学习一 增删该查