常用基本方法

时间:2021-7-21 作者:qvyue

系统

//获取iPhone自带的语言种类
-(NSArray *)getlangeArr{    
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    return languages;
}

判空

//字符串判空
-(BOOL )strIsEmpty:(NSString *)str{
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmedStr = [str stringByTrimmingCharactersInSet:set];
    if (!trimmedStr.length) {
        return YES;
    }else if ([str isKindOfClass:[NSNull class]]) {
        return YES;
    }else if (str == nil || str == NULL || [str isEqualToString:@"(null)"] || [str isEqualToString:@"null"] ||[str isEqualToString:@""]) {
        return YES;
    }
    return NO;
}

//数组判空
-(BOOL)arrIsEmpty:(NSArray *)array{
    if (array == nil) {
        return YES;
    }
    if (array.count

基本类型操作及转换

//判断是否包含中文
- (BOOL)containChinese:(NSString *)str {
    for(int i=0; i 0x4e00 && a ]*)?)|(www.[a-zA-Z0-9\.\-]+\.([a-zA-Z]{2,4})(:\d+)?(/[a-zA-Z0-9\.\-~!@#$%^&*+?:_/=]*)?)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:urlStr
                                                options:0
                                                  range:NSMakeRange(0, [urlStr length])];
    
    //NSString *subStr;
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    
    for (NSTextCheckingResult *match in arrayOfAllMatches){
        NSString* substringForMatch;
        substringForMatch = [string substringWithRange:match.range];
        [arr addObject:substringForMatch];
    }
    return arr;
}

//data转string
- (NSString *)jsonToString:(id)data {
    if(data == nil) { return nil; }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
- (NSData *)UTF8Data:(NSData *)data {
    //保存结果
    NSMutableData *resData = [[NSMutableData alloc] initWithCapacity:data.length];
    NSData *replacement = [@"�" dataUsingEncoding:NSUTF8StringEncoding];
    uint64_t index = 0;
    const uint8_t *bytes = data.bytes;
    long dataLength = (long) data.length;
    while (index 

UI相关

//字符串名称转颜色,适用于自带的类型,如redColor
-(UIColor *)colorFromStr:(NSString *)colorStr{
    SEL backColorSel = NSSelectorFromString(colorStr);
    UIColor* backColor= nil;
    if ([UIColor respondsToSelector: backColorSel])
        backColor  = [UIColor performSelector:backColorSel];
    return backColor;
}

//16进制转颜色
- (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] 
声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。