本文中将简单介绍几个笔者认为在开发中很有用的iOS开源类库及其简单使用方法
1: SBJson
SBJson, 又名Json Framework, 是一个非常流行的,开源的JSON解析类库。SBJSon的使用非常简单,为在网络中传输与解析格式化的数据提供了极大的便利。
SBJson的使用也很简单,在项目中将类库文件添加到项目中,然后加入几个依赖的FrameWork,目前比价稳定的有ARC3.1版本的和非ARC版本的3.0,大家可以根据自身需要进行下载。
下面就SBJson的使用进行简单介绍:
- #import "SBJSon.h"
- //写json
- SBJsonWriter* jsonWriter = [[[SBJsonWriter alloc] init] autorelease];
- NSMutableArray* tempArray = [NSMutableArray array];
- NSDictionary* tempDicA = [NSMutableDictionary dictionary];
- [tempDicA addObject @"valueA" forKey @"keyA"];
- [tempArray addObject: tempDicA];
- NSDictionary* tempDicB = [NSMutableDictionary dictionary];
- [tempDicB addObject @"valueB" forKey @"keyB"];
- [tempArray addObject: tempDicB];
- NSMutableDictionary* jsonDic = [NSMutableDictionary dictionary];
- [jsonDic setObject: tempArray forKey: @"array"];
- NSString* jsonString = [jsonWriter stringWithObject: jsonDic];
- //解析json
- NSDictionary* resultDic = [jsonString JSONValue];
- NSArray* resultArray = [resultDic objectForKey @"array"];
- NSDictionary* dicA = [resultArray objectAtIndex: 0];
- NSDictionary* dicB = [resultArray objectAtIndex: 1];
- NSLog(@"%@, %@" [dicA objectForKey @"keyA", [dicB objectForKey: @"keyB"]);
2: ASIHTTPRequest
ASIHTTPRequest是一个非常流行的iOS平台网络通信类库,使用ASIHTTPRequest之后,大大简化了iOS平台的网络编程。其以方便的接口对同步、异步的网络传输进行了传输,将ASIHTTPRequest添加到自己的项目也非常方便,将类库中所有文件拷贝到一个文件夹中,然后将此文件夹添加到项目中,同时要添加如下图CFNetWork之下所示的类库,就可以使用ASIHTTPRequest了:
使用ASIHTTPRequest步骤非常简答,在一般应用开发中,网络连接基本上使用的都是异步方式,下面简单演示一下最简单的异步通讯方法
- #import "ASIHTTPRequest.h"
- - (void) requestDataFromServer
- {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- NSURL* url = [NSURL URLWithString: @"www.fakeurl.com"];
- ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL: url];
- [request setTag: 1024];
- [request setTimeOutSeconds: 3];
- [request setAllowCompressedResponse:YES];
- [request setDelegate:self];
- [request startAsynchronous];
- [pool drain];
- }
- - (void)requestFinished:(ASIHTTPRequest *)request
- {
- NSString* rawString = [request responseString];
- if (request.tag == 1024) {
- //处理网络返回结果
- }
- }
- - (void)requestFailed:(ASIHTTPRequest *)request
- {
- if (request.tag == 1024) {
- //处理网络错误
- }
- }
注意上面的两个函数中,后面连个为ASIHTTPRequest的delegate函数,其声明类型不能改变,只要在生成ASIHTTPRequest时的deleage设成了self,那么最后返回结果,不管是成功调用还是网络失败,都会调用这两个函数中的对应的一个。
3: FMDataBase
FMDataBase是iOS平台中一个非常强大的数据库类库,其将sqlite面向过程的接口以面向对象的方法展现出来,提供了极高的可用性。
其使用很简单,将sqlite 库添加到项目中,然后将FMDataBase类库文件添加到项目中,下面是对笔者对FMDataBase进行的一个二次封装,处理的数据库很简单,只有一张表,两个列,存储的都是一些key-value对,读者可以根据自身需要对此类进行修改。
- #import <Foundation/Foundation.h>
- #import "FMDatabase.h"
- @interface DBController : NSObject {
- }
- @property (nonatomic, assign) FMDatabase *dataBase;
- +(BOOL)databaseExit;
- -(BOOL)initDatabase;
- -(void)closeDatabase;
- -(BOOL)deleteTable;
- -(BOOL)InsertTable:(NSString *)key_type value:(NSString *)key_value;
- -(BOOL)UpdataTable:(NSString *) valueStr key:(NSString *)keyStr;
- -(NSMutableDictionary *)querryTable;
- +(BOOL) deleteDataBase;
- @end
- @synthesize dataBase = _dataBase;
- - (id)init{
- if(self = [super init]){
- _dataBase = [FMDatabase databaseWithPath: [DBController getPath]];
- if (![_dataBase open]) {
- NSLog(@"Create/Open dataBase %@ Failed!", [DBController getPath]);
- }
- }
- return self;
- }
- //数据库是否存在
- +(BOOL)databaseExit
- {
- return [[NSFileManager defaultManager] fileExistsAtPath: [self getPath]];
- }
- //初始化数据库
- -(BOOL)initDatabase{
- if ([DBController databaseExit]) {
- return [self createTable];
- }
- return NO;
- }
- //创建数据库
- -(BOOL)createTable
- {
- return [self.dataBase executeUpdate: @"create table if not exists personTable(id integer primary