0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Override, from method to stored property - Swift 3 調整記

Posted at

在 iOS 10 的 SDK 中,很多成員方法 (instance method) 都換成了成員變數 (instance variable, or stored property) 。

這樣在複寫父類別的方式就要改變了。

preferredStatusBarStyle 為例,在過去的寫法是這樣:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
}

但是這時候 Xcode 就會報錯,並抱怨說:

Method does not override any method from its superclass

意思是這個 method 已經從父類別移除。他也不是不見,而是以 property 的方式存在:

open var preferredStatusBarStyle: UIStatusBarStyle { get }

而不再以 method 的方式存在。

修改覆寫方式

這時候就要從覆寫方法,轉而覆寫一個成員變數:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

這樣即可。

小結

其實 Xcode 應該可以知道哪些覆寫的對象是從 方法 轉為 變數 ,要是碰到這一類型的變更,能提供更好的錯誤訊息的話會更好。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?