Basic functions

  • 抓取手機資訊

    • 語言
    let langStr = Locale.current.languageCode
    
    • 使用者設置的手機名稱
    let deviceName = UIDevice.currentDevice().name // 例如: 我的手機
    
    • 系統名稱
    let sysName = UIDevice.currentDevice().systemName // 例如: iPhone O
    
    • 作業系統版本
    let sysVersion = UIDevice.currentDevice().systemVersion // 例如:9.2
    
    • 裝置唯一識別碼
    let uuid = UIDevice.currentDevice().identifierForVendor?.UUIDString  // 例如:FBF2306E-A0D8-4F4B-BDED-9333B627D3E6
    
    • 設備型號
    let model = UIDevice.currentDevice().model // 例如:iPhone
    
  • 抓取App資訊

    let infoDic = NSBundle.mainBundle().infoDictionary 
    let appVersion = infoDic?["CFBundleShortVersionString"] // App的版本
    let appBuildVersion = infoDic?["CFBundleVersion"] // App的build版本
    let appName = infoDic?["CFBundleDisplayName"] // App的名稱
    
  • 獲取UUID

    • UIDevice.current.identifierForVendor!.uuidString
    • 現在的UUID是表示這個Vender在這台裝置上的unique ID,若是移除掉這Vender的所有App後再重新安裝App,UUID會不一樣
  • Guard

    guard 條件式 else {

    //當條件式為false要執行的程式

    return

    }

      由於`guard`只判斷`true or false`,要注意條件式不能回傳為`optional(true) or optional(false)`
    
  • 隱藏鍵盤

    使用Extension Class的方式來擴充隱藏鍵盤功能,可支援任意個ViewController。

  • 獲取時間的用法

    • 先獲取現在時間

      let now = NSDate()
      
    • 再轉成unix時間since1970

      let timeInterval:TimeInterval = now.timeIntervalSince1970
      
    • 再用Int轉成10位數

      let timeStamp = Int(timeInterval)
      
    • 轉成人類看得懂的格式

      let dformatter = DateFormatter()
      dformatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
      
    • 列印出來結果

      print("目前日期時間:\(dformatter.string(from: now as Date))")
      
    • DatePicker內的時間想要顯示成24小時制,不要設定DatePicker的locale屬性

  • 陣列的Search方法

    matchingItems = self.cafes.filter { term in
              return (term.name.contains(searchBarText) || term.address.contains(searchBarText))}
    
  • 取得時間的年、月、日

Calendar.current.component(.year, from: date) //年
Calendar.current.component(.month, from: date) //月
  • 比較某個物件是否為特定class
obj is UIView
  • 將圖片轉成NSData

UIImagePNGRepresentation(result.forwardImg as! UIImage)! as NSData

  • NSPredicate要使用Bool的方法
let predicate = NSPredicate(format: "isFinished == %@", NSNumber(booleanLiteral: false))
  • 給系統字型的方式
titleLabel.font = UIFont.boldSystemFont(ofSize: 24 * scale)
titleLabel.font = UIFont.systemFont(ofSize: 24 * scale, weight: UIFontWeightBold)
  • selector語法

    #selector(ViewController.getData(name:))

  • 讓使用者跳到此App在手機內的設定頁面

  • if #available(iOS 10.0, *) {
        UIApplication.shared.open(appSettings, options: [:], completionHandler: nil)
    }else {
        UIApplication.shared.openURL(appSettings)
    }
    
  • 給UI元件Constrain的方式
// item:子元件 toItem:父元件
NSLayoutConstraint(item: image, attribute: .topMargin, relatedBy: .equal, toItem: scrollView, attribute: .topMargin, multiplier: 1.0, constant: 20.0 * scale).isActive = true
  • 轉移到AppStore的評分頁面

    // 唯一需要改的就是 id+AppID (id1190355913)
    let openAppStoreForRating = "itms-apps://itunes.apple.com/gb/app/id1190355913?action=write-review&mt=8"
    if UIApplication.shared.canOpenURL(URL(string: openAppStoreForRating)!) {
       UIApplication.shared.openURL(URL(string: openAppStoreForRating)!)
    }
    
  • Textfield的placeholder要變換顏色

    • 先點選textfield,再選擇IB的Identity inspector
    • 然後在User Defined Runtime Attributes內新增一筆record
    • Key Path的值為:_placeholderLabel.textColor,Type選擇Color,Value就選顏色
  • 獲得高度

    • 螢幕高度
    UIScreen.main.bounds.size.height
    
    • Navigation高度
    (self.navigationController?.navigationBar.frame.size.height)!
    
    • TabBar高度
    (self.tabBarController?.tabBar.frame.height)!)
    

results matching ""

    No results matching ""