属性作用域扩大到 extension , 防止
- private
的滥用
- fileprivate
组合在一起作为一个类型使用,优化代码结构
- &
可以追加
- Associated Type
约束语句
- Where
: Swift 3
- keyPath
Swift 4
- let barKeyPath = #keyPath(Foo.bar)
- var bar = foo.value(forKeyPath: barKeyPath)
- foo.setValue("BAR", forKeyPath: barKeyPath)
swift 4: 更安全,控制 value 的 anyObject 类型,并且不需要类继承 NSObject
- let barKeyPath = \Foo.bar
- var bar = foo[keyPath: barKeyPath]
- foo[keyPath: barKeyPath] = "BAR"
- let joke = """
- Q: Why does \(name) have \(n) \(character)'s in their name?
- A: I don't know, why does \(name) have \(n) \(character)'s in their name?
- Q: Because otherwise they'd be called \(punchline).
- """
让归档更简单只能, 继承 Codeable 协议的对象都可 实现 let encoded = try? JSONEncoder().encode(swift) 将 对象归档为Data,相反解档
- Encoding and Decoding
迁移顺序
。注意:(1) 当一个 workSpace 中有多个 target , 优先迁移多个 target,。有错误先不要着急改,等自动迁移完成后再一步步改错,否则改动会导致迁移其他 target 出错。(2) 如果用 cocoapods 管理的第三方库,自动迁移的时候会提示选择需要迁移的 framework, 因为 Swift 4.0 是兼容 Swift 3.2 的,考虑到有些第三方库没有及时迁移到 4.0 ,所以此时可以不勾选第三方库的迁移选项,仅迁移当前项目即可。
- automatic mergation
- Ambiguous reference to member '+'
号表达不明确,将表达式拆分
- +
可能会有多个原因,如果是 KVO 报此错误,是因为 Swift 4 使用了新的 KVO 方法。 参照 Swift 新的 KVO 方法
- accesses to 0x7f887f904430, but modification requires exclusive access
- // MARK: - Property
- fileprivate var _observation: NSKeyValueObservation?
- //MARK: - Lifecycle
- _observation = LCKXMPPActivity.sharedInstance.observe(\.status, options: [.initial, .old]) { (child, change) in
- //
- }
为
- status
中声明为
- LCKXMPPActivity
的属性
- @objc dynamic
遇到一个很很很奇怪的问题,
- @objc open dynamic var status: LCKXMPPStatus
对象如果不声明为全局的话,
- _observation
方法并不会被
- observe
的的改变而触发
- status
- NSAttriburedString
- Swift 3
- let attributes = [NSStrikethroughStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue,
- NSForegroundColorAttributeName: UIColor.kTitleColor4] as [String : Any]
- Swift 4
- let attributes: [NSAttributedStringKey: Any] = [NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue,
- NSAttributedStringKey.foregroundColor: UIColor.kTitleColor4]
的
- attributedString
统一通过
- key
调用,并且
- NSAttributedStringKey
接受的类型是
- NSAttributedString(string: <#T##String#>, attributes: <#T##[NSAttributedStringKey : Any]?#>)
类型
- [NSAttributedStringKey : Any]
-->
- swap(&a, &b)
- swapAt(Index:Int, Index: Int)
-->
- init(colorLiteralRed:green:blue:alpha:)
- init(red:green:blue:alpha:)
协议声明重复,查看当前所继承的协议是否有重复,或者查看子类是否又继承了一遍
- redundant conformance of XXX to protocol YYY
首先在设置里如图所示:
- The use of Swift 3 @objc inference in Swift 4 mode is deprecated.
属性,所以需要 swift 调用 OC 的地方手动加上
- @objc
.
- objc
- Multiple Closures with Trailing Closure Violation: Trailing closure syntax should not be used when passing more than one closure argument. (multiple_closures_with_trailing_closure)
尾随闭包的问题,参见资料: Apple
- warning:
- ObjectiveManager.shared.finishObjective(objectiveID: "2", keyResults: [], success: {
- //
- }) { _ in
- //
- }
- success:
- ObjectiveManager.shared.finishObjective(objectiveID: "2", keyResults: [], success: {
- //
- }, failure: { _ in
- //
- })
- Declarations in extensions cannot override yet
用户添加新方法,但不能修改原方法,所以讲
- override
的方法不能放在
- override
中. 父方法前加 @objc
- extension
修改
- This block declaration is not a prototype
set
- strict prototypes
或者 OC 的
- no
一个一个慢慢改吧,这里是混编 OC 的问题。 参照资料 stackoverflow
- block
所调用的方法前加
- No known class method for selector
- @objc
- 'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator.
的
- String
属性在 Swift 4 新的实现
- substring
如果是某一范围
- let newStr = str.substring(to: index)-->let newStr = String(str[.. < index])
参照资料:stackoverflow
- str[..<str.index(str.startIndex, offsetBy: 8)]
在 iOS 11 不再有默认值。解决方案: 设置
- estimatedSectionHeaderHeight
或者
- tableview.estimatedSectionHeaderHeight = 0
代理返回 nil, footer 同理
- viewForFooterInSection
有些问题可能忘写了,以及不清楚的地方请留言讨论
持续更新,可以收藏关注先。。。
下一篇 iOS 11 以及 iPhone X 适配(11 已完成, X 还在挣扎中)
写这篇博客的时候,并没有女朋友喂我水果吃 = =
来源: https://juejin.im/post/5a02d932f265da43062a3446