从Swift 2迁移到Swift 3期间的编译错误
警告部分
我想分享在迁移过程中发现并修复的编译器错误。
原始代码:OC。
Xcode转换:XC(当Xcode自动将代码从swift 2转换为swift 3时)
// OC Swift 2 让deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle(.Destructive,title:“ Remove”) {[弱自我]动作,indexPath in // XC 让deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle(rawValue:Int)!,标题:“ Remove”) {[弱自我]动作,indexPath in // Swift 3 让deleteAction = UITableViewRowAction(style: .destructive,title:“ Remove” ){[弱自我]动作,indexPath在
[String:(Format ,NSCache ,DiskCache)]→[String:(Format ,NSCache,DiskCache)]
//迅速2 var formats:[String:(Format , NSCache ,DiskCache)] = [:] // XC var formats:[String:(Format , NSCache ,DiskCache)] = [:]为! [String:(Format ,NSCache,DiskCache)] // Swift 3 var格式:[字符串:(Format , NSCache ,DiskCache)] = [:]
//迅速2 让ctx:CGContextRef = CGBitmapContextCreate(nil,Int(size.width),Int(size.height), CGImageGetBitsPerComponent(CGImage) ,0, CGImageGetColorSpace(CGImage) ,CGImageAlphaInfo.PremultipliedLast.rawValue)!// Swift 3 让ctx:CGContext = CGContext(数据:无,宽度:Int(size.width),高度:Int(size.height),bitsPerComponent: self.cgImage.bitsPerComponent ,bytesPerRow:0,空间: self.cgImage.colorSpace ,bitmapInfo :CGImageAlphaInfo.premultipliedLast.rawValue)!
//迅速2 静态var fgSessions = [String:NSURLSession]()// XC 静态var fgSessions = [String:Foundation.URLSession]()// Swift 3 静态var fgSessions:[String:Foundation.URLSession] = [:]
Swift 3决定禁止为函数类型使用参数标签。
解决方案:删除功能类型中的所有标签
//迅速2 dispatch_async_main {handler( result :SignupResponse(email:info.email,success:true,errorCode:nil,errorDescription:nil))} //迅速3 dispatch_async_main {handler(SignupResponse(email:info.email,success:true,errorCode:nil,errorDescription:nil))}}
// XC 让bytesPointer = UnsafeMutablePointer (valuePointer)//迅速3 让bytesPointer = UnsafeMutablePointer (OpaquePointer(valuePointer))
//迅速2 bylineView.dateLabel.date =日期// XC bylineView.dateLabel.date =日期为! NSDate //迅速3 bylineView.dateLabel.date =日期
//迅速2 let json = JSON(数据:sampleNS.data(使用:String.Encoding.utf8.rawValue)!)// Swift 3 做 { let json = 试试 JSON(数据:sampleNS.data(使用:String.Encoding.utf8.rawValue)!) dummy = DailyChallenge(json:json)} catch _ {}
参考:https://swiftunboxed.com/lang/closures-escaping-noescape-swift3/
//迅速2 func addRow(_ title:String,icon:UIImage ?, action:()-> Void ){ ...让hlite = HighlightedAction(view:rowView, handler:action ) } //迅速3 func addRow(_ title:String,icon:UIImage ?, action:@escaping()-> Void ){ ...让hlite = HighlightedAction(view:rowView,handler:action) }
//迅速2 numFollowersLabel.text = numberFormatter.string(来自: NSNumber (vendor!.memberCnt))! +“关注者” //迅速3 numFollowersLabel.text = numberFormatter.string(来自: NSNumber ( 值: vendor!.memberCnt))! +“关注者”
可选绑定仅在表达式右侧的结果为可选时有效。 如果右侧的结果不是可选的,则不能使用此可选绑定。
//迅速2 如果让path = self.path { 返回path.lowercased()。range(of:“ jpeg”)!= nil || path.lowercased()。range(of:“ jpg”)!=无|| path.lowercased()。range(of:“ gif”)!=无|| path.lowercased()。range(of:“ png”)!=无 } 其他{返回false} } // Swift 3 返回path.lowercased()。range(of:“ jpeg”)!=无|| path.lowercased()。range(of:“ jpg”)!=无|| path.lowercased()。range(of:“ gif”)!=无|| path.lowercased()。range(of:“ png”)!=无
隐式转换为桥接类型已被删除。 在这种情况下,我们将需要添加显式强制转换以使其起作用。
//迅速2 让渐变= CGGradient(colorsSpace:CGColorSpaceCreateDeviceRGB(),颜色: [UIColor(hex:“ F8A147”)。cgColor,UIColor(hex:“ FD4C19”)。cgColor] ,位置:[ 0.0,1.0 ])// Swift 3 让渐变= CGGradient(colorsSpace:CGColorSpaceCreateDeviceRGB(), 颜色:[UIColor(hex:“ F8A147”)。cgColor, UIColor(hex:“ FD4C19”)。cgColor]作为CFArray, 位置:[0.0,1.0])
//迅速2 静态函数randomInt(_ range:Range = 1 ... 10 )-> Int {…} // Swift 3 静态函数randomInt(_ range:Range = 1 .. <10 )-> Int {…}
//迅速2 让restOfString = substringFromIndex(range.startIndex.advancedBy (skip) )//迅速3 让restOfString = self.substring( 来自:self.index(range.lowerBound,offsetBy:skip) )
//迅速2 让路径:CGMutablePath = CGMutablePath() CGPathMoveToPoint(path, nil ,0,height / 2) CGPathAddLineToPoint(path, nil ,frame.width,height / 2)// Swift 3 让路径:CGMutablePath = CGMutablePath() path.move(至:CGPoint(x:0,y:高度/ 2)) path.addLine(至:CGPoint(x:frame.width,y:高度/ 2))
//迅速2 CGContextSetLineDash (上下文,0,模式,pattern.count)// Swift 3 上下文? setLineDash (阶段:0,长度:模式)
// XC 让urlDecodedStr = sessionStr。 stringByRemovingPercentEncoding //迅速3 让urlDecodedStr = sessionStr。 removePercentEncoding
//迅速2 让字符串= NSString(数据:download.receivedData !,编码: String.Encoding.utf8 )// Swift 3 让字符串= NSString(数据:download.receivedData !,编码: String.Encoding.utf8.rawValue )
//迅速2 var isDirectory = ObjCBool(false)如果!FileManager.default.fileExists(atPath:dirURL.path,isDirectory:&isDirectory){...}如果!isDirectory {return nil} // Swift 3 var isDirectory = ObjCBool(false)如果!FileManager.default.fileExists(atPath:dirURL.path,isDirectory:&isDirectory){...}如果!isDirectory.boolValue {return nil}
//迅速2 tracker.send(builder.build()as [NSObject:AnyObject] )// XC tracker.send(builder.build()as! [AnyHashable:Any] )//迅速3 警卫让build =(builder.build() 作为NSDictionary)为? [AnyHashable:任何]其他{返回} tracker.send( build )
// XC 让错误= Error() 错误。 代码 // Swift 3 让错误= Error() 错误。 _码
//迅速2 var secresult = SecTrustResultType(kSecTrustResultInvalid) ... 如果secresult == UInt32(kSecTrustResultProceed)|| secresult == UInt32(kSecTrustResultUnspecified){...} -------------------------------------- ------------------------------ // XC var secresult = SecTrustResultType.invalid ...如果secresult == UInt32(SecTrustResultType.proceed)|| secresult == UInt32(SecTrustResultType.unspecified){...} ------------------------------------ -------------------------------- // Swift 3 var secresult = SecTrustResultType.invalid ...如果成功。 rawValue == UInt32( SecTrustResultType.proceed。rawValue )|| 秘密。 rawValue == UInt32( SecTrustResultType.unspecified。rawValue ){...}