// // ViewController.m // GCD // // Created by 朱逸 on 16/7/5. // Copyright © 2016年 朱逸. All rights reserved. // /* * 同步 : 你叫我去吃饭,如果我没有听到,你就会一直叫我去吃饭 * 异步 : 你叫我去吃饭,而不管我是否有没有听到,就自己去了 * 如果你叫我去吃饭,就用异步,这样你就比较省钱 */ #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } #pragma mark--创建串行队列,并执行操作 - (void)serialQueueOfAsync{ /* * 串行队列下的异步操作 */ dispatch_queue_t serial = dispatch_queue_create("xiaozhu", DISPATCH_QUEUE_SERIAL); dispatch_async(serial, ^{ /* * 执行所需要的操作 */ [self doSomeThings]; }); } #pragma mark--需要执行的代码 - (void)doSomeThings{ dispatch_queue_t mainQueue = dispatch_get_main_queue(); dispatch_async(mainQueue, ^{ /* * 刷新UI */ }); } #pragma mark--并行队列的创建方式 - (void)queue{ /* * 全局队列 * 第一个参数表示队列优先级 * 第二个参数没有用 */ dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(global, ^{ }); /* * 并行队列 * 执行无顺序 */ dispatch_queue_t concurrent = dispatch_queue_create("zy", DISPATCH_QUEUE_CONCURRENT); dispatch_async(concurrent, ^{ }); } #pragma mark--dispatch_group_t - (void)group{ dispatch_queue_t queue = dispatch_queue_create("", DISPATCH_QUEUE_SERIAL); dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, queue, ^{ NSLog(@"1"); }); dispatch_group_async(group, queue, ^{ NSLog(@"2"); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"end"); }); } @end
// // ViewController.m // NSOperation // // Created by 朱逸 on 16/7/5. // Copyright © 2016年 朱逸. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } #pragma mark--NSInvocaionOperation - (void)queueWithInvocationOpration{ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; for (int i = 0; i < 8; i++) { NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doSomeThings) object:nil]; [queue addOperation:invocation]; } } #pragma mark--NSBlockOperation - (void)queueWithBlockOperation{ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; for (int i = 0; i < 9; i++) { NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{ [self doSomeThings]; }]; [queue addOperation:blockOperation]; } } #pragma mark--queue - (void)queue{ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; for (int i = 0; i < 8; i++) { [queue addOperationWithBlock:^{ [self doSomeThings]; }]; } } #pragma mark--优先级 - (void)praority{ NSOperationQueue *queue = [[NSOperationQueue alloc] init]; NSBlockOperation *highBlockOperation = [NSBlockOperation blockOperationWithBlock:^{ [self praorityHigh]; }]; NSBlockOperation *lowBlockOperation = [NSBlockOperation blockOperationWithBlock:^{ [self praorityLow]; }]; [lowBlockOperation addDependency:highBlockOperation]; NSArray *operationArr = [NSArray arrayWithObjects:highBlockOperation, lowBlockOperation, nil]; [queue addOperations:operationArr waitUntilFinished:YES]; } - (void)doSomeThings{ } - (void)praorityHigh{ } - (void)praorityLow{ } @end
// // ViewController.m // JavaScript-OC // // Created by 朱逸 on 16/7/4. // Copyright © 2016年 朱逸. All rights reserved. // #import "ViewController.h" @interface ViewController ()<WKScriptMessageHandler,WKUIDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.wk.UIDelegate = self; [self.view addSubview:self.wk]; [self loadHtml:@"JSWKWebView"]; } - (WKWebView *)wk{ if (_wk != nil) { return _wk; } /* * 初始化WKWebView */ _wk = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:self.config]; return _wk; } - (WKWebViewConfiguration *)config{ if (_config != nil) { return _config; } /* * 初始化WKWebViewConfiguration,配置WKWebView */ _config = [[WKWebViewConfiguration alloc] init]; _config.userContentController = self.content; _config.websiteDataStore = self.store; return _config; } - (WKUserContentController *)content{ if (_content != nil) { return _content; } /* * 初始化WKUserContentController,用于做native与JavaScript的交互管理 */ _content = [[WKUserContentController alloc] init]; [_content addScriptMessageHandler:self name:@"Native"]; return _content; } -(WKWebsiteDataStore *)store{ if (_store != nil) { return _store; } _store = [WKWebsiteDataStore defaultDataStore]; return _store; } #pragma mark--加载网页 - (void)loadHtml:(NSString *)name{ NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"html"]; NSURL *url = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.wk loadRequest:request]; } #pragma mark--WKScriptMessageHandler协议,用于接收并处理JavaScript调用Native的方法 - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{ NSLog(@"body--%@",message.body); NSMutableDictionary *dict = [self qureyStringToDictionary:message.body]; NSLog(@"dict--%@",dict); NSString *func = [dict objectForKey:@"func"]; NSLog(@"func--%@",func); if ([func isEqualToString:@"alert"]) { [self sayHello:[dict objectForKey:@"message"]]; } else if ([func isEqualToString:@"addSubView"]) { }else if ([func isEqualToString:@"addSubView"]) { } else { } } #pragma mark--参数转字典 - (NSMutableDictionary *)qureyStringToDictionary:(NSString *)body{ NSArray *element = [body componentsSeparatedByString:@"&"]; NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:[element count]]; for (NSString *str in element) { NSArray *array = [str componentsSeparatedByString:@"="]; [dict setObject:[array objectAtIndex:1] forKey:[array objectAtIndex:0]]; } return dict; } #pragma mark--JavaScript调用的Native方法 - (void)sayHello:(NSString *)message{ UIAlertView *aleart = [[UIAlertView alloc] initWithTitle:@"来自网页的提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [aleart show]; } #pragma mark--OC调用JavaScript方法 - (void)JavaScriptCallOC{ [self.wk evaluateJavaScript:@"showAlert(‘hahahha‘)" completionHandler:^(id item, NSError * _Nullable error) { }]; } @end
static AccountManager *DefaultManager = nil; + (AccountManager *)defaultManager { if (!DefaultManager) DefaultManager = [[self allocWithZone:NULL] init]; return DefaultManager; } + (AccountManager *)sharedManager { static AccountManager *sharedAccountManagerInstance = nil; static dispatch_once_t predicate; dispatch_once(&predicate, ^{ sharedAccountManagerInstance = [[self alloc] init]; }); return sharedAccountManagerInstance; }
//用来记录上一次创建的UILabel UILabel *lastLabel = nil; for (int i = 0; i < 9; i++) { UILabel *label = [UILabel new]; label.textAlignment = NSTextAlignmentCenter; label.backgroundColor = [UIColor orangeColor]; [self.view addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { //设置高度 make.height.offset(40.0f); //计算距离顶部的公式 60 = 上一个距离顶部的高度 + UIlabel的高度 float colTop = (100 + i/3 * 60.0f); make.top.offset(colTop); //当是 左边一列的时候 都是 距离父视图 向左边 20的间隔 if (i%3 == 0) { make.left.offset(20.0f); }else{ //当时中间列的时候 在上一个UIlabel的右边 添加20个 距离 并且设置等高 make.width.equalTo(lastLabel.mas_width); make.left.equalTo(lastLabel.mas_right).offset(20.0f); } //当是 最右边列的时候 距离右边父视图的 距离为20 因为是向左所以是-20 控制底部也是 负数!! if (i%3 == 2) { make.right.offset(-20.0f); } }]; lastLabel = label; }
// // AppDBOperation.m // BigTiger // // Created by zdf on 14-8-18. // // #import "AppDBOperation.h" #import "FMDatabaseQueue.h" @implementation AppDBOperation #pragma mark - 数据库 核心操作 +(AppDBOperation*)sharedDBOperation { AppDBOperation *q = [[self alloc] init] ; return q; } +(void)insertIntoUserInfoWithUserDic:(NSDictionary *)dic { /* * @param AccessToken 认证令牌 * @param RefreshToken 刷新令牌 */ FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:DLDBPath]; [queue inDatabase:^(FMDatabase *db) { [db executeUpdate:@"delete from UserInfo"]; BOOL result = [db executeUpdate:@"INSERT INTO UserInfo (UserID,Ucard,UserType,UserName,PassWord,AccessToken,RefreshToken) VALUES (?,?,?,?,?,?,?)",[dic objectForKey:@"uid"],[dic objectForKey:@"uCard"],[dic objectForKey:@"uCardType"],[dic objectForKey:@"uTel"],[dic objectForKey:@"uPwd"],[dic objectForKey:@"AccessToken"],[dic objectForKey:@"RefreshToken"]]; NSLog(@"result %d",result); } ]; } +(void)deleteUserInfoDB{ FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:DLDBPath]; [queue inDatabase:^(FMDatabase *db) { [db executeUpdate:@"delete from UserInfo "]; } ]; } +(void)updateUserNameWithuserId{ FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { BOOL result = [db executeUpdate:@"update UserInfo set UserName=? where UserID = ?",userName,userID]; NSLog(@"result %d",result); } [db close]; } +(void)updateUserAccessTokenAndRefreshTokenWithUserId{ FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { BOOL result = [db executeUpdate:@"update UserInfo set AccessToken=?,RefreshToken=? where UserID = ?",AccessToken,RefreshToken,userID]; NSLog(@"result %d",result); } [db close]; } +(UserObj *)selectUserInfoByUserId:(NSString *)userId{ UserObj * obj=nil; FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { FMResultSet *rs; rs = [db executeQuery:@"select * from UserInfo where UserID=?",userId]; while ([rs next]) { obj=[UserObj setUserObjWithLocalUserDic:rs.resultDictionary]; } [rs close]; } [db close]; return obj; } +(void) insertintoSearchHistoryWithSearchStr:(NSString *)searchStr SearchDate:(NSString *)date{ if (searchStr == nil || searchStr.length==0) { return; } FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { bool isHave = NO; //去重 FMResultSet *rs1 = [db executeQuery:@"select * from SearchHistory where SearchStr = ? and userID = ?",searchStr, [ShareMethod returnStringBy:userID]]; if ([rs1 next]) { isHave=YES; } [rs1 close]; if (isHave) { return; } NSMutableArray *dataAry = [NSMutableArray array]; FMResultSet *rs; rs = [db executeQuery:@"SELECT * FROM SearchHistory where userID = ? order by SearchDate desc", [ShareMethod returnStringBy:userID]];// while ([rs next]) { if (dataAry.count==9) { [db executeUpdate:@"delete from SearchHistory where SearchDate = ? and userID = ?",[rs.resultDictionary objectForKey:@"SearchDate"], [ShareMethod returnStringBy:userID]]; } else{ [dataAry addObject:rs.resultDictionary]; } } BOOL result = [db executeUpdate:@"INSERT INTO SearchHistory (SearchDate,SearchStr,userID) VALUES (?,?,?)",date,searchStr,[ShareMethod returnStringBy:userID]]; NSLog(@"result %d",result); [rs close]; } [db close]; } +(NSMutableArray *)selectAllSearchHistory{ NSMutableArray *ary = [NSMutableArray array]; FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { FMResultSet *rs; rs = [db executeQuery:@"select * from SearchHistory where userID = ?", [ShareMethod returnStringBy:userID]]; while ([rs next]) { [ary addObject:rs.resultDictionary]; } [rs close]; } [db close]; return ary; } +(void) insertMerchantSearchHistoryWithStr:(NSString *)searchStr{ if (searchStr == nil || searchStr.length==0) { return; } //获取的系统时间 NSString *dateStr = [ShareMethod dateToTimeString:[NSDate date] Formar:nil]; FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { bool isHave = NO; //去重 FMResultSet *rs1 = [db executeQuery:@"select * from MerchantsSearchHistory where SearchStr = ? and userID = ?",searchStr, [ShareMethod returnStringBy:userID]]; if ([rs1 next]) { isHave=YES; } [rs1 close]; if (isHave) { return; } NSMutableArray *dataAry = [NSMutableArray array]; FMResultSet *rs; rs = [db executeQuery:@"SELECT * FROM MerchantsSearchHistory where userID = ? order by SearchDate desc", [ShareMethod returnStringBy:userID]]; while ([rs next]) { if (dataAry.count==9) { [db executeUpdate:@"delete from MerchantsSearchHistory where SearchDate = ? and userID = ?",[rs.resultDictionary objectForKey:@"SearchDate"], [ShareMethod returnStringBy:userID]]; } else{ [dataAry addObject:rs.resultDictionary]; } } BOOL result = [db executeUpdate:@"INSERT INTO MerchantsSearchHistory (SearchDate,SearchStr,userID) VALUES (?,?,?)",dateStr,searchStr,userID]; NSLog(@"result %d",result); [rs close]; [db close]; } } +(NSMutableArray *)getAllMerchantSearchHistory{ NSMutableArray *ary = [NSMutableArray array]; FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { FMResultSet *rs; rs = [db executeQuery:@"select * from MerchantsSearchHistory where userID = ?", [ShareMethod returnStringBy:userID]]; while ([rs next]) { [ary addObject:rs.resultDictionary]; } [rs close]; } [db close]; return ary; } +(void)insertExceptSearchHistoryWithStr:(NSString *)searchStr { if (searchStr == nil || searchStr.length==0) { return; } //获取的系统时间 NSString *dateStr = [ShareMethod dateToTimeString:[NSDate date] Formar:nil]; FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { bool isHave = NO; //去重 FMResultSet *rs1 = [db executeQuery:@"select * from ExceptSearchHistory where SearchStr = ? and userID = ?",searchStr, [ShareMethod returnStringBy:userID]]; if ([rs1 next]) { isHave=YES; } [rs1 close]; if (isHave) { return; } NSMutableArray *dataAry = [NSMutableArray array]; FMResultSet *rs; rs = [db executeQuery:@"SELECT * FROM ExceptSearchHistory where userID = ? order by SearchDate desc", [ShareMethod returnStringBy:userID]]; while ([rs next]) { if (dataAry.count==9) { [db executeUpdate:@"delete from ExceptSearchHistory where SearchDate = ? and userID = ?",[rs.resultDictionary objectForKey:@"SearchDate"], [ShareMethod returnStringBy:userID]]; } else{ [dataAry addObject:rs.resultDictionary]; } } BOOL result = [db executeUpdate:@"INSERT INTO ExceptSearchHistory (SearchDate,SearchStr,userID) VALUES (?,?,?)",dateStr,searchStr,userID]; NSLog(@"result %d",result); [rs close]; [db close]; } } +(NSMutableArray *)getAllExceptSearchHistory { NSMutableArray *ary = [NSMutableArray array]; FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { FMResultSet *rs; rs = [db executeQuery:@"select * from ExceptSearchHistory where userID = ?", [ShareMethod returnStringBy:userID]]; while ([rs next]) { [ary addObject:rs.resultDictionary]; } [rs close]; } [db close]; return ary; } @end
// // FMDBObj.m // DuiDuiPeng3.0 // // Created by yangbin on 15/10/26. // Copyright © 2015年 bar. All rights reserved. // #import "FMDBObj.h" @implementation FMDBObj // 创建表 -(void)createDatabaseAndFiles { NSString *rootFilePath=[FileFunction createFolderAtDocuments:@"APP"];//根目录 [FileFunction createFolderAtPath:rootFilePath folderName:@"Temp"];//缓存 [FileFunction createFolderAtPath:rootFilePath folderName:@"Data"];//本地存储数据 FMDatabase *db = [FMDatabase databaseWithPath:DLDBPath]; if ([db open]) { [db setShouldCacheStatements:YES]; /** * 个人信息表 * * @param UserID 用户ID * @param Ucard 用户card * @param UEmail 用户email * @param UserType 用户身份 * @param UserName 用户名 * @param PassWord 密码 * @param AccessToken 认证令牌 * @param RefreshToken 刷新令牌 * * @return */ [db executeUpdate:@"CREATE TABLE [UserInfo] (UserID text,Ucard text,UserType text,UserName text,PassWord text,AccessToken text,RefreshToken text)"]; NSLog(@"执行语句失败 :%@",db.lastErrorMessage); /* * 搜索历史记录礼品 * * @param SearchDate 搜索时间 * @param userID 用户id * @param SearchStr 搜索内容 * * @return */ [db executeUpdate:@"CREATE TABLE [SearchHistory] (SearchDate text,SearchStr text, userID text)"]; /* * SearchHistory 搜索历史记录商家 * * @param SearchDate 搜索时间 * @param SearchStr 搜索内容 * @param userID 用户id * * @return */ [db executeUpdate:@"CREATE TABLE [MerchantsSearchHistory] (SearchDate text,SearchStr text, userID text)"]; /* * SearchHistory 搜索历史记录同城券 * * @param SearchDate 搜索时间 * @param userID 用户id * @param SearchStr 搜索内容 * * @return */ [db executeUpdate:@"CREATE TABLE [ExceptSearchHistory] (SearchDate text,SearchStr text, userID text)"]; } [db close]; } #pragma mark - 判断字段是否存在 +(BOOL)isExistRateColumn:(NSString*)column TableName:(NSString*)table { BOOL isexist = NO; FMDatabase * db=[FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; NSString *str = nil; if ([db open]) { NSString *selectSql = [NSString stringWithFormat:@"select sql from sqlite_master where tbl_name=‘%@‘ and type=‘table‘",table]; FMResultSet *rs = [db executeQuery:selectSql]; if ([rs next]) { str = [rs stringForColumnIndex:0]; if (str && [str rangeOfString:column].location==NSNotFound) { isexist =NO; }else{ isexist =YES; } } [rs close]; } [db close]; return isexist; } +(void)addColumn:(NSString*)column TableName:(NSString*)table DefaultValue:(id)defaultValue { NSString *sqlStr; if ([defaultValue isKindOfClass:[NSNumber class]]) { // 插入数值 sqlStr = [[NSString alloc] initWithFormat:@"ALTER TABLE %@ ADD COLUMN %@ DEFAULT %d",table,column,[(NSNumber *)defaultValue boolValue]]; }else if ([defaultValue isKindOfClass:[NSDate class]]){ sqlStr=[[NSString alloc] init]; } else if ([defaultValue isKindOfClass:[NSString class]]) // 插入 字符 { sqlStr = [[NSString alloc] initWithFormat:@"ALTER TABLE %@ ADD COLUMN %@ DEFAULT ‘%@‘",table,column,defaultValue]; } else // 插入默认值 { sqlStr = [[NSString alloc] initWithFormat:@"ALTER TABLE %@ ADD COLUMN %@",table,column]; } FMDatabase *db=[FMDatabase databaseWithPath:DLDBPath]; [db setShouldCacheStatements:YES]; if ([db open]) { BOOL succe = [db executeUpdate:sqlStr]; printf("succ is %d",succe); } [db close]; } @end
时间: 2024-11-10 03:34:00