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
In my case I have declared a Bool
like this:
var isActivityOpen: Bool
i.e. I declared it without unwrapping so, This is how I solved the (no initializer) error :
var isActivityOpen: Bool!
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 !
.
Long answer (reccomended) - read the doc as per mprivat suggests...
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.
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.
@IBOutlet var imgBook: UIImageView!
@IBOutlet var titleBook: UILabel!
@IBOutlet var pageBook: UILabel!
Read this doc, they explain it all nicely.
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 initializers because your variables is in an indeterminate state. Either you create initializers or you make them optional types, using ! or ?
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.
The code below would also generate the same error:
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