Calling a function from other Swift class - swift

I am trying to access a function from my Class1.swift to Class2.swift
Here's my function in Class1.swift:
class ExampleFormViewController: FormViewController {
......
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
load_data()
self.loadForm()
}
func get_facility(company: Int!, territory: Int!){
}
}
And here's how I call it in my Class2.swift:
ExampleFormViewController().get_facility(ExampleFormViewController().val_company[row], territory: ExampleFormViewController().val_territory[row])
The error I get in this line is: Missing argument for parameter 'coder' in call

Your init method for the class requires a coder parameter.You need an NSCoder to pass in, put in a variable called theCoder and then change your call to this:
ExampleFormViewController().get_facility(ExampleFormViewController(theCoder).val_company[row], territory: ExampleFormViewController(theCoder).val_territory[row])

Related

Swift Call an override method by subclass from superclass

I have two classes in swift. I have an action method that I want to override in many subclasses that will do a different action. I wanted this to sort of function like an abstract class in Java, where I could pass a bunch of different subclasses of a certain class, but they would be referred to from the superclass so that one method could be called for all of them even though they are different subclasses with different overridden versions of that one method. Here is what I mean.
class1: SKSpriteNode{
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func action(){ print("this should never be called") }
}
class2: class1 {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func action() {
self.physicsBody?.affectedByGravity = true
}
}
I would want to do something like this. A node with name someName would be defined as a class2 in the SKScene editor.
class GameScene...
let node = self.childNode(withName: "someName") as? class1
noe.action()
However, I never get the output I want. I always get the call of class1 even when I want it to be overridden as a class2 and turn on gravity for that node. I would want to call the action method for many subclasses of class1 no matter the subclass, so just changing the class1 method to do what I want will not work.
Thank you
EDIT: Here are my actual classes:
So I have some classes that are nodes that will be triggered when the user passes the trigger node. It has a stored value for the name of the target node that is then retrieved from the main SKScene, its parent. Then I want to have a specific TargetNode, DropNode that will make it fall from the sky, but I want to be able to trigger any subclass of TargetNode by calling the TriggerAction method, but it never seems to perform the code of the subclass, the DropNode, but instead always does nothing/ whatever code I put in the action method of the TargetNode class. It never overrides the method.
//SUPERCLASS:
import Foundation
class SKTargetNode: SKGlowNode {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func action(){
print("i don't want this to be called")
}
}
//SUBCLASS:
import Foundation
class SKDropNode: SKTargetNode {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func action() {
self.physicsBody?.affectedByGravity = true
}
}
//Just a class that triggers any TargetNode
import Foundation
import SpriteKit
class SKTriggerNode: SKSpriteNode {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func triggerAction(_ scene: GameScene){
let nodeName = self.userData?.value(forKey: "TargetNode") as? String ?? ""
let node = scene.childNode(withName: "//\(nodeName)") as? SKTargetNode
node?.action()
}
}

Instantiate class with NSCODER

I face a problem when trying to instantiate a class(SettingsView) that uses NSCoder, in another class(Swipe). The SettingsView class is already an instance of an objective -C class(FXBlurView)
I get the following error "Missing argument for parameter 'coder' in call". I realized that I have to call some parameters, but I do not know which.
SettingsView :
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.tintColor = UIColor.clearColor()
}
Swipe :
var settings = SettingsView()//error occurs here
i already tried to use
init () {
super.init (tint: ...)
}
But it seems to be not functionnal.
Ok , i succeed to do it just by adding :
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
var settings = SettingsView(coder: aDecoder)
}
into my "Swipe" view controller.

Correctly init NSCoder in sub class when init NSCoder is convenience method in base class in Swift

Here's my code:
import Foundation
class Person: NSObject, NSCoding {
var name: String
init(name: String) {
self.name = name
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: "name")
}
required convenience init?(coder aDecoder: NSCoder) {
let name = aDecoder.decodeObjectForKey("name") as! String
self.init(name: name)
}
}
class Martin: Person {
init() {
self.init(name: "Martin")
}
required convenience init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
let p = Martin()
print(p.name)
For some reason I always end-up in a catch-22 situation, the only way i see making this work is to explicitly initialize all properties in required convenience init?(coder aDecoder: NSCoder) to able to remove the convenience and do super.init(coder: aDecoder) in Martin
I read about the init rules in Swift, still don't understand why Martin can't inherit the convenience init from Person in this case.
Because the rules state that
A designated initializer must call a designated initializer from its immediate superclass.
A convenience initializer must call another initializer from the same class.
A convenience initializer must ultimately call a designated initializer.

Make a class of type SKNode in swift

Im trying to make a class in swift that will be of type sknode that will also contain a parameter holding a position. I keep getting an error trying to create the class saying "initializer does not override a designated initializer from its super class" I'm not sure what I'm doing wrong, or how the class is supposed to be created.
import Foundation
import SpriteKit
class hud:SKNode {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(position:CGPoint){
}
}
You are getting the "initializer does not override a designated initializer from its super class" error since you have marked your init(position: CGPoint) initializer as override.
SKNode has no such initializer so there is nothing to override.
Simply removing the override will rid you of the error, and your Hud class will have an initializer taking a CGPoint as a parameter.
init(position: CGPoint) {...}
It's worth noting that SKNode already has a property called position of type CGPoint, so it's possible that SKNode already have the capabilities you are after. Otherwise you'll likely want to call your property something else (such as myPosition) to avoid clashing with the existing property.
Remove the override keyword and don't forget to call the original initializer in your init method:
class hud:SKNode {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(position:CGPoint){
super.init()
}
}

Annotating Swift function declarations that support conformance to a protocol?

Is there a standard mechanism for annotating function declarations in Swift to indicate that they are present because a class conforms to some protocol?
For instance, this declaration might be present because a class conforms to NSCoding. (Marking it with override would result in a syntax error, so it's not the kind of annotation I am looking for.) Ideally I am looking for a code-level annotation (e.g. override instead of /*! ... */).
// ... annotation such as "conform to NSCoding", if possible
func encodeWithCoder(encoder: NSCoder) {
// ...
}
You can use extension. for example:
protocol SomeProtocol {
func doIt() -> Int
}
class ConcreteClass {
....
}
extension ConcreteClass: SomeProtocol {
func doIt() -> Int {
// ...
return 1
}
}
But you cannot define required initializer in extension, for example:
// THIS DOES NOT WORK!!!
class Foo: NSObject {
}
extension Foo: NSCoding {
required convenience init(coder aDecoder: NSCoder) {
self.init()
}
func encodeWithCoder(aCoder: NSCoder) {
// ...
}
}
emits an error:
error: 'required' initializer must be declared directly in class 'Foo' (not in an extension)
required convenience init(coder aDecoder: NSCoder) {
~~~~~~~~ ^
In this case, you should use // MARK: comments:
class Foo: NSObject {
// ...
// MARK: NSCoding
required init(coder aDecoder: NSCoder) {
super.init()
}
func encodeWithCoder(aCoder: NSCoder) {
// ...
}
}