1. 程式人生 > >常用開發技巧系列(六)

常用開發技巧系列(六)

一: 關於UIColor 自己在平時的工作中用到幾個比較好的UIColor的類別,分享出來:

      1、 UIColor 初始化關於 Hex (16進位制的可以的)  比如我們 #FFFFFF 等怎麼初始化一個UIColor,在iOS中是沒有直接的方法初始化的,所以很多時候安卓同學用16進位制的iOS的還要RGB就會很麻煩,下面是根據兩個類別方法,OC版本的:

+ (UIColor *)colorWithRGBHex:(UInt32)hex
{
	int r = (hex >> 16) & 0xFF;
	int g = (hex >> 8) & 0xFF;
	int b = (hex) & 0xFF;
	
	return [UIColor colorWithRed:r / 255.0f
						   green:g / 255.0f
							blue:b / 255.0f
						   alpha:1.0f];
}

+ (UIColor *)colorWithHexString:(NSString *)stringToConvert{
    
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];

	NSScanner *scanner = [NSScanner scannerWithString:cString];
	unsigned hexNum;
	if (![scanner scanHexInt:&hexNum]) return nil;
	return [UIColor colorWithRGBHex:hexNum];
}

      簡單的使用如下:

self.backgroundColor = [UIColor colorWithHexString:@"#F1F1F1"];

label.textColor = [UIColor colorWithRGBHex:0x707070];

      2、Swift 版本的:

/// 自定義初始化方法
///
/// - Parameters:
///   - r: red
///   - g: green
///   - b: blue
///   - a: alpha
convenience init(r:UInt32 ,g:UInt32 , b:UInt32 , a:CGFloat = 1.0) {
    self.init(red: CGFloat(r) / 255.0,
              green: CGFloat(g) / 255.0,
              blue: CGFloat(b) / 255.0,
              alpha: a)
}
    
/// 自定義初始化方法
///
/// - Parameters:
///   - hexString: 6位字元 'F1F1F1'
///   - alpha: 透明度值 0.0-1.0
convenience init(hexString: String, alpha:CGFloat = 1.0) {
        
   var r: UInt32 = 0x0
   var g: UInt32 = 0x0
   var b: UInt32 = 0x0
        
   var cString: String = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
   if cString.count < 6 {
        self.init(red: CGFloat(r) / 255.0,
                  green: CGFloat(g) / 255.0,
                  blue: CGFloat(b) / 255.0,
                  alpha: alpha)
        return
    }
        
    let index = cString.index(cString.endIndex, offsetBy: -6)
    let subString = cString[index...]
    if cString.hasPrefix("0X") { cString = String(subString) }
    if cString.hasPrefix("#") { cString = String(subString) }
        
    if cString.count != 6 {
        self.init(red: CGFloat(r) / 255.0,
                  green: CGFloat(g) / 255.0,
                  blue: CGFloat(b) / 255.0,
                  alpha: alpha)
        return
    }
        
    var range: NSRange = NSMakeRange(0, 2)
    let rString = (cString as NSString).substring(with: range)
    range.location = 2
    let gString = (cString as NSString).substring(with: range)
    range.location = 4
    let bString = (cString as NSString).substring(with: range)
        
    Scanner(string: rString).scanHexInt32(&r)
    Scanner(string: gString).scanHexInt32(&g)
    Scanner(string: bString).scanHexInt32(&b)
        
    self.init(red: CGFloat(r) / 255.0,
               green: CGFloat(g) / 255.0,
               blue: CGFloat(b) / 255.0,
               alpha: alpha)
}

      它的使用也是很簡單的如下:

self.backgroundColor = UIColor.init(hexString: "F9F9F9")

       3、在給一個單色取RGB的UIColor類別方法 OC版本的 Swift的有需要可以自己嘗試著寫一下哈:

// 顏色模式
- (CGColorSpaceModel)colorSpaceModel {

	return CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
}


- (NSString *)colorSpaceString {

	switch (self.colorSpaceModel) {

		case kCGColorSpaceModelUnknown:
			return @"kCGColorSpaceModelUnknown";
		case kCGColorSpaceModelMonochrome:
			return @"kCGColorSpaceModelMonochrome";
		case kCGColorSpaceModelRGB:
			return @"kCGColorSpaceModelRGB";
		case kCGColorSpaceModelCMYK:
			return @"kCGColorSpaceModelCMYK";
		case kCGColorSpaceModelLab:
			return @"kCGColorSpaceModelLab";
		case kCGColorSpaceModelDeviceN:
			return @"kCGColorSpaceModelDeviceN";
		case kCGColorSpaceModelIndexed:
			return @"kCGColorSpaceModelIndexed";
		case kCGColorSpaceModelPattern:
			return @"kCGColorSpaceModelPattern";
		default:
			return @"Not a valid color space";
	}
}

