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文件objective-c

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:畫曲線的第二個基準點
相關文章
相關標籤/搜索