一些实用的东西

精度问题

double a = 2.01;

double b = 1.02;

double c = a - b;

NSLog(@"%@",@(c));

if (c == 0.99){

NSLog(@"double 0.99"); // 不打印

}

if (c == 0.98999999999999976){

NSLog(@"double 0.98999999999999976");// 打印

}

if ([[NSString stringWithFormat:@"%g",c] isEqualToString:@"0.99"]){

NSLog(@"string 0.99");// 打印

}

Use the printf() function.

UITextfield 限制输入字数

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (range.length + range.location > textField.text.length) {
        return NO;
    }
    NSUInteger length = textField.text.length + string.length - range.length;
    return length <= 你要限制字数;
}

判断是否是汉字

for (int i = 0; i < self.identityInformationView.nameTextField.text.length; i++) {
    NSRange range = NSMakeRange(i, 1);
    NSString *subString = [self.identityInformationView.nameTextField.text substringWithRange:range];
    const char *cString = [subString UTF8String];
    if (strlen(cString) != 3) {
    // 是汉字
    }
}

判断后台返回的对象是否为空的判断方法

if (responseObject isEqual:[NSNull null])

跳转到一个tabBar上

TabBarViewController * tabBar = [[TabBarViewController alloc] init];
        [self presentViewController:tabBar animated:YES completion:^{
            
        }];

放大按钮的感应区

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    CGRect bounds = self.bounds;
    //若原热区小于44x44,则放大热区,否则保持原大小不变
    CGFloat widthDelta = MAX(88 - bounds.size.width, 0);
    CGFloat heightDelta = MAX(88 - bounds.size.height, 0);
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
    return CGRectContainsPoint(bounds, point);

}

NSArray-->数组 求和/平均值/最小值/最大值

NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];

CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];

CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];

CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];

CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];

去掉UItableview headerview黏性(sticky) 让tableView的头部可以悬浮

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

按钮点击获取当前的indexPath

hhhhCell*cell=(hhhhCell*)[[btn superview] superview];
    NSIndexPath*indexPath=[self.tableV indexPathForRowAtPoint:cell.center];
    NSLog(@"------%@",indexPath);

设置导航栏渐变

UIImage *image = [self drawLinearGradient];
[navigationBarAppearance setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
- (UIImage *)drawLinearGradient {
    CGRect rect = self.navigationBar.bounds;
    rect.size.height += 20;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = {0.0, 1.0};
    NSArray *colors = @[(__bridge id)(kNavigationLeftColor.CGColor), (__bridge id)(kNavigationRightColor.CGColor)];
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
    CGRect pathRect = CGPathGetBoundingBox(path.CGPath);
    
    CGPoint startPoint = CGPointMake(CGRectGetMinX(pathRect), CGRectGetMinY(pathRect));
    CGPoint endPoint = CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMinY(pathRect));
    
    CGContextSaveGState(context);
    CGContextAddPath(context, path.CGPath);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(context);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
    return image;
}

是否有麦克风权限

AVAuthorizationStatus statusAudio = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
if (statusAudio == AVAuthorizationStatusDenied) {
    NSLog(@"没有录音权限");
}
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    if (status == PHAuthorizationStatusDenied) {
        NSLog(@"没有相册权限");
    }
}];

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
CFShow(infoDictionary);

打印百分号和引号

NSLog(@"%%");
NSLog(@"\"");

定位权限

if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
    NSLog(@"没有定位权限");
}

摄像头权限

AVAuthorizationStatus statusVideo = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (statusVideo == AVAuthorizationStatusDenied) {
    NSLog(@"没有摄像头权限");
}

HTML去除标签&去除空格、换行符、制表符

-(NSString *)JJFilterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        [scanner scanUpToString:@"<" intoString:nil];
        [scanner scanUpToString:@">" intoString:&text];
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    return html;
}

IOS中忽略警告的三种方法

https://blog.csdn.net/zxw_xzr/article/details/72469110

你可能感兴趣的:(一些实用的东西)