Swift新特性整理

Swift新特性整理,第1张

概述  编译警告 运行时警告会打印在控制台: ***Swift runtime: ClassName.swift:lineInFile:columnInLine: entrypoint -[ClassName methodName] generated by implicit @objc inference is deprecated and will be removed in Swift 4;
  编译警告 

运行时警告会打印在控制台:

***Swift runtime: Classname.swift:lineInfile:columnInline: entrypoint -[Classname methodname] generated by implicit @objc inference is deprecated and will be removed in Swift 4; add explicit @objc to the declaration to emit the Objective-C entrypoint in Swift 4 and suppress this message

同样:想要修复运行时警告,需要添加 @objc 修饰符到对应的方法或者符号。

运行时警告的常见原因:

在 OC 中使用 SEL 在 swift 中使用了 perform methods 在 OC 中使用了 performSelector methods 使用了 @IBOutlet 或者 @IBAction 其他修改 NSAttributedStringKey
swift3.xpublic init(string str: String,attributes attrs: [AnyHashable : Any]? = nil)swift4.0public init(string str: String,attributes attrs: [NSAttributedStringKey : Any]? = nil)
String

废弃characters

swift 3var count = string.characters.counterror'characters' is deprecated: Please use String or Substring directlyswift 4count = string.count

废弃addingPercentEscapes

swift 3var url = @"http://www.example.com?username=姓名"url = url.addingPercentEscapes(using: String.EnCoding.utf8)!error'addingPercentEscapes(using:)' is unavailable: Use addingPercentEnCoding(withAllowedCharacters:) instead,which always uses the recommended UTF-8 enCoding,and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valID.swift 4uri = uri.addingPercentEnCoding(withAllowedCharacters: CharacterSet.urlqueryAllowed)!

废弃substring(to:)

swift 3let index = tagText.index(tagText.startIndex,offsetBy: MPMultipleStyleListItemTagMaxLength)// 警告:'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator.let b = tagText.substring(to: index)Swift 4let a = tagText.prefix(upTo: index) //a 的类型是 Substring,不是 String
pod 引用

添加以下内容到 Podfile

post_install do |installer|    installer.pods_project.targets.each do |target|        if ['WTCarouselFlowLayout','XSLRevenue','OHhttpStubs/Swift'].include? target.name            target.build_configurations.each do |config|                config.build_settings['SWIFT_VERSION'] = '3.2'            end        end    endend
系统方法

UItableVIEwDelegate 协议方法名变更,没有错误提示:

// swift3.xfunc tableVIEw(_ tableVIEw: UItableVIEw,heightForRowAtIndexPath indexPath: IndexPath) -> CGfloat // swift4.0func tableVIEw(_ tableVIEw: UItableVIEw,heightForRowAt indexPath: IndexPath) -> CGfloat
总结

以上是内存溢出为你收集整理的Swift新特性整理全部内容,希望文章能够帮你解决Swift新特性整理所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1056663.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存