SKNode subclass generates error: cannot invoke initializer for type X with no arguments
If you look at The Swift Programming Language: Initialization, under Automatic Initializer Inheritance, one of the rules for automatically inheriting a superclass's designated initialisers is:
If your subclass doesn’t define any designated initializers, it
automatically inherits all of its superclass designated initialisers.
This assumes you provide default values for any new properties you introduce.
Since you're defining the designated initialiser init(gridWidth: CGFloat, deviceHeight: CGFloat)
your subclass doesn't inherit init()
from SKNode
. Therefore, to be able to use StatusScreen()
you need to override init()
in your StatusScreen
class:
class StatusScreen: SKNode {
// ...
override init() {
super.init()
// Do other stuff...
}
}
Now you can use:
let node1 = StatusScreen()
let node2 = StatusScreen(gridWidth: 100, deviceHeight: 100)
Hope that helps!
Cannot invoke initializer for type 'CustomDetailView' with no arguments
By adding the following code in custom class, the issue is fixed
convenience init() {
self.init(frame: CGRect.zero)
}
Error passing a SKNode as parameter for a function
In the target/action pattern the (first) parameter of any action is required to be the object which performs the action
@objc func swipedRight(_ sender: UISwipeGestureRecognizer) { ... }
and add the selector just with the method name
swipeRightRec.addTarget(self, action: #selector(swipedRight))
But as the node is a property anyway it's not necessary to pass it as parameter.
Can't call function in different class, error: Cannot invoke with my parameter type
grav
is an instance method. You are calling it on the class while you should call it on an instance of the class.
Comparing struct values which are inside class
This gets a little cumbersome since goalDifference is optional but here is a solution with a function that compares self
with another team. I assume that this is sports related and that goals always start at 0.
extension Team {
private static let emptyGoalsDifferenc = GoalsDifference(scoredGoal: .zero, passedGoal: .zero)
func best(goals: KeyPath<GoalsDifference, Int>, comparing other: Team) -> Team? {
let ownDifference = self.goalsDifference ?? Self.emptyGoalsDifferenc
let otherDifference = other.goalsDifference ?? Self.emptyGoalsDifferenc
let ownGoals = ownDifference[keyPath: goals]
let otherGoals = otherDifference[keyPath: goals]
if ownGoals == otherGoals { return nil }
return ownGoals > otherGoals ? self : other
}
}
Example
var team1 = Team(name:"Team one", goalsDifference: .init(scoredGoal: 3, passedGoal: 5))
var team2 = Team(name:"Team two", goalsDifference: .init(scoredGoal: 3, passedGoal: 2))
let bestScored = team1.best(goals: \.scoredGoal, comparing: team2)
let bestPassed = team1.best(goals: \.passedGoal, comparing: team2)
Related Topics
Why Would One Use Nested Classes
Weak Reference to Closure in Swift
Detecting Swipes on All Four Directions on Watchkit Using The Storyboard
Difference Between @Propertydelegate and @Propertywrapper
How to Return Subscript in Constant Time
Vapor 3 - How to Check for Similar Email Before Saving Object
All of My UIalertcontroller Messages Became Single Line
Alamofire Post Request with JSON Encoding
Nsdatepicker in Nsstatusbar Nssmenuitem Not Receiving Input
How to Test If an Instance Is a Specific Class or Type in Swift
Type 'Bundle' Has No Member "Module"
How to Get Reliable Timing for My Audio App
Carthage Update Error: "Github API Request Failed: Bad Credentials"