Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios Swift fatal error: use of unimplemented initializer 'init()'

I've been trying very hard, have looked up every similar question pertaining to this issue on StackOverflow and trying them to no avail.

class TimeLineTableViewController:      UITableViewController,        UIImagePickerControllerDelegate,     UINavigationControllerDelegate  {          var timelineData = [PFObject]()               required init(coder aDecoder: NSCoder) {         super.init(coder: aDecoder)     }          override func viewDidLoad() {         super.viewDidLoad()         self.loadData()          }      @IBAction func loadData(){        timelineData.removeAll(keepCapacity: false)                 var findTimelineData:PFQuery = PFQuery(className:"timelineMessages")        findTimelineData.findObjectsInBackgroundWithBlock { (objects:[AnyObject]! , error:NSError!) -> Void in             if error == nil {                 self.timelineData = objects.reverse() as [PFObject]                 //let array:NSArray = self.timelineData.reverseObjectEnumerator().allObjects                 // self.timelineData = array as NSMutableArray                 self.tableView.reloadData()             }         }     }      override func viewDidAppear(animated: Bool) {         var footerView:UIView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))         self.tableView.tableFooterView = footerView                  var logoutButton:UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton         logoutButton.frame = CGRectMake(20, 10, 50, 20)         logoutButton.setTitle("Logout", forState: UIControlState.Normal)         logoutButton.addTarget(self, action:"logout:", forControlEvents: UIControlEvents.TouchUpInside)                  footerView.addSubview(logoutButton)     }  

To clarify, timelineTableViewController has one class that inherits, MessageTableCell. It's also part of a project that I've integrated into Objective-C code, so it's a combination of both Swift and ObjC. I've run both projects (the swift one and the ObjC one) independently and they work fine; it's only when I try to run it together do they mess up. Any suggestions? I'm at an utter loss for this.

like image 884
Tam Avatar asked Jan 28 '15 07:01

Tam


Video Answer


1 Answers

“Unlike subclasses in Objective-C, Swift subclasses do not inherit their superclass initializers by default.”

Automatic Initializer Inheritance

  • Rule 1: If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.
  • Rule 2: If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/tw/jEUH0.l

Since you have override the init(coder aDecoder: NSCoder), TimeLineTableViewController won't have the init() initiailzer.

You can provide an implementation of all of its superclass designated initialisers like this

override init() {         super.init() }  override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {         super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) }  required init(coder aDecoder: NSCoder) {         super.init(coder: aDecoder) } 

, or just delete the implementation of init(coder aDecoder: NSCoder).

like image 181
ylin0x81 Avatar answered Oct 02 '22 17:10

ylin0x81