簡體   English   中英

如何使用非可選屬性對UITableViewController進行子類化

[英]How to subclass UITableViewController with non-optional property

如果我這樣設置UITableViewController:

class FolderTableViewController: UITableViewController {

    var rootFolder:String

    init(path:String) {

        rootFolder = path        
        super.init(style: .Plain)

    }
}

然后我得到一個運行時錯誤: fatal error: use of unimplemented initializer 'init(nibName:bundle:)'

如果我添加了初始化程序init(nibName:bundle:) ,那么我會得到property 'self.rootFolder' not initialized at super.init call的編譯時錯誤property 'self.rootFolder' not initialized at super.init call

...但是我無法在該方法中設置字符串-字符串值僅在我自己的初始化器中可用,並且實際上已經設置了。

我是否缺少有關應該如何工作的信息,或者這是一個錯誤? 我能想到的唯一可能的解決方案是使該屬性為可選。

謝謝你提供的所有幫助。

如果您想快速消除錯誤,可以執行以下操作:

var rootFolder:String = ""

但是,如果您想完全理解為什么會收到該錯誤,則應閱讀Swift文檔的以下部分: https : //developer.apple.com/library/prerelease/iOS/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html

這也是Ash Furrow在Swift Initializers上的精彩文章: http : //ashfurrow.com/blog/swift-initializers

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM