- 单例
使用dispatch_once可以简化代码且保证线程安全,开发者无需担心加锁或同步。所有问题都在GCD底层处理。此外,dispatch_once更高效。它没有使用重量级的同步机制。使用同步机制,每次运行代码都需要获取锁。dispatch_once采用“原子访问”来查询标记,判断代码是否执行过。
- 视错觉效果
视错觉就是当人观察物体时,基于经验主义或不当的参照形成的错误的判断和感知,是指观察者在客观因素干扰下或者自身的心理因素支配下,对图形产生的与客观事实不相符的错误的感觉。
我们日常生活中,所遇到的视错觉的例子有很多。比如,法国国旗白:蓝:红三色的比例为33:30:37,而我们却感觉三种颜色面积相等。这是因为白色给人以扩张感觉,而蓝色则有收缩的感觉,这就是视错觉。
image.png
视错觉结合UI:从一个看似简单的自定义控件说起
FMDB
+(DataBaseTool*)shareDataBaseTool{
static DataBaseTool *dataBaseTool = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dataBaseTool = [[DataBaseTool alloc]init];
});
return dataBaseTool;
}
+(FMDatabaseQueue*)shareQueue{
static FMDatabaseQueue *queue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [filePath objectAtIndex:0];
NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"MyData.sqlite"];
//self.db = [FMDatabase databaseWithPath:dbFilePath];
queue = [FMDatabaseQueue databaseQueueWithPath:dbFilePath];
});
return queue;
}
//建表,所有数据库表的创建都写在这个方法里
-(void)creatTable{
FMDatabaseQueue *queue = [DataBaseTool shareQueue];
[queue inDatabase:^(FMDatabase *db) {
[db setShouldCacheStatements:YES];
BOOL result1 = [db tableExists:@"PhoneType1"];
BOOL result2 = [db tableExists:@"PhoneType2"];
BOOL result3 = [db tableExists:@"PhoneType3"];
BOOL result4 = [db tableExists:@"PhoneType4"];
if (result1 == YES) {
BOOL result = [db executeUpdate:@"DROP TABLE PhoneType1"];
if (result) {
[db executeUpdate:@"CREATE TABLE PhoneType1(ID TEXT,channel_id TEXT,class_layer TEXT,class_list TEXT,parent_id TEXT,sort_id INTEGER,title TEXT,data BLOB)"];
}else{
}
}else{
BOOL result = [db executeUpdate:@"CREATE TABLE PhoneType1(ID TEXT,channel_id TEXT,class_layer TEXT,class_list TEXT,parent_id TEXT,sort_id INTEGER,title TEXT,data BLOB)"];
if (result) {
}else{
}
}
if (result2 == YES) {
BOOL result = [db executeUpdate:@"DROP TABLE PhoneType2"];
if (result) {
[db executeUpdate:@"CREATE TABLE PhoneType2(ID TEXT,channel_id TEXT,class_layer TEXT,class_list TEXT,parent_id TEXT,sort_id INTEGER,title TEXT,data BLOB)"];
}else{
}
}else{
BOOL result = [db executeUpdate:@"CREATE TABLE PhoneType2(ID TEXT,channel_id TEXT,class_layer TEXT,class_list TEXT,parent_id TEXT,sort_id INTEGER,title TEXT,data BLOB)"];
if (result) {
// NSLog(@"创建表完成");
}else{
// NSLog(@"创建表失败");
}
}
if (result3 == YES) {
BOOL result = [db executeUpdate:@"DROP TABLE PhoneType3"];
if (result) {
[db executeUpdate:@"CREATE TABLE PhoneType3(ID TEXT,channel_id TEXT,class_layer TEXT,class_list TEXT,parent_id TEXT,sort_id INTEGER,title TEXT,data BLOB)"];
}else{
}
}else{
BOOL result = [db executeUpdate:@"CREATE TABLE PhoneType3(ID TEXT,channel_id TEXT,class_layer TEXT,class_list TEXT,parent_id TEXT,sort_id INTEGER,title TEXT,data BLOB)"];
if (result) {
}else{
}
}
if (result4 == YES) {
BOOL result = [db executeUpdate:@"DROP TABLE PhoneType4"];
if (result) {
[db executeUpdate:@"CREATE TABLE PhoneType4(ID TEXT,channel_id TEXT,class_layer TEXT,class_list TEXT,parent_id TEXT,sort_id INTEGER,title TEXT,data BLOB)"];
}else{
}
}else{
BOOL result = [db executeUpdate:@"CREATE TABLE PhoneType4(ID TEXT,channel_id TEXT,class_layer TEXT,class_list TEXT,parent_id TEXT,sort_id INTEGER,title TEXT,data BLOB)"];
if (result) {
}else{
}
}
}];
}
-(void)createPhoneInfoTable{
FMDatabaseQueue *queue = [DataBaseTool shareQueue];
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db setShouldCacheStatements:YES];
//创建商品信息
BOOL lc_phoneInfo = [db tableExists:@"PerPhoneInfo"];
//存储商家手机信息
if (lc_phoneInfo == YES) {
//清除表格
BOOL result = [db executeUpdate:@"drop table PerPhoneInfo"];
if (result == YES) {
[db executeUpdate:@"create table if not exists PerPhoneInfo(GID text primary key not null,TCID text,TName text,GTitle text,Price text,GStock text,Pic text,UpTime text,data BLOB)"];
}else{
}
}else{
BOOL resul = [db executeUpdate:@"create table if not exists PerPhoneInfo(GID text primary key not null,TCID text,TName text,GTitle text,Price text,GStock text,Pic text,UpTime text,data BLOB)"];
if (resul) {
}else{
}
}
}];
}
-(void)insertPhoneType:(PhoneType *)phoneType Block:(void(^)(NSString *result))block{
FMDatabaseQueue *queue = [DataBaseTool shareQueue];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db setShouldCacheStatements:YES];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:phoneType];
if ([phoneType.class_layer isEqualToString:@"1"]) {
[db executeUpdate:@"INSERT INTO PhoneType1(ID,channel_id,class_layer,class_list,parent_id,sort_id,title,data) VALUES (?,?,?,?,?,?,?,?)",phoneType.ID,phoneType.channel_id,phoneType.class_layer,phoneType.class_list,phoneType.parent_id,@(phoneType.sort_id),phoneType.title,data];
}else if ([phoneType.class_layer isEqualToString:@"2"]){
[db executeUpdate:@"INSERT INTO PhoneType2(ID,channel_id,class_layer,class_list,parent_id,sort_id,title,data) VALUES (?,?,?,?,?,?,?,?)",phoneType.ID,phoneType.channel_id,phoneType.class_layer,phoneType.class_list,phoneType.parent_id,@(phoneType.sort_id),phoneType.title,data];
}else if([phoneType.class_layer isEqualToString:@"3"]){
[db executeUpdate:@"INSERT INTO PhoneType3(ID,channel_id,class_layer,class_list,parent_id,sort_id,title,data) VALUES (?,?,?,?,?,?,?,?)",phoneType.ID,phoneType.channel_id,phoneType.class_layer,phoneType.class_list,phoneType.parent_id,@(phoneType.sort_id),phoneType.title,data];
}else if ([phoneType.class_layer isEqualToString:@"4"]){
[db executeUpdate:@"INSERT INTO PhoneType4(ID,channel_id,class_layer,class_list,parent_id,sort_id,title,data) VALUES (?,?,?,?,?,?,?,?)",phoneType.ID,phoneType.channel_id,phoneType.class_layer,phoneType.class_list,phoneType.parent_id,@(phoneType.sort_id),phoneType.title,data];
}
}];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *success = @"加载完毕";
NSLog(@"%@",success);
block(success);
});
});
}
-(void)selectAllPhoneType{
_arrayOne = [[NSMutableArray alloc]init];
FMDatabaseQueue *queue = [DataBaseTool shareQueue];
[queue inDatabase:^(FMDatabase *db) {
FMResultSet *result = [db executeQuery:@"select *from PhoneType1 where class_layer = 1 order by sort_id"];
while ([result next]) {
NSData * data = [result dataForColumn:@"data"];
PhoneType *phoneType = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[_arrayOne addObject:phoneType];
}
}];
}
//判断表是否存在
- (BOOL)isTableOK:(NSString *)tableName
{
FMDatabaseQueue *queue = [DataBaseTool shareQueue];
__block int count;
[queue inDatabase:^(FMDatabase *db) {
FMResultSet *rs = [db executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = ?", tableName];
while ([rs next])
{
// just print out what we've got in a number of formats.
count = [rs intForColumn:@"count"];
NSLog(@"isTableOK %d", count);
}
}];
if (count == 0)
{
return NO;
}
else
{
return YES;
}
return NO;
}
swift和OC的异同
浅谈Swift和OC的区别