最近因为项目需求需要对项目代码进行升级,从之前的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