美文网首页
iOS - 可以直接用的小代码

iOS - 可以直接用的小代码

作者: CDLOG | 来源:发表于2018-08-21 11:46 被阅读10次

    避免方法重复执行

    //不带参数
    [[self class] cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(text111)  afterDelay:0.3];
    //带参数
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(requstRootDataSourceforFileDircevory:) object:models.fileDirId];
    [self performSelector:@selector(requstRootDataSourceforFileDircevory:) withObject:models.fileDirId afterDelay:0.3];
    
    

    1,打印bool值

    NSLog(@"ifReadOnly value: %@" ,ifReadOnly?@"YES":@"NO");
    

    2,拨打电话

    NSString * number = [NSString stringWithFormat:@"tel://%@",self.lclModel.phoneNo];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
    

    3,验证码输入

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
    //验证码的位数
        NSInteger count = 6;
    //    得到输入的字符串
        NSString *aStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (textField == self.enterCodeText) {
            //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
            if (range.length == 1 && string.length == 0) {
                self.nextPage.enabled = YES;
                [self.nextPage setBackgroundColor:[UIColor grayColor]];
                return YES;
            }else if (aStr.length>=count){
                self.nextPage.enabled = YES;
                self.enterCodeText.text = [aStr substringToIndex:count];
                 [self.nextPage setBackgroundColor:color_green];
                return NO;
            }
        }
        return [self validateNumber:string];
    }
    - (BOOL)validateNumber:(NSString*)number {
        
        BOOL res = YES;
        NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        int i = 0;
        while (i < number.length) {
            NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
            NSRange range = [string rangeOfCharacterFromSet:tmpSet];
            if (range.length == 0) {
                res = NO;
                break;
            }
            i++;
        }
        return res;
    }
    

    iOS中对字典的key进行排序

    // 将key排序
    NSArray *keys = dictionary.allKeys;
    keys = [keys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSComparisonResult result = [obj1 compare:obj2];
        return result == NSOrderedDescending;
    }];
    

    data转化字典

    NSDictionary *dict =  [NSJSONSerialization JSONObjectWithData:responseObject options:nil error:nil];
    

    字典转json

    // 字典转json字符串方法
    -(NSString *)convertToJsonData:(NSDictionary *)dict
    {
        
        NSError *error;
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
        
        NSString *jsonString;
        
        if (!jsonData) {
            
            NSLog(@"%@",error);
            
        }else{
            
            jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
            
        }
        
        NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
        
        NSRange range = {0,jsonString.length};
        
        //去掉字符串中的空格
        
        [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
        
        NSRange range2 = {0,mutStr.length};
        
        //去掉字符串中的换行符
        
        [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
        return mutStr;
        
    }
    

    输入即时搜索

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beiginSearch) name:UITextFieldTextDidChangeNotification object:self.searchText];
    -(void)beiginSearch{
        if (textField.markedTextRange == nil) {
            NSLog(@"需要搜索的内容:%@", self.searchText);
        }
    }
    
    

    masonry动画

    //告知需要更改约束
            [self.view setNeedsUpdateConstraints];
            [UIView animateWithDuration:0.3 animations:^{
                [self.baseTableView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.offset(40);
                    
                }];
                //告知父类控件绘制,不添加注释的这两行的代码无法生效
                [self.view layoutIfNeeded];
            }];
    

    相关文章

      网友评论

          本文标题:iOS - 可以直接用的小代码

          本文链接:https://www.haomeiwen.com/subject/rvjibftx.html