swift程序员

Xcode10.2 升级swift3.x 到swift5遇到的问

2019-03-29  本文已影响38人  若雨千寻

3.25日苹果春季发布会之后Xcodes发布了10.2版本
之前一直用Xcode10.1,项目打包编译都正常,昨天运营同事升级Xcode之后,反映代码无法打包了,原来项目用到了Swift和OC混编,由于之前是Swift3.x写的,新版Xcode出来之后,最低支持Swift4.0,于是打开项目便遇到了一些问题:

1.提示The target “NewAirCloud” contains source code developed with Swift 3.x. This version of Xcode does not support building or migrating Swift 3.x targets.

Use Xcode 10.1 to migrate the code to Swift 4.

如下图:

提示.png
解决办法:在Build Settings找到 Swift Language Version 选择5.0(如果有同事用其它版本xcode选4.2)
选择swift版本.png
好了,可以编译了,但是汇报很多错误,大部分根据提示fix即可!

2.selector方法前需要添加@objc

Argument of '#selector' refers to instance method 'textDidChange()' that is not exposed to Objective-C
Add '@objc' to expose this instance method to Objective-C

3.通知名写法:之前写法如下图

通知写法

然后报错:
'UITextViewTextDidChange' has been renamed to 'UITextView.textDidChangeNotification'
Replace 'UITextViewTextDidChange' with 'UITextView.textDidChangeNotification'
打开通知中心头文件如下:


Snip20190328_5.png

于是尝试着写出UITextView.textDidChangeNotification发现类型匹配,更改即可


通知名.png

4 属性字符串的属性字典类型写法问题

Cannot subscript a value of type '[String : Any]' with an index of type 'NSAttributedString.Key'


Snip20190328_7.png

将[String : Any]改为[NSAttributedString.Key : Any]
NSForegroundColorAttributeName->NSAttributedString.Key.foregroundColor

5.'UIImageJPEGRepresentation' has been replaced by instance method 'UIImage.jpegData(compressionQuality:)'方法过期需要替换为实例方法

xxxImage.jpegData(compressionQuality: 0.5)

方法过期.png

6.Swift类在OC中无法找到方法和属性:

No known class method for selector 'xxx:'
Property 'xxx' not found on object of type 'xxxx *'

无法找到方法和属性.png
为了找到解决办法绞尽脑汁:
最后发现需要在oc需要调用的属性和方法前加:@objc
更改@objc.png
相关链接:https://blog.csdn.net/weixin_33785972/article/details/86829423
上一篇 下一篇

猜你喜欢

热点阅读