(10.19)SQL列出城市

1、关于把中文转化为拼音和选出首字符的方法:

-(NSString *)convertTextToAlphabet:(NSString*)text{
    NSMutableString *str3 = text.mutableCopy;
    //把str3转换成拼音
    CFStringTransform((__bridge CFMutableStringRef)str3, NULL, kCFStringTransformToLatin, NO);
    //把str3的声调去掉
    CFStringTransform((__bridge CFMutableStringRef)str3, NULL, kCFStringTransformStripDiacritics, NO);
    //返回str3的首字符,并转化为大写
    return [[str3 substringToIndex:1] uppercaseString];
}

2、关于sql在OC中持久化的实现:

//在这之前建立一个数据库的类文件
//先导入库文件
//封装
-(void)persistCity:(City *)city{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    unsigned int count;
    objc_property_t *pros = class_copyPropertyList([city class], &count);
    for (unsigned int i = 0; i < count; i++) {
        objc_property_t pro = pros[i];
        const char *obj_str = property_getName(pro);
        NSString *key = [[NSString alloc]initWithCString:obj_str encoding:NSUTF8StringEncoding];
        id value = [city valueForKey:key];
        dict[key] = value;
        
    }
    
    NSMutableString *str1 = [dict.allKeys componentsJoinedByString:@","].mutableCopy;
    [str1 insertString:@"(" atIndex:0];
    [str1 appendString:@" )"];
    
    NSMutableString *str2 = [dict.allKeys componentsJoinedByString:@",:"].mutableCopy;
    [str2 insertString:@"(:" atIndex:0];
    [str2 appendString:@")"];
    NSString*tab = [NSString stringWithFormat:@"create table if not exists %@ %@",NSStringFromClass([city class]),str1];
    
    [_dataBase executeUpdate:tab];
    NSString *sql = [NSString stringWithFormat:@"insert into %@ %@ values %@",NSStringFromClass([city class]),str1,str2];
    if([_dataBase executeUpdate:sql withParameterDictionary:dict]){
        NSLog(@"success");
    }
    else {
        NSLog(@"error = %@",_dataBase.lastErrorMessage);
    }
}

3、OC中去重的方法

    //去重
    NSSet *set = [NSSet setWithArray:_alpha];

你可能感兴趣的:((10.19)SQL列出城市)