Search code examples
iosswifttyphoon

Typhoon with view controller property


I have class:

class InformationTableViewController: UITableViewController {
    private var cos: Int!
}

And I'm trying to inject property:

public dynamic func informationTableViewController() -> AnyObject {
    return TyphoonDefinition.withClass(InformationTableViewController.self) {
        (definition) in

        definition.injectProperty("cos", with: 3)
    }
}

When it's a simple class it works normal. But when I use InformationTableViewController on Storyboard (as some view class) I'm getting error:

'Can't inject property 'cos' for object 'Blah.InformationTableViewController: 0x7fca3300afe0'. Setter selector not found. Make sure that property exists and writable'

What's the problem?


Solution

  • Private access modifier restricts the use of an entity to its own defining source file.

    So one problem is that you are trying to set your property from outside of it private scope. Remove private keyword from property declaration.

    Another problem here is that you are trying to inject primitive type.

    In Obj-C Typhoon has support of injecting primitive types but not in Swift yet.

    Every class you want to inject has to be a subclass of NSObject in some way (either by subclassing or adding @objc modifier).

    As a workaround you may use NSNumber instead of an Int type for your property.

    class InformationTableViewController: UITableViewController {
       var cos: NSNumber!
    }
    

    Assembly:

    public dynamic func informationTableViewController() -> AnyObject {
        return TyphoonDefinition.withClass(InformationTableViewController.self) {
            (definition) in
    
            definition.injectProperty("cos", with: NSNumber.init(int: 3))
        }
    }