iOS的一些常用代码(二)

文件操作

//文件路径获取
NSString *Dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = @"fileName";
NSString *filePath = [NSString stringWithFormat:@"%@/%@", Dir,fileName];
    
NSBundle *bundle = [NSBundle mainBundle];
NSString *bundleFilePath = [bundle pathForResource:@"chen" ofType:@"plist"];
NSData *data = [NSData dataWithContentsOfFile:bundleFilePath];
NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:bundleFilePath];

clang 命令使用

clang -rewrite-objc FileName会在目录下生成一个cpp文件

Literal Syntax

NSNumber *fortyTwo = @42;             // 等价于 [NSNumber numberWithInt:42]
NSNumber *fortyTwoUnsigned = @42U;    // 等价于 [NSNumber numberWithUnsignedInt:42U]
NSNumber *fortyTwoLong = @42L;        // 等价于 [NSNumber numberWithLong:42L]
NSNumber *fortyTwoLongLong = @42LL;   // 等价于 [NSNumber numberWithLongLong:42LL]
// 浮点数
NSNumber *piFloat = @3.141592654F;    // 等价于 [NSNumber numberWithFloat:3.141592654F]
NSNumber *piDouble = @3.1415926535;   // 等价于 [NSNumber numberWithDouble:3.1415926535]
// 布尔值
NSNumber *yesNumber = @YES;           // 等价于 [NSNumber numberWithBool:YES]
NSNumber *noNumber = @NO;             // 等价于 [NSNumber numberWithBool:NO]

//NSDictionary
NSDictionary *dic = @{};

//NSArray
NSArray *array = @[];

//NSMutableArray
NSMutableArray *mutableArray = [@[]mutableCopy];
mutableArray[0] = @"object0";
mutableArray[1] = @"object2";
[mutableArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSLog(@"%@",obj);
}];

//NSMutableDictionary
NSMutableDictionary *mutableDic = [@{} mutableCopy];
mutableDic[@0] = @"dic0";
mutableDic[@1] = @"dic1";
[mutableDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSLog(@"%@",key);
}];

判断当前是否插入耳机

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
    AVAudioSessionRouteDescription *des = [[AVAudioSession sharedInstance] currentRoute];
    NSArray *a = des.outputs;
    for (AVAudioSessionPortDescription *s in a) {
        if ([s.portType isEqualToString:AVAudioSessionPortHeadsetMic] || [s.portType isEqualToString:AVAudioSessionPortHeadphones]) {
            NSLog(@"耳机");
            return YES;
        }
    }
} else {
    CFStringRef route;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &route);
    if((route == NULL) || (CFStringGetLength(route) == 0)){
        // Silent Mode
        NSLog(@"AudioRoute: SILENT, do nothing!");
    } else {
        NSString* routeStr = (__bridge NSString*)route;
        NSLog(@"AudioRoute: %@", routeStr);
        /* Known values of route:
         * "Headset"
         * "Headphone"
         * "Speaker"
         * "SpeakerAndMicrophone"
         * "HeadphonesAndMicrophone"
         * "HeadsetInOut"
         * "ReceiverAndMicrophone"
         * "Lineout"
         */
        NSRange headphoneRange = [routeStr rangeOfString : @"Headphone"];
        NSRange headsetRange = [routeStr rangeOfString : @"Headset"];
        if (headphoneRange.location != NSNotFound) {
            return YES;
        } else if(headsetRange.location != NSNotFound) {
            return YES;  
        }  
    }
}
return NO;

获取类本身的弱引用

__weak SomeObjectClass *weakSelf = self;

线程等待

 dispatch_group_t group = dispatch_group_create();
 dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
      // 并行执行的线程一
 });
 dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
      // 并行执行的线程二
 });
 dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
      // 汇总结果
 });

UIBezierPath基本用法

根据一个矩形画曲线
+ (UIBezierPath *)bezierPathWithRect:(CGRect)rect
根据矩形框的内切圆画曲线
+ (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect
根据矩形画带圆角的曲线
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:
在矩形中,可以针对四角中的某个角加圆角, 一般用于设置某个视图的顶端两角为圆形
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
corners:枚举值,可以选择某个角
cornerRadii:圆角的大小
以某个中心点画弧线
参数:
center:弧线中心点的坐标
radius:弧线所在圆的半径
startAngle:弧线开始的角度值
endAngle:弧线结束的角度值
clockwise:是否顺时针画弧线
画二元曲线,一般和moveToPoint配合使用
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
参数:
endPoint:曲线的终点
controlPoint:画曲线的基准点
以三个点画一段曲线,一般和moveToPoint配合使用
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
参数:
endPoint:曲线的终点
controlPoint1:画曲线的第一个基准点
controlPoint2:画曲线的第二个基准点
    原文作者:走路放慢脚步
    原文地址: https://segmentfault.com/a/1190000002402483
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