Category 是Objective-C 里面最常用到的功能之一。简单的讲Category可以为已经存在的类增加方法,而不需要增加一个子类。而且,我们可以在不知道某个类内部实现的情况下,为该类增加方法。如果我们想增加某个框架(framework)中的类的方法,Category就非常有效。比如,今天的内容中,就想在NSDictionary上增加一个方法来直接处理URL远程数据,后面有详细代码。 定义格式如下:
1 #import " 类名.h "
2 @interface 类名(类别名)
3 // 新方法的声明
4 @end
具体操作,如下步骤:
1. 创建项目,项目名称:listCityWeather
2. 添加一个Category类,扩展NSDictionary,主要方便于处理远程的json数据
2.1 修改NSDictionary+Json.h文件
1
//
2 // NSDictionary+Json.h
3 // listCityWeather
4 //
5 // Created by tony on 12-9-5.
6 // Copyright (c) 2012年 chinapcc.com. All rights reserved.
7 //
8
9 #import
10
11 @interface NSDictionary (Json)
12
13
14 // 直把远程的地址上Json数据转,换成Dictionary对象
15 +(NSDictionary*)dictionaryWithContentsOfURLString:(NSString*)urlAddress;
16
17 // 把当前的Dictionary对象,转成Json对象
18 -(NSData*)toJSON;
19
20 @end
2 // NSDictionary+Json.h
3 // listCityWeather
4 //
5 // Created by tony on 12-9-5.
6 // Copyright (c) 2012年 chinapcc.com. All rights reserved.
7 //
8
9 #import
10
11 @interface NSDictionary (Json)
12
13
14 // 直把远程的地址上Json数据转,换成Dictionary对象
15 +(NSDictionary*)dictionaryWithContentsOfURLString:(NSString*)urlAddress;
16
17 // 把当前的Dictionary对象,转成Json对象
18 -(NSData*)toJSON;
19
20 @end
2.2 修改NSDictionary+Json.m文件
1
//
2 // NSDictionary+Json.m
3 // listCityWeather
4 //
5 // Created by tony on 12-9-5.
6 // Copyright (c) 2012年 chinapcc.com. All rights reserved.
7 //
8
9 #import " NSDictionary+Json.h "
10
11 @implementation NSDictionary (Json)
12
13 // 直把远程的地址上Json数据转,换成Dictionary对象
14 +(NSDictionary*)dictionaryWithContentsOfURLString:(NSString*)urlAddress
15 {
16 // 请求远程数据,存放到NSData对象中
17 NSData* data =[NSData dataWithContentsOfURL:[NSURL URLWithString: urlAddress]];
18
19 // 定义一个错误信息的对象
20 __autoreleasing NSError *error =nil;
21
22 // 序列化字符串
23 id result =[NSJSONSerialization JSONObjectWithData:data
24 options:kNilOptions error:&error];
25 if(error !=nil)
26 return nil;
27
28 return result;
29 }
30
31 // 把当前的Dictionary对象,转成Json对象
32 -(NSData*)toJSON{
33 NSError *error =nil;
34 // 把当前的Dictionary对象转换成字符串
35 id result =[NSJSONSerialization dataWithJSONObject:self
36 options:kNilOptions error:&error];
37 if(error !=nil)
38 return nil;
39
40 return result;
41 }
42
43 @end
2 // NSDictionary+Json.m
3 // listCityWeather
4 //
5 // Created by tony on 12-9-5.
6 // Copyright (c) 2012年 chinapcc.com. All rights reserved.
7 //
8
9 #import " NSDictionary+Json.h "
10
11 @implementation NSDictionary (Json)
12
13 // 直把远程的地址上Json数据转,换成Dictionary对象
14 +(NSDictionary*)dictionaryWithContentsOfURLString:(NSString*)urlAddress
15 {
16 // 请求远程数据,存放到NSData对象中
17 NSData* data =[NSData dataWithContentsOfURL:[NSURL URLWithString: urlAddress]];
18
19 // 定义一个错误信息的对象
20 __autoreleasing NSError *error =nil;
21
22 // 序列化字符串
23 id result =[NSJSONSerialization JSONObjectWithData:data
24 options:kNilOptions error:&error];
25 if(error !=nil)
26 return nil;
27
28 return result;
29 }
30
31 // 把当前的Dictionary对象,转成Json对象
32 -(NSData*)toJSON{
33 NSError *error =nil;
34 // 把当前的Dictionary对象转换成字符串
35 id result =[NSJSONSerialization dataWithJSONObject:self
36 options:kNilOptions error:&error];
37 if(error !=nil)
38 return nil;
39
40 return result;
41 }
42
43 @end
2.3 调用方法如下:
1 NSDictionary *city =[NSDictionary dictionaryWithContentsOfJSONURLString:
@"
http://www.weather.com.cn/data/cityinfo/101010100.html
"];
2
3 NSLog( @" city: %@ ",[[city valueForKey: @" weatherinfo "] valueForKey: @" city "] );
4
5 /
6
7 NSDictionary *myInfo =[NSDictionary dictionaryWithObjectsAndKeys: @" tony ", @" apple ", @" anny ", @" ada ",nil];
8 NSData *json =[myInfo toJSON];
9
10 NSLog( @" json: %@ ",[[NSString alloc]initWithData:json encoding:NSUTF8StringEncoding]);
2
3 NSLog( @" city: %@ ",[[city valueForKey: @" weatherinfo "] valueForKey: @" city "] );
4
5 /
6
7 NSDictionary *myInfo =[NSDictionary dictionaryWithObjectsAndKeys: @" tony ", @" apple ", @" anny ", @" ada ",nil];
8 NSData *json =[myInfo toJSON];
9
10 NSLog( @" json: %@ ",[[NSString alloc]initWithData:json encoding:NSUTF8StringEncoding]);
3. 添加plist数据, 地区资料,我就懒得做服务器端程序,这里就用一个plist数据来展示中国有天气的城市列表