// 判斷能不能取
- (BOOL)canProvideRGBComponents {
	switch (self.colorSpaceModel) {
		case kCGColorSpaceModelRGB:
		case kCGColorSpaceModelMonochrome:
			return YES;
		default:
			return NO;
	}
}


// 只有單色能取出RGB,要不是就不能
- (NSArray *)arrayFromRGBAComponents {
	NSAssert(self.canProvideRGBComponents, @"Must be an RGB color to use -arrayFromRGBAComponents");

	CGFloat r,g,b,a;
	if (![self red:&r green:&g blue:&b alpha:&a]) return nil;
	
	return  [NSArray arrayWithObjects:
			[NSNumber numberWithFloat:r],
			[NSNumber numberWithFloat:g],
			[NSNumber numberWithFloat:b],
			[NSNumber numberWithFloat:a],
			nil];
}

 

二:這個Xcode錯誤

error: Cycle inside ****(這裡是你專案的名稱); building could produce unreliable results. This usually can be resolved by moving the shell script phase 'h' so that it runs before the build phase that depends on its outputs.

Cycle details:

→ That command depends on command in Target '****': script phase “[CP] Copy Pods Resources”

○ That command depends on command in Target '****': script phase “[CP] Copy Pods Resources”

 

Xcode10和Xcode9的不同引發的問題,具體的解決辦法下面的文章說的比較清楚,我自己修改的時候是按下面圖所示:

 

修改了Build Systen 為 Legacy Build System 不用Xcode的 Default模式      解決辦法:升級Xcode10問題集

 

三:關於蘋果開發者賬號的支付問題

      單標卡到底能不能用這個東西我以前也沒有確認過,不過最近通過官方渠道確認了一下,在購買開發者賬號的時候我們可以看到支付型別就一個visa或者MasterCard兩種,然後我們就會找帶這兩個標識的信用卡來支付,然後有些成功了有些會遇到支付失敗的問題,其實就是這個單標雙標的問題或者有沒有開通國際支付功能引起的,還有網上有些可以使用蘋果支付的完全是沒有根據的,下面這兩張聊天的截圖可以把這個問題說清楚:

 

                                  

 

 四:  iOS 清楚.(點)和->(箭頭)的區別

      這兩個訪問方式在看一下第三方庫程式碼的時候應該會經常遇到的,在這裡做一個區分記錄:

      1、.(點語法)是訪問類的屬性,本質是呼叫set、get方法。

      2、->是訪問成員變數,但成員變數預設受保護,所以常常報錯,手動設為public即可解決

 

五:  Swift版本的CGD定時器

      經常用經常忘記怎麼寫的我還得經常去查詢,記錄在這裡方便自己查詢

/// 測試
var timeCount = 0
let  timer =  DispatchSource.makeTimerSource(queue: DispatchQueue.global())
timer.schedule(deadline: .now(), repeating: .seconds(2))
/// 設定時間源的觸發事件
timer.setEventHandler(handler: {
               
      timeCount += 1
      if timeCount == 200 {
          timer.cancel()
      }
      print("--------------",timeCount)
      // 返回主執行緒處理一些事件,更新UI等等
      DispatchQueue.main.async {
         
      }
})
/// 啟動時間源
timer.resume()

 

六:Git 報錯了  You won't be able to pull or push project code via SSH until you add an SSH (在您的配置檔案中新增一個ssh金鑰之前,您將無法通過ssh來拖動或推動專案程式碼)

      這個問題就是SSH配置的問題,具體的解決辦法就是你配置好本地的SSH然後把它填寫到Gitlab就可以了,具體的按下面走:

      1、cd ~/.ssh  進來之後 ls -a  檢查一下有沒有生成Key (要是沒有一個 .pub)的檔案那就是沒有Key

      2、ssh-keygen -t rsa -C "你公司在GITLAB上的郵箱地址"  生成key (注意下這個冒號 以免 dquote ) 

      3、第一次輸入檔名   第二次輸入密碼    第三次確認密碼   OK  

      4、 cat   你輸入的檔名.pub  (檢視這個pub檔案)  

      5、貼上複製你需要的key  (全部複製包括 ssh 開頭資訊)

 

七:Git fatal: Authentication failed for '你的專案git地址'

      這時候一般不要懷疑就是許可權出問題了  In a case you entered incorrect password, please update it in Keychain Acces  要是出現這個那就是賬號密碼有問題

      當你賬號密碼有問題的時候你你可以這樣解決

      1、你要是已經clone下來了, cd 到你的專案下面,按下面所示

 

       2、你要有別的正常的專案你可以進來看一下這個地方怎麼寫的,一般都是   url = http:// 賬號:密碼@你的賬目地址  然後儲存退出再試試

       3、你要是沒有colne下來專案,直接這樣報錯了,那你其實可以  git config --system --unset credential.helper 從新填寫賬號密碼 也可以

       4、當然你也可以直接建立一個檔案之後進入到資料夾下面 直接 git clone 專案地址 

&n