1. 程式人生 > >Swift2.0到Swift4.1程式碼轉換中的一些語法改變總結

Swift2.0到Swift4.1程式碼轉換中的一些語法改變總結

最近因為專案需求需要對專案程式碼進行升級,從之前的swift2.0版本升級到swift4.1版本。現將升級過程中遇到的一些語法變化與大家分享一下,希望會對大家有所幫助,

Swift 2.0 --> Swift 4.0

1.self.edgesForExtendedLayout = UIRectEdge.None  -->  self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)

2.UINavigationBar.appearance().titleTextAttributes=NSDictionary(object:UIColor.whiteColor(), forKey:NSForegroundColorAttributeName) as? [String : AnyObject]  -->  UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
[NSForegroundColorAttributeName: UIColor.white]  -->  [NSAttributedStringKey.foregroundColor : UIColor.white]

3.使用Alamofire獲取statusCode: res.result.error?.code  -->  res.response?.statusCode

4.CGRectZero  -->  CGRect.zero

5.從其他執行緒回到主執行緒的方法:
   dispatch_async(dispatch_get_main_queue(), { () -> Void in 

   })		--> 

   DispatchQueue.main.async {

   }

 6.	UILabel獲取字型大小的屬性boundingRectWithSize的改變:
 	let size = body.boundingRectWithSize(CGSizeMake(CGFloat(w-20), CGFloat(10000.0)),options:NSStringDrawingOptions.UsesLineFragmentOrigin,  attributes: [NSFontAttributeName:UIFont.systemFontOfSize(12)],context:nil)     -->
 	let size = body.boundingRect(with: CGSize.init(width: CGFloat(Constants.SCREEN_W - 20), height: CGFloat(10000.0)),options:NSStringDrawingOptions.usesLineFragmentOrigin,  attributes:[NSAttributedStringKey.font : UIFont.systemFont(ofSize: 12)],context:nil)

 7.let myMutableString = NSMutableAttributedString(string: (!body.isEmpty ? body : "") , attributes: [NSFontAttributeName:UIFont(name: "HelveticaNeue", size: 12.0)!])	 -->  
   let myMutableString = NSMutableAttributedString.init(string: (!body.isEmpty ? body : ""), attributes: [NSAttributedStringKey.font : UIFont(name: "HelveticaNeue", size: 12.0)!])
 
 8.let paragraphStyle = NSMutableParagraphStyle()
 	myMutableString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle,range: NSRange(location:0,length:myMutableString.length))   -->
 	myMutableString.addAttribute(NSAttributedStringKey.paragraphStyle, value: paragraphStyle, range: NSRange.init(location: 0, length: myMutableString.length))

 9.SDWebImage的用法改變:
   headImg.sd_setImageWithURL(NSURL(string: self.images[indexPath.row] as! String), placeholderImage: UIImage(named: "default_logo"))     -->   
   headImg.sd_setImage(with: NSURL.init(string: self.images[indexPath.row] as! String)! as URL, placeholderImage: UIImage(named: "default_logo"), options: .retryFailed, completed: nil)

10. UIApplication.sharedApplication().keyWindow?.windowLevel = UIWindowLevelStatusBar    -->   UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar

11. NSCalendarUnit.Day  -->   NSCalendar.Unit.day

12. var components = cal.components([NSCalendar.Unit.day, NSCalendar.Unit.month, NSCalendar.Unit.year, NSCalendar.Unit.weekOfYear, NSCalendar.Unit.hour, NSCalendar.Unit.minute, NSCalendar.Unit.second, NSCalendar.Unit.nanosecond], from: NSDate() as Date)
	components.setValue(components.month-3, forComponent: NSCalendar.Unit.month)  -->  components.setValue(components.month! - 3, for: .month)

13. formatter.stringFromDate(cal.dateFromComponents(components)!)  -->  formatter.string(from: cal.date(from: components)!)

14. string.characters.count  -->  string.count

15. string.stringByReplacingOccurrencesOfString(" ", withString: "")	-->	   string.replacingOccurrences(of: " ", with: "")

16. for i in str.characters {
	
	}         -->     
	for i in str {

	}

17. str.removeRange(index!)  -->   str.removeSubrange(index!)

18. let alertInputTelController = UIAlertController(title: title, message: "", preferredStyle: UIAlertControllerStyle.alert)
	alertInputTelController.addTextFieldWithConfigurationHandler {

	}	  -->   
	alertInputTelController.addTextField {

	}

19. addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange.init(location: startIndex!, length: 1))   -->  
	attrString.addAttribute(.foregroundColor, value: UIColor.red, range: NSRange.init(location: startIndex!, length: 1))

