Swift - Must call a designated initializer of the superclass SKSpriteNode error
init(texture: SKTexture!, color: UIColor!, size: CGSize)
is the only designated initializer in the SKSpriteNode class, the rest are all convenience initializers, so you can't call super on them. Change your code to this:
class Creature: SKSpriteNode {
var isAlive:Bool = false {
didSet {
self.hidden = !isAlive
}
}
var livingNeighbours:Int = 0
init() {
// super.init(imageNamed:"bubble") You can't do this because you are not calling a designated initializer.
let texture = SKTexture(imageNamed: "bubble")
super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
self.hidden = true
}
init(texture: SKTexture!) {
//super.init(texture: texture) You can't do this because you are not calling a designated initializer.
super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
}
init(texture: SKTexture!, color: UIColor!, size: CGSize) {
super.init(texture: texture, color: color, size: size)
}
}
Furthermore I would consolidate all of these into a single initializer.
Swift: Must call a designated initializer of the superclass error even though code is doing so
The problem there is that you are also trying to access your PreviewNode properties before calling self.init()
Try like this:
Xcode 8 GM • Swift 3
class PreviewNode: SCNNode {
let previewNodeColor: UIColor = .red
let size: CGFloat = 1
let chamferRadius: CGFloat = 0
convenience override init() {
self.init()
let previewBox = SCNBox(width: size, height: size, length: size, chamferRadius: chamferRadius)
previewBox.firstMaterial?.diffuse.contents = previewNodeColor
previewBox.firstMaterial?.transparency = 0.2
previewBox.firstMaterial?.specular.contents = UIColor.white
self.geometry = previewBox
}
}
Must call a designated initializer of the superclass 'Day' error
The problem is caused by calling self.init
in init?(coder
of Day
. That call makes the initializer convenient
and the subclass is not able to fulfill the requirements to call a designated initializer.
The solution is to initialize the properties directly
required init?(coder aDecoder: NSCoder) {
self.dayName = aDecoder.decodeObject(forKey: PropertyKey.dayName) as! String
self.subjects = aDecoder.decodeObject(forKey: PropertyKey.subjects) as? [Subject]
}
By the way: You are encoding always dayName
as a non-optional string so it never can be nil
when being decoded. The guard
is useless.
In the subclass you might need to add code to en-/decode the properties of the subclass and call super
to consider also the properties of the superclass.
class Subject: Day {
var subjectName: String
var startsAt: String?
init(dayName: String, subjectName: String) {
self.subjectName = subjectName
super.init(dayName: dayName)
}
override func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
aCoder.encode(subjectName, forKey: "subjectName")
aCoder.encode(startsAt, forKey: "startsAt")
}
required init?(coder aDecoder: NSCoder) {
subjectName = aDecoder.decodeObject(forKey: "subjectName") as! String
startsAt = aDecoder.decodeObject(forKey: "startsAt") as? String
super.init(coder: aDecoder)
}
}
The question for the sense of using a subclass as property in its superclass is another story /p>
Am unable to subclass SKSpriteNode using an image name
Just use the full initializer on the super, like so...
class MyBall : SKSpriteNode{
init(iNamed: String) {
let texture = SKTexture(imageNamed: iNamed)
super.init(texture: texture, color: .clear, size: texture.size())
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Must call a designated initializer of the superclass 'UIViewController' in Swift 1.2
You are writing a view controller class for other people to use as part of their own app. If this view controller could have a nib file, then what you are doing would be wrong and would always have been wrong: it would be up to you to allow the caller to supply the nib file name as one of the parameters of your initializer, so that you could call init(nibName:bundle:)
and pass that value along.
But I gather that your view controller just uses the default empty automatic view, so this is not a problem in actual fact.
Therefore what you'll do is call init(nibName:bundle:)
, and since this view controller has no nib file, just pass nil
for the nib name. This in fact is what super.init()
was doing in the earlier version of Swift - it was calling init(nibName:bundle:)
with nil
values for you. So nothing is really lost or changed.
Must call a designated initializer of the superclass 'UITextView'
As per document for UITextView
// Create a new text view with the specified text container (can be nil) - this is the new designated initializer for this class
@available(iOS 7.0, *)
public init(frame: CGRect, textContainer: NSTextContainer?)
Why am I unable to subclass the SKSpriteNode
In Swift, you need to call an initializer that is implemented directly from your super class, in this case, SKSpriteNode. super.init()
is implemented by another class that is in SKSpriteNode's inheritance tree, like SKNode or NSObject. You can always check the documentation for which constructors can you call for each class. It's very important to pay understand that you need to call designated initializers in your subclass
For example, you can do
class GuessSlot : SKSpriteNode{
init(color: SKColor, size: CGSize) {
super.init(texture: nil, color: color, size: size)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
How to call a designated initializer of the superclass SKPhysicsBody?
The initializer you are attempting to call is derived from conversion of the Objective C factory method:
+ (SKPhysicsBody *)bodyWithCircleOfRadius:(CGFloat)r;
By command clicking on the class in your code, you can see that it is presented to Swift as:
/*not inherited*/ init(circleOfRadius r: CGFloat)
For contrast, the UIView initializer:
- (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER;
is seen in Swift as:
init(frame: CGRect)
without the /*not inherited*/
comment.
The reason for the difference is that the factory method underlying the SKPhysicsBody initializer always returns a concrete instance of SKPhysicsBody, while initWithFrame is an instance method returning instancetype
, allowing it to be used in subclass initialization.
Your subclass therefore cannot rely on any of the standard initializers for SKPhysicsBody.
Related Topics
iOS Enterprise Provisioning Profile Expiration
Customizing the Colors of a Uisegmentedcontrol
Titletextattributes Uiappearance Font in iOS 7
Swift Add Footer View in Uitableview
Easiest Way of Getting Reverse Geocoded Current Location from iOS
Watchkit: Unable to Find Interface Controller Class
Customizing the Mkannotation Callout Bubble
Background User Location When App Is Terminated/Suspended
Uiscrollview Not Scrolling Regardless of Large Contentsize
How to Set the Cornerradius of a Uistackview
Uidatepicker Bug? Uicontroleventvaluechanged After Hitting Minimum Internal
Has Anyone Found a Good Way of Using the New iOS5 Keyboard Events
Calling Function from Another Viewcontroller in Swift
Flutter:Not Connecting to iOS Simulator in Android Studio
Google Objective-C API 'Gtl' with Swift
Sfspeechrecognizer - Detect End of Utterance