iOS的一些常用程式碼(二)

走路放慢腳步發表於2014-12-02

檔案操作

//檔案路徑獲取
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:畫曲線的第二個基準點

相關文章