登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

一些有用的分类 [复制链接]

2017-6-27 11:39
ccp_code4 阅读:177 评论:0 赞:0
Tag:  项目实用分类NSObjectNSArrayUIView
第一次接触一个超级项目,从零开始逐步搭建花了好几个月.现在项目做完,放出一些有用到的分类供大家参考.
不bb,直接放代码
ps:东西太多,没有一次性写完,GitHub上会陆续更新,而且这个东西的排版也不会,需要的兄弟移步
https://github.com/coolboy-ccp/CCPExtension
1. NSArray
/*
 * 创建文件名为 name的plist
 * 将数组存储到 name文件中
 */
- (void)writeTo:(NSString *_Nonnull)name {
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *baseP = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *fp = [baseP add:[name add:@".plist"]];
    if (![fm fileExistsAtPath:fp]) {
        [fm createFileAtPath:fp contents:nil attributes:nil];
    }
    [self writeToFile:fp atomically:YES];
}
/*
 * 去重
 */
- (NSArray *_Nonnull)removeDuplicate {
    return [self valueForKey:@"distinctUnionOfObjects.self"];
}

/*
 * 映射
 * ps 这个不知道有没有问题,目前项目中用到的都没有啥问题,求指点
 */
- (NSArray *)map:(id(^)(id value))block {
    NSMutableArray *marr = self.mutableCopy;
    [marr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        id value = block(obj);
        [marr replaceObjectAtIndex:idx withObject:value];
    }];
    return marr.copy;
}
2.NSString

/*
 * 拼接
 */
- (NSString *_Nonnull)add:(NSString *_Nonnull)string {
    return [self stringByAppendingString:string];
}

/*
 * 返回一个富文本
 * dic 富文本属性
 * sub 需要特殊化的文本
 */
- (NSAttributedString *_Nonnull)ats:(NSDictionary *_Nonnull)dic sub:(NSString *_Nonnull)sub {
    NSMutableAttributedString *mats = [[NSMutableAttributedString alloc] initWithString:self];
    NSRange rg = [self rangeOfString:sub];
    [mats setAttributes:dic range:rg];
    return mats.copy;
}

/*
 * 返回中英文首字母
 */
- (NSString *_Nonnull)char_first {
    NSMutableString *mstr = [NSMutableString stringWithString:self];
    CFStringTransform((CFMutableStringRef)mstr, NULL, kCFStringTransformToLatin, NO);
    CFStringTransform((CFMutableStringRef)mstr, NULL, kCFStringTransformStripDiacritics, NO);
    NSString *py = [mstr capitalizedString];
    return [py substringToIndex:1];
}

/*
 * 返回带图片的富文本
 * imgName 图片名称
 */
- (NSMutableAttributedString *_Nonnull)ats:(NSString *_Nonnull)imgName {
    NSMutableAttributedString *mats = [[NSMutableAttributedString alloc] initWithString:self];
    NSTextAttachment *tat = [[NSTextAttachment alloc] init];
    tat.image = [UIImage imageNamed:imgName];
    tat.bounds = CGRectMake(0, 0, tat.image.size.width, tat.image.size.height);
    NSAttributedString *tatString = [NSAttributedString attributedStringWithAttachment:tat];
    [mats appendAttributedString:tatString];
    return mats;
}

3. NSObject

/*
 * 获得所有属性名称
 */
- (NSArray *)propertys_name {
    u_int count;
    objc_property_t *ptys = class_copyPropertyList([self class], &count);
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        const char *ptyName = property_getName(ptys[i]);
        [marr addObject:[NSString stringWithUTF8String:ptyName]];
    }
    return marr;
}
/*
 * 获得所有属性类型
 */
- (NSArray *)propertys_type {
    u_int count;
    objc_property_t *ptys = class_copyPropertyList([self class], &count);
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        const char *type_c = property_getAttributes(ptys[i]);
        NSString *type_obj = [NSString stringWithUTF8String:type_c];
        type_obj = [type_obj componentsSeparatedByString:@"\","].firstObject;
        type_obj = [type_obj componentsSeparatedByString:@"T@\""].lastObject;
        [marr addObject:NSClassFromString(type_obj)];
    }
    return marr;
}

