最近公司事情不是很多,然后就整理一下自己在工作中比較重要的代碼.
單例的使用
/*
專門用來保存單例代碼
最后一行不要加 \
*/
// @interface
#define singleton_interface(className) \
+ (className *)shared##className;
// @implementation
#define singleton_implementation(className) \
static className *_instance; \
+ (id)allocWithZone:(NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
+ (className *)shared##className \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
}
FMDB的操作(事務(wù)+線程安全)
***數(shù)據(jù)庫路徑***
#define dbPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)[0] stringByAppendingPathComponent:@"test.db"]
***創(chuàng)建數(shù)據(jù)庫***
-(BOOL)createDBTable{
//創(chuàng)建數(shù)據(jù)庫文件
BOOL result = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
//判斷數(shù)據(jù)時是否存在
BOOL isExist = [fileManager fileExistsAtPath:dbPath];
if (!isExist) {
NSMutableArray *sqlArr = [[NSMutableArray alloc]init];
// 創(chuàng)建表-訂單的表-master頭 obj5:退貨才有,關(guān)聯(lián)的原始憑證的sid
[sqlArr addObject:@"CREATE TABLE IF NOT EXISTS 'master' ('sid' varchar NOT NULL PRIMARY KEY,'imel' varchar,'type' int)"];
// 創(chuàng)建
result = [self execute:sqlArr];
}else{
result = YES;
}
return result;
}
***對原有數(shù)據(jù)庫進(jìn)行增加字段,增加表***
-(BOOL)executeAddnew{
__block BOOL result = NO;
FMDatabaseQueue *dbQueue = [[FMDatabaseQueue alloc]initWithPath:dbPath];
[dbQueue inDatabase:^(FMDatabase *db) {
[db beginTransaction];
//增加表
if (![db tableExists:@"TicketInfo"]) {
[db executeUpdate:@"CREATE TABLE IF NOT EXISTS 'TicketInfo' ('ticketSid' varchar NOT NULL PRIMARY KEY, 'storeNo' varchar, 'posNo' varchar, 'transNo' varchar, 'price' float, 'date' varchar, 'flag' int)"];
//增加字段
if ([db tableExists:@"Printer"]) {
if (![db columnExists:@"Printer" inTableWithName:@"sid"]) {
[db executeUpdate:@"ALTER TABLE 'Printer' ADD 'sid' varchar"];
}
}
}
result = [db commit];
}];
[dbQueue close];
return result;
}
***插入多條語句***
-(BOOL)execute:(NSMutableArray*)sqlStrArr
{
__block BOOL result = NO;
FMDatabaseQueue *dbQueue = [[FMDatabaseQueue alloc]initWithPath:dbPath];
[dbQueue inDatabase:^(FMDatabase *db) {
[db beginTransaction];
for (NSString *sqlStr in sqlStrArr) {
BOOL detailResult = [db executeUpdate:sqlStr];
if (!detailResult) {
[db rollback];
break;
}
}
result = [db commit];
}];
return result;
}
***插入單條語句***
-(BOOL)executeSingle:(NSString*)sqlStr
{
__block BOOL result = NO;
FMDatabaseQueue *dbQueue = [[FMDatabaseQueue alloc]initWithPath:dbPath];
[dbQueue inDatabase:^(FMDatabase *db) {
result = [db executeUpdate:sqlStr];
if (!result) {
[db rollback];
}
}];
return result;
}
***查詢***
-(NSArray *)queryGoodsTypeWithSqlite:(NSString *)sqlite{
__block NSMutableArray *marray = [[NSMutableArray alloc]initWithCapacity:10];
FMDatabaseQueue *dbQueue = [[FMDatabaseQueue alloc]initWithPath:dbPath];
[dbQueue inDatabase:^(FMDatabase *db) {
FMResultSet *result = [db executeQuery:sqlite];
while ([result next]) {
GoodsTypeInfoModel *goodsTypeModel = [[GoodsTypeInfoModel alloc]init];
goodsTypeModel.goodSid = [result stringForColumn:@"goodSid"];
[marray addObject: goodsTypeModel];
}
}];
return marray;
}
AFNetworking的封裝
時間處理
JSON解析
見GitHub地址:https://github.com/Sunkangp/TOOL