Class has no initializers Swift

IosUitableviewSwift

Ios Problem Overview


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.

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)
	}
	
}

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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][1] as per mprivat suggests...

[1]: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html#//apple_ref/doc/uid/TP40014097-CH20-XID_98 "doc"

Solution 3 - Ios

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 ?

Solution 4 - Ios

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🤓, 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 never be able to get out of its nil state. That's bad!

So the correct way of writing it is:

Option1

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

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) Option3

  • 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 here and here

Solution 5 - Ios

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!

Solution 6 - Ios

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.

Solution 7 - Ios

simply provide the init block for HomeCell class

it's work in my case

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin PyView Question on Stackoverflow
Solution 1 - IosmprivatView Answer on Stackoverflow
Solution 2 - IosByron CoetseeView Answer on Stackoverflow
Solution 3 - IosCodeHelpView Answer on Stackoverflow
Solution 4 - IosmfaaniView Answer on Stackoverflow
Solution 5 - IosAnurag SharmaView Answer on Stackoverflow
Solution 6 - IosdevView Answer on Stackoverflow
Solution 7 - Iosanoop ghildiyalView Answer on Stackoverflow