/*
 * 处理赋值时为空的情况
 * ps 若为空 返回
 * 1.@[]
 * 2.@{}
 * 3.@""
 */
//1.
- (NSArray *)arr:(id)oArr {
    NSArray *arr = @[];
    if (oArr != nil && oArr != [NSNull null] && [oArr isKindOfClass:[NSDictionary class]]) {
        arr = oArr;
    }
    return arr;
}
//2.
- (NSDictionary *)dic:(id)oDic {
    NSDictionary *dic = @{};
    if (oDic != nil && oDic != [NSNull null] && [oDic isKindOfClass:[NSDictionary class]]) {
        dic = oDic;
    }
    return dic;
}
//3.
- (NSString *)str:(id)oStr _default:(NSString *)df {
    NSCAssert(df, @"df can't be nil");
    NSString *str = df;
    if (oStr != nil && oStr != [NSNull null]) {
        str = [NSString stringWithFormat:@"%@",oStr];
    }
    return str;
}


/*
 * 从json中获取数据
 * sc : 获取成功 主线程
 * ps 以下两方法只能获取在mainBundle中的数据
 */
/*-------global--------*/
- (void)json_arr:(NSString *)name success:(void(^)(NSArray *arr))sc {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        id obj = [self bundle_obj:name type:@"geojson"];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (sc) {
                sc([self arr:obj]);
            }
        });
    });
}

- (void)json_dic:(NSString *)name success:(void(^)(NSDictionary *dic))sc {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        id obj = [self bundle_obj:name type:@"geojson"];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (sc) {
                sc([self dic:obj]);
            }
        });
    });
}

/*---------main-----------*/
- (void)main_json_arr:(NSString *)name success:(void(^)(NSArray *arr))sc {
    id obj = [self bundle_obj:name type:@"geojson"];
    if (sc) {
        sc([self arr:obj]);
    }
}

- (void)main_json_dic:(NSString *)name success:(void(^)(NSDictionary *dic))sc {
    id obj = [self bundle_obj:name type:@"geojson"];
    if (sc) {
        sc([self dic:obj]);
    }
}

/*
 * 从plist中获取数据
 * sc : 获取成功 主线程
 * ps 以下两方法只能获取在mainBundle中的数据
 */
/*---------global--------------*/
- (void)plist_arr:(NSString *)name success:(void(^)(NSArray *arr))sc {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        id obj = [self bundle_obj:name type:@"plist"];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (sc) {
                sc([self arr:obj]);
            }
        });
    });
}

- (void)plist_dic:(NSString *)name success:(void(^)(NSDictionary *dic))sc {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        id obj = [self bundle_obj:name type:@"plist"];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (sc) {
                sc([self dic:obj]);
            }
        });
    });
}

/*---------main-----------*/
- (void)main_plist_arr:(NSString *)name success:(void(^)(NSArray *arr))sc {
    id obj = [self bundle_obj:name type:@"plist"];
    if (sc) {
        sc([self arr:obj]);
    }
}

- (void)main_plist_dic:(NSString *)name success:(void(^)(NSDictionary *dic))sc {
    id obj = [self bundle_obj:name type:@"plist"];
    if (sc) {
        sc([self dic:obj]);
    }
}

- (id)bundle_obj:(NSString *)name type:(NSString *)type {
    NSString *pth = [[NSBundle mainBundle] pathForResource:name ofType:type];
    NSData *data = [NSData dataWithContentsOfFile:pth];
    id plistObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    return plistObj;
}

/*
 * 从txt文件中获取文字
 * sc : 获取成功 主线程
 * ps 以下方法只能获取在mainBundle中的数据
 */
/*-----global-----*/
- (void)txt:(NSString *)name success:(void(^)(NSString *str))sc {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        id obj = [self bundle_obj:name type:@"txt"];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (sc) {
                sc([self str:obj _default:@""]);
            }
        });
    });
}

/*---------main-----------*/
- (void)main_txt:(NSString *)name success:(void(^)(NSString *str))sc {
    id obj = [self bundle_obj:name type:@"txt"];
    if (sc) {
        sc([self str:obj _default:@""]);
    }
}
分享到:
我来说两句
facelist
您需要登录后才可以评论 登录 | 立即注册
所有评论(0)
关闭

每日头条

通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

返回顶部