Problems debugging initializer? - xcode

I have a debugging error on the following code:
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Here is all of my code:
class ViewController: UIViewController {
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
var button = UIButton.buttonWithType(.Custom) as UIButton
button.frame = CGRectMake(160, 100, 50, 50)
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.setImage(UIImage(named:"thumbsUp.png"), forState: .Normal)
button.addTarget(self, action: "thumbsUpButtonPressed", forControlEvents: .TouchUpInside)
view.addSubview(button)
}
func thumbsUpButtonPressed() {
println("thumbs up button pressed")
}
The error says:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
The debugger output says the following:
fatal error: init(coder:) has not been implemented: file...hi/ViewController.swift, line 14
I have had this error before and am unsure of how to fix it. The weird thing is, there was an error saying You don't have any initializers and had me put it in. It is giving me an error either way (with it or without it).
Any input and suggestions would be greatly appreciated.
Thanks in advance.

In your code, you trigger a "fatalError" instead of initializing. Replace
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
with
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

Related

How to initialize NSTableRowView subclass?

The compiler crashses on line 3 and I cant find information on NSTableRowView initializers anywhere
class ItemRowView: NSTableRowView {
convenience override init(frame: NSRect) {
self.init(frame: frame) // EXC BAD ACCESS
self.draggingDestinationFeedbackStyle = NSTableViewDraggingDestinationFeedbackStyle.None
}
First, init( frame: NSRect )is the designated initializer, so the keyword convenience is wrong in this place.
Then you probably meant to call the super initializer than your own method recursively.
At last you'll need to implement another required initializer init?(coder: NSCoder)
The following code should get you going:
class ItemRowView: NSTableRowView {
override init(frame: NSRect) {
super.init(frame: frame) // super - not self
self.draggingDestinationFeedbackStyle = NSTableViewDraggingDestinationFeedbackStyle.None
}
required init?(coder: NSCoder) {
// Write something useful here, or leave the default implementation
fatalError("init(coder:) has not been implemented")
}
}

IBDesignable can't set background on buttons

I'm trying to create a custom button in my IOS project. But when I'm creating the button with the my class the background doesn't work in the storyboard. But when I run the program the button works fine.
Here you see my class
#IBDesignable class GreenButtonTest : UIButton{
override init(frame: CGRect) {
super.init(frame: frame)
style()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
style()
}
private func style() {
self.backgroundColor = UIColor(CGColor: "#6eb18c".CGColor)
}
}
You need to override prepareForInterfaceBuilder() and call style() from there too.
Try this:
#IBDesignable class GreenButtonTest : UIButton{
override init(frame: CGRect) {
super.init(frame: frame)
style()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
style()
}
override func prepareForInterfaceBuilder() {
style()
}
private func style() {
self.backgroundColor = UIColor.greenColor()
}
}

Custom UILabel-Subclass does not appear in dropdown selection of IB

I wrote a tiny class that inherits from UILabel
import UIKit
class ConnectionStateLabel: UILabel, BTConnectionStateChangedDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
func didConnect() {
self.text = "Connected"
}
func didDisconnect() {
self.text = "Disconnected"
}
}
As far as I know, in Xcode that class should now be selectable from the dropdown menu (section "Custom Class") for any UILabel that I drag into my story board, but it does not show up. What is wrong with that class? What did I miss?

Is it possible to make #IBDesignable override Interface Builder values?

For example, I want to subclass UIButton and set it's font to 20.0f by default. I can write something like this:
#IBDesignable
class HCButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.customInit()
}
func customInit () {
titleLabel?.font = UIFont.systemFontOfSize(20)
}
}
But this does not affect preview in Interface Builder, all custom buttons appear with 15.0f font size by default. Any thoughts?
I have created new IBInspectable as testFonts :
import UIKit
#IBDesignable
class CustomButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.customInit()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.customInit()
}
func customInit () {
titleLabel?.font = UIFont.systemFontOfSize(20)
}
convenience init() {
self.init(frame:CGRectZero)
self.customInit()
}
override func awakeFromNib() {
super.awakeFromNib()
self.customInit()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.customInit()
}
}
Hope it helps you :)
This is working for me.
I think you must override the init with frame initializer as well to affect that
override init(frame: CGRect) {
super.init(frame: frame)
self.customInit()
}
EASIER: The following solution usually works or me
import UIKit
#IBDesignable
class CustomButton: UIButton {
open override func layoutSubviews() {
super.layoutSubviews()
customInit()
}
func customInit () {
titleLabel?.font = UIFont.systemFontOfSize(20)
}
}
I hope that's what you're looking for.

Failable och non-failable initializers prevents me to extend NSView and implement NSCoding

I'm having problems implementing a subclass to NSView that implements NSCoding.
It seems like the declarations of init(coder: NSCoder) are conflicting in NSView and NSCoding.
NSView nowadays says it's failable, but NSCoding still says it's non-failable.
When I try to override init(coder: NSCoder) and do my custom initialisation, Xcode 6.1 gives me an error message:
A non-failable initializer cannot chain to failable initializer
'init(coder:)' written with 'init?'
How am I supposed to do my custom init of my class?
Here's a silly example where I extend a view and I want to persist an additional click counter for my view.
import Cocoa
import Foundation
class MyView: NSView, NSCoding {
var clickCounter:Int = 0
required init(coder: NSCoder) {
super.init(coder: coder)
coder.encodeObject(self.clickCounter,
forKey: "clickCounter")
}
override func encodeWithCoder(coder: NSCoder) {
super.encodeWithCoder(coder)
coder.encodeObject(self.clickCounter, forKey: "clickCounter")
}
override func mouseDown(theEvent: NSEvent) {
clickCounter++
}
}
You don't have to explicitly conform to NSCoding, because NSResponder (superclass of NSView) already conforms to it.
required init?(coder: NSCoder) {
super.init(coder: coder)
self.clickCounter = coder.decodeObjectForKey("clickCounter") as Int
}

Resources