20. tf.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) != ""   --> 
	tf.text?.trimmingCharacters(in: NSCharacterSet.whitespaces) != ""     //檢測textField中輸入的字元去掉空格後是否為空的方法

21. kNewStoreInfo.setObject(tf.text!, forKey: key)   --> 
	kNewStoreInfo.setObject(tf.text!, forKey: key as NSCopying)

22. textView.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())    -->   
	textView.text.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)

23. //將內容同步寫到檔案中去(Caches資料夾下)
        let cachePath = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: .UserDomainMask)[0]
        let path = cachePath.URLByAppendingPathComponent("log.txt")
        appendText(path, string: "\(datestr) \(consoleStr)")     

        --> 

        let cachePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let path = cachePath.appendingPathComponent("log.txt")
        appendText(fileURL: path as NSURL, string: "\(datestr) \(consoleStr)")

24. FileManager.defaultManager().createFileAtPath(fileURL.path!, contents: nil, attributes: nil)  -->  
	FileManager.default.createFile(atPath: fileURL.path!, contents: nil, attributes: nil)

25. NSFileHandle.init(forWritingAtPath: fileURL.path!)   -->
	FileHandle.init(forWritingAtPath: fileURL.path!)

26. //找到末尾位置並新增
    fileHandle!.seekToEndOfFile()        
    fileHandle?.writeData(stringToWrite.dataUsingEncoding(NSUTF8StringEncoding)!)  -->
    fileHandle?.write(stringToWrite.data(using: String.Encoding.utf8)!)

27. string.componentsSeparatedByString(",")  -->   string.components(separatedBy: ",")

28. NSIndexPath(forRow: i, inSection: 0)  -->   NSIndexPath(row: i, section: 0)

29. NSComparisonResult.OrderedAscending  -->  ComparisonResult.orderedAscending

30. NSNotificationCenter.defaultCenter().postNotificationName("Notification_UploadImage", object: j)  -->
	NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Notification_UploadImage"), object: j)

31. NSNotificationCenter.defaultCenter().postNotificationName("Notification_UploadImage", object: "error", userInfo: nil)  -->
	NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Notification_UploadImage"), object: "error", userInfo: nil)

32. let startIndex =  Int("\(range.startIndex)") -->
	let startIndex =  Int("\(range.upperBound.encodedOffset)") - 1

33. // 單例模式
    class var sharedInstance: TreeNodeHelper {
        struct Static {
            static var instance: TreeNodeHelper?
            static var token: dispatch_once_t = 0
        }
        dispatch_once(&Static.token) { // 該函式意味著程式碼僅會被執行一次,而且此執行是執行緒同步
            Static.instance = TreeNodeHelper()
        }
        return Static.instance!
    }
    -->  

    //由於dispatch_once在Swift4.0也已經被廢棄   通過給DispatchQueue新增擴充套件實現
    extension DispatchQueue { 
    private static var _onceTracker = [String]()
    public class func once(token: String, block: () -> ()) {
        objc_sync_enter(self)
        defer {
            objc_sync_exit(self)
        }
        if _onceTracker.contains(token) {
            return
        }
        _onceTracker.append(token)
        block()
    }
    
    func async(block: @escaping ()->()) {
        self.async(execute: block)
    }
    
    func after(time: DispatchTime, block: @escaping ()->()) {
        self.asyncAfter(deadline: time, execute: block)
    }
}
// 單例模式
    class var sharedInstance: TreeNodeHelper {
        
        struct Static {
            static var instance: TreeNodeHelper?
            static let _onceToken = NSUUID().uuidString
        }
        //由於dispatch_once在Swift4.0也已經被廢棄  此處通過給DispatchQueue新增擴充套件實現
        DispatchQueue.once(token: Static._onceToken) { // 該函式意味著程式碼僅會被執行一次,而且此執行是執行緒同步
           Static.instance = TreeNodeHelper()
        }
        return Static.instance!
    }

34. let url = NSURL(string:urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!)  -->
	let url = NSURL(string: urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)

35. dispatch_async(DispatchQueue.global(0, 0), {
                    
    })    -->   
    DispatchQueue.global().async {

    }
 
36. 在Google Maps SDK中有三個常量來表示每種型別,此屬性必須設定成其中一個. 這些常量是:
	kGMSTypeNormal
	kGMSTypeTerrain
	kGMSTypeHybrid
	在使用時候的變化:
 	kGMSTypeNormal  -->  GMSMapViewType.normal

 	kGMSMarkerAnimationPop --> GMSMarkerAnimation.pop

37. let directionsData = NSData(contentsOfURL: directionsURL! as URL)  -->
	let directionsData = NSData(contentsOf: directionsURL as URL)

38. 使用Alamofire解析獲取返回的error code值方法:
	res.result.error?.code   -->   res.result.error?._code