ios 类没有初始化器 Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27797351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class has no initializers Swift
提问by Kevin Py
I have a problem with Swift class. I have a swift file for UITableViewController class and UITableViewCell class. My problem is the UITableViewCell class, and outlets. This class has an error Class "HomeCell" has no initializers, and I don't understand this problem.
我对 Swift 类有问题。我有一个 UITableViewController 类和 UITableViewCell 类的 swift 文件。我的问题是 UITableViewCell 类和插座。这个类有一个错误Class "HomeCell" has no initializers,我不明白这个问题。
Thanks for your responses.
感谢您的回复。
import Foundation
import UIKit
class HomeTable: UITableViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableViex: UITableView!
var items: [(String, String, String)] = [
("Test", "123", "1.jpeg"),
("Test2", "236", "2.jpeg"),
("Test3", "678", "3.jpeg")
]
override func viewDidLoad() {
super.viewDidLoad()
var nib = UINib(nibName: "HomeCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "bookCell")
}
// Number row
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
// Style Cell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("bookCell") as UITableViewCell
// Style here
return cell
}
// Select row
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Select
}
}
// PROBLEM HERE
class HomeCell : UITableViewCell {
@IBOutlet var imgBook: UIImageView
@IBOutlet var titleBook: UILabel
@IBOutlet var pageBook: UILabel
func loadItem(#title: String, page: String, image:String) {
titleBook.text = title
pageBook.text = page
imgBook.image = UIImage(named: image)
}
}
回答by mprivat
You have to use implicitly unwrapped optionals so that Swift can cope with circular dependencies (parent <-> child of the UI components in this case) during the initialization phase.
您必须使用隐式解包的选项,以便 Swift 可以在初始化阶段处理循环依赖项(在这种情况下,UI 组件的父级 <-> 子级)。
@IBOutlet var imgBook: UIImageView!
@IBOutlet var titleBook: UILabel!
@IBOutlet var pageBook: UILabel!
Read this doc, they explain it all nicely.
阅读这个文档,他们很好地解释了这一切。
回答by Byron Coetsee
Quick fix - make sure all variables which do not get initialized when they are created (eg var num : Int?
vs var num = 5
) have either a ?
or !
.
快速修复 - 确保所有在创建时未初始化的变量(例如var num : Int?
vs var num = 5
)都具有 a?
或!
.
Long answer (reccomended) - read the docas per mprivat suggests...
长答案(推荐) -按照 mprivat 的建议阅读文档...
回答by CodeHelp
This is from Apple doc
这是来自苹果文档
Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
类和结构必须在创建该类或结构的实例时将其所有存储的属性设置为适当的初始值。存储的属性不能处于不确定状态。
You get the error message Class "HomeCell" has no initializersbecause your variables is in an indeterminate state. Either you create initializers or you make them optional types, using ! or ?
您收到错误消息Class "HomeCell" has no initializers因为您的变量处于不确定状态。要么创建初始值设定项,要么使用 ! 或者 ?
回答by Honey
My answer addresses the error in general and not the exact code of the OP. No answer mentioned this note so I just thought I add it.
我的回答一般解决了错误,而不是 OP 的确切代码。没有答案提到这个注释,所以我只是想我添加了它。
The code below would also generate the sameerror:
下面的代码也会产生同样的错误:
class Actor {
let agent : String? // BAD! // Its value is set to nil, and will always be nil and that's stupid so Xcode is saying not-accepted.
// Technically speaking you have a way around it, you can help the compiler and enforce your value as a constant. See Option3
}
Others mentioned that Either you create initializers or you make them optional types, using ! or ?which is correct. However if you have an optional member/property, that optional should be mutable ie var
. If you make a let
then it would neverbe able to get out of its nil
state. That's bad!
其他人提到,要么创建初始值设定项,要么使用 ! 或者 ?哪个是正确的。但是,如果您有一个可选成员/属性,则该可选成员应该是可变的,即var
. 如果你制作 alet
那么它永远无法摆脱它的nil
状态。那很糟!
So the correct way of writing it is:
所以正确的写法是:
Option1
选项1
class Actor {
var agent : String? // It's defaulted to `nil`, but also has a chance so it later can be set to something different || GOOD!
}
Or you can write it as:
或者你可以把它写成:
Option2
选项2
class Actor {
let agent : String? // It's value isn't set to nil, but has an initializer || GOOD!
init (agent: String?){
self.agent = agent // it has a chance so its value can be set!
}
}
or default it to any value (including nil
which is kinda stupid)
或将其默认为任何值(包括nil
有点愚蠢的值)
Option3
选项3
class Actor {
let agent : String? = nil // very useless, but doable.
let company: String? = "Universal"
}
If you are curious as to why let
(contrary to var
) isn't initialized to nil
then read hereand here
回答by Anurag Sharma
In my case I have declared a Bool
like this:
就我而言,我已经声明了Bool
这样的:
var isActivityOpen: Bool
i.e. I declared it without unwrapping so, This is how I solved the (no initializer) error :
即我在没有解包的情况下声明了它,这就是我解决(没有初始化程序)错误的方法:
var isActivityOpen: Bool!
回答by Sonu VR
Not a specific answer to your question but I had got this error when I hadn't set an initial value for an enum while declaring it as a property. I assigned a initial value to the enum to resolve this error. Posting here as it might help someone.
不是您问题的具体答案,但是当我在将枚举声明为属性时没有为枚举设置初始值时,我收到了此错误。我为枚举分配了一个初始值以解决此错误。在这里发帖,因为它可能会帮助某人。
回答by anoopbryan2
simply provide the init block for HomeCellclass
只需为HomeCell类提供 init 块
it's work in my case
在我的情况下是有效的