how to inject a delegate in swift using typhoon? - ios

Using typhoon I'm trying to inject the type of "worker" in my view controller. my "Worker" requires a delegate so that when the work is done, it calls this method. I need to set my view controller to be the delegate of the worker class that was injected. in other words a circular dependency.
Updated question with source:
//my typhoon assembly class
import Typhoon
class Assembly : TyphoonAssembly {
public dynamic func viewController() -> AnyObject {
return TyphoonDefinition.withClass(ViewController.self) {
(definition) in
definition.injectProperty("worker", with: self.foo())
definition.scope = TyphoonScope.Singleton
}
}
public dynamic func foo() -> AnyObject {
return TyphoonDefinition.withClass(Foo.self) {
(definition) in
definition.injectProperty("delegate", with: self.viewController())
}
}
}
Foo is where the work done, it implements WorkHandler protocol and has a delegate of type SomeProtocol to call when work has finished:
import Foundation
#objc
protocol SomeProtocol: class {
optional func hasFinishedWork(value: Bool)
}
protocol WorkHandler : class {
func doSomething()
}
class Foo: WorkHandler{
//how will this get set?
var delegate:SomeProtocol?
func doSomething(){
print("doing the work")
delegate?.hasFinishedWork!(true)
}
}
And my ViewController conforms to SomeProtocol like so:
import UIKit
class ViewController: UIViewController, SomeProtocol{
var worker:WorkHandler?
override func viewDidLoad() {
super.viewDidLoad()
worker?.doSomething();
}
#objc func hasFinishedWork(value: Bool){
print("The work was all done")
}
}
above code gives the following error when it runs:
2016-02-29 20:25:43.250 TestApp[30604:5316415] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Subclass of NSProxy or NSObject is required.'
Is anyone able to help with this?

Turns out i had to make my protocol to inherit from NSObject:
#objc
protocol SomeProtocol: class {
optional func hasFinishedWork(value: Bool)
}
#objc
protocol WorkHandler : class {
func doSomething()
}
class Foo: NSObject, WorkHandler{
//how will this get set?
var delegate:SomeProtocol?
#objc func doSomething(){
print("doing the work")
delegate?.hasFinishedWork!(true)
}
}
Now it works as expected.

Related

Call a method with dynamic class name in swift

How can we call class functions with a dynamic class name?
Assume the following example where I have two class with methods with same signature
class Foo{
class func doSomething()
}
class Foobar {
class func doSomething()
}
class ActualWork{
//call following method with a variable type so that it accepts dynamic class name
func callDynamicClassMethod(x: dynamicClass)
x.doSomething()
}
How can this be implemented so that x accepts values at run time
Edit: Sorry, I missed to mention that I was looking for any other ways other than protocol oriented approach. This is more of an exploratory question to explore if there is a more direct approach/pods/libraries to achieve this.
I liked this question, because it made me to think a lit'bit outside of the box.
I'll answer it, by dividing it into a few parts.
First
call class functions
Class function is basically a Type methods, which can be achieved using the static word inside the class context.
Taking that into account, you can get a simple solution, using protocol and passing the class reference (conforming to that protocol) like this:
protocol Aaa{
static func doSomething();
}
class Foo : Aaa{
static func doSomething() {
print("Foo doing something");
}
}
class FooBar : Aaa{
static func doSomething() {
print("FooBar doing something");
}
}
class ActualWork{
//Using class (static) method
func callDynamicClassMethod <T: Aaa> (x: T.Type) {
x.doSomething();
}
}
//This is how you can use it
func usage(){
let aw = ActualWork();
aw.callDynamicClassMethod(x: Foo.self);
aw.callDynamicClassMethod(x: Foo.self);
}
Second
In case you don't really need the method on the class context, you may consider using instance methods. In that case the solution would be even simpler, like this:
protocol Bbb{
func doSomething();
}
class Bar : Bbb{
func doSomething() {
print("Bar instance doing something");
}
}
class BarBar : Bbb{
func doSomething() {
print("BarBar instance doing something");
}
}
class ActualWork{
//Using instance (non-static) method
func callDynamicInstanceMethod <T: Bbb> (x: T){
x.doSomething();
}
}
//This is how you can use it
func usage(){
let aw = ActualWork();
aw.callDynamicInstanceMethod(x: Bar());
aw.callDynamicInstanceMethod(x: BarBar());
}
Third
If you need to use the class func syntax, as OP originally did:
class func doSomething()
You CANNOT simply use a protocol. Because protocol is not a class...
So compiler won't allow it.
But it's still possible, you can achieve that by using
Selector with NSObject.perform method
like this:
class ActualWork : NSObject{
func callDynamicClassMethod<T: NSObject>(x: T.Type, methodName: String){
x.perform(Selector(methodName));
}
}
class Ccc : NSObject{
#objc class func doSomething(){
print("Ccc class Doing something ");
}
}
class Ddd : NSObject{
#objc class func doSomething(){
print("Ccc class Doing something ");
}
#objc class func doOther(){
print("Ccc class Doing something ");
}
}
//This is how you can use it
func usage() {
let aw = ActualWork();
aw.callDynamicClassMethod(x: Ccc.self, methodName: "doSomething");
aw.callDynamicClassMethod(x: Ddd.self, methodName: "doSomething");
aw.callDynamicClassMethod(x: Ddd.self, methodName: "doOther");
}
Generics and Protocol oriented programming will do the job:
protocol Doable {
static func doSomething()
}
class Foo: Doable {
static func doSomething() {
debugPrint("Foo")
}
}
class Foobar: Doable {
static func doSomething() {
debugPrint("Foobar")
}
}
class ActualWork {
func callDynamicClassMethod<T: Doable>(x: T.Type) {
x.doSomething()
}
}
let work = ActualWork()
work.callDynamicClassMethod(x: Foo.self)
work.callDynamicClassMethod(x: Foobar.self)
you can achieve this with help of Protocol
protocol common {
static func doSomething()
}
class Foo : common{
static func doSomething() {
print("Foo")
}
}
class Foobar : common {
static func doSomething() {
print("Foobar")
}
}
class ActualWork{
//call following method with a variable type so that it accepts dynamic class name
func callDynamicClassMethod(x: common.Type) {
x.doSomething()
}
}
let fooObj : common = Foo()
let Foobarobj : common = Foobar()
let workObk = ActualWork()
workObk.callDynamicClassMethod(x:Foo.self)
workObk.callDynamicClassMethod(x:Foobar.self)
I think, there are three solutions. I shared an sample below.
Use "protocol" that has "doSomething()" function requirements.
Create a function which gets function definition as a parameter.
Use reflection. you can use EVReflection that is good Api for reflection.
sample code:
protocol FooProtocol {
static func doSomething()
}
class Foo: FooProtocol {
class func doSomething() {
print("Foo:doSomething")
}
}
class Foobar: FooProtocol {
class func doSomething() {
print("Foobar:doSomething")
}
}
class ActualWork {
func callDynamicClassMethod<T: FooProtocol>(x: T.Type) {
x.doSomething()
}
func callDynamicClassMethod(x: #autoclosure () -> Void) {
x()
}
func callDynamicClassMethod(x: () -> Void) {
x()
}
}
ActualWork().callDynamicClassMethod(x: Foo.self)
ActualWork().callDynamicClassMethod(x: Foobar.self)
print("\n")
ActualWork().callDynamicClassMethod(x: Foo.doSomething())
ActualWork().callDynamicClassMethod(x: Foobar.doSomething())
print("\n")
ActualWork().callDynamicClassMethod(x: Foo.doSomething)
ActualWork().callDynamicClassMethod(x: Foobar.doSomething)
Looks like you are searching for duck typing, and this is harder to achieve in a statically typed language (with some exceptions, listed in the linked Wikipedia page).
This is because dynamically calling a method requires knowledge about the layout of the target object, thus either inheritance of the class declaring the method, or conformance to a protocol that requires that method.
Starting with Swift 4.2, and the introduction of dynamic member lookup, there is another approach to solve your problem, however it also involves some ceremony:
// This needs to be used as base of all classes that you want to pass
// as arguments
#dynamicMemberLookup
class BaseDynamicClass {
subscript(dynamicMember member: String) -> () -> Void {
return { /* empty closure do nothing */ }
}
}
// subclasses can choose to respond to member queries any way they like
class Foo: BaseDynamicClass {
override subscript(dynamicMember member: String) -> () -> Void {
if member == "doSomething" { return doSomething }
return super[dynamicMember: member]
}
func doSomething() {
print("Dynamic from Foo")
}
}
class Bar: BaseDynamicClass {
override subscript(dynamicMember member: String) -> () -> Void {
if member == "doSomething" { return doSomething }
return super[dynamicMember: member]
}
func doSomething() {
print("Dynamic from Bar")
}
}
func test(receiver: BaseDynamicClass) {
receiver.doSomething()
}
test(receiver: Bar()) // Dynamic from Bar
To conclude, in the current Swift version there is no way to have both the argument and the method dynamic, some common ground needs to be set.

method in protocol extension gets called instead of method implementation in View Controller

so I have a viewController which holds a custom view,
and that viewController class conforms to ViewProtocol
I expect when someAction method triggered in someCustomizedView
it will print " method in otherCustomizedClass called "
but it prints (" method in extension Called") instead.
The theNotOptionalMethod works just fine but not the optional method.
Is there anything that I misunderstand of protocol extension ?
Please help, been struggling for hours, thanks
protocol ViewDelegate: class {
func theNOTOptionalMethod()
}
extension ViewDelegate {
func theOptionalMethod(){
print (" method in extension Called")
}
}
class someCustomizedView: UIView {
weak var deleage: ViewDelegate?
#IBAction func someAction(sender: UIButton) {
deleage?.theOptionalMethod()
}
}
class someCustomizedVC: UIViewController, ViewDelegate {
lazy var someView: someCustomizedView = {
var v = someCustomizedView()
v.deleage = self
return v
}()
//...... someView added to controller
func theNOTOptionalMethod() {
// do nothing
}
func theOptionalMethod() {
print (" method in otherCustomizedClass called ")
}
}
That is how methods in extensions work. They hide the implementations in a class.
To create a protocol with optional methods, you need to put the optional method in the protocol definition:
protocol ViewDelegate: class {
func theNOTOptionalMethod()
func theOptionalMethod()
}
Alternatively, you can use #objc and optional modifiers:
#objc protocol MyDelegate : class{
func notOptionalMethod()
#objc optional func optionalMethod()
}
When you call optionalMethod, you need to unwrap the optional:
delegate.optionalMethod?()

Duplicate codes in Controller

I am currently working on one project. It may potentially have duplicated codes in multiple controllers like below.
Controller A
class A: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
// about 50~70 lines of codes
#IBAction func scanButtonTapped {
// used self (as AVCaptureMetadataOutputObjectsDelegate)
// used view
// called presentViewController(...), which is a func in UIViewController
}
}
Controller B
class B: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
#IBAction func scanButtonTapped {
// will need same logic as in Controller A
}
}
My current solution is have another class C, and move the duplicated codes into it. However, if I do so, controller can cast to AVCaptureMetadataOutputObjectsDelegate, but not to UIViewController.
class C {
func btnTapped (view: UIView, controller: AnyClass) {
// logic is here
// controller can cast to AVCaptureMetadataOutputObjectsDelegate
// but controller cannot cast to UIViewController
}
}
so A and B will have
class A {
#IBAction func scanButtonTapped {
let c = C()
c.btnTapped(view, self)
}
}
My question is if it is possible to cast controller into UIViewController. OR is there another way to refactor the codes properly?
What about extend AVCaptureMetadataOutputObjectsDelegate protocol and create default implementation by protocol extension (POP approach)?
protocol ScanButtonClickable: AVCaptureMetadataOutputObjectsDelegate {
func btnTapped() // this line is optional
}
extension Clickable where Self: UIViewController {
func btnTapped() {
// logic is here
}
}
class A: UIViewController, ButtonClickable {
...
}
class B: UIViewController, ButtonClickable {
...
}
Try this:
//declare your default method to be used across classes
protocol MyProtocol {
func myFunc()
}
//provide the implementation of your default behavior here in myFunc()
extension MyProtocol {
func myFunc() {
print("Default behavior")
}
}
class A: MyProtocol {
}
class B: MyProtocol {
}
let a = A()
a.myFunc()
let b = B()
b.myFunc()
//prints
Default behavior
Default behavior

Conformance issues even after fulfilling requirements using protocol extensions for #objc protocols

Let us consider this example which compiles without any issues :
protocol AProtocol {
func veryImportantMethod()
}
protocol BProtocol : AProtocol {
}
extension BProtocol {
func veryImportantMethod() {
print("A's protocol requirement satisfied!")
}
}
class ConcreteClass : BProtocol {
}
However, if we do this :
#objc protocol AProtocol { //Added #objc here
func veryImportantMethod()
}
protocol BProtocol : AProtocol {
}
extension BProtocol {
func veryImportantMethod() {
print("A's protocol requirement satisfied!")
}
}
class ConcreteClass : BProtocol {
}
It doesn't compile with a message saying :
Type 'ConcreteClass' does not conform to protocol 'AProtocol'
However, if we actually implement it,
class ConcreteClass : NSObject, BProtocol {
func veryImportantMethod() { }
}
the code compiles. Why is this happening? Is there anything that I am missing here?
I am trying to achieve this form of protocol hierarchy for UITableViewDataSource and I really do not want to repeat tableView:cellForRowAtIndexPath: code across conformants.
The compiler wants to make sure that the class can be compiled with ObjC. Once you inherit from an ObjC class (like NSObject) the compiler knows it, but you implement the extension before that point. You could avoid the error by defining the protocol to be implemented by a class that inherits from NSObject, like this:
#objc protocol AProtocol {
func veryImportantMethod()
}
protocol BProtocol : AProtocol {
}
class BProtocolClass: NSObject {
}
extension BProtocolClass {
func veryImportantMethod() {
print("A's protocol requirement satisfied!")
}
}
class ConcreteClass: BProtocolClass {
}

Swift 2.0 Protocol as a Type Extension

While playing around in playground with protocol extension I came to strange error. More precisely I declared a protocol and used it as type in the following manner :
protocol InvokeProtocol{
func invokeA()
func invokeB()
}
class Controller{
var invoker : InvokeProtocol?
func methodA(){
invoker?.invokeA()
}
func methodB(){
invoker?.invokeB()
}
}
What I did next was creating a subclass of Controller and tried to extend the InvokeProtocol for that particular type
extension InvokeProtocol where Self: SpecificController{
func invokeC(){
}
}
class SpecificController : Controller {
override func methodA() {
super.methodA()
}
override func methodB() {
super.methodB()
}
func methodC(){
invoker?.invokeC()
}
}
But this give me the following compile time error while I try to call invokeC on SpecificController
#opened("70A8B09C-65E3-11E5-9A8E-6C40088AF546") InvokeProtocol' is not a subtype of 'SpecificController'
I just jumped to Swift 2.0 and this is my first time I see errors starting with #opened. What does this mean? Is this a known bug waiting to be fixed? If so do you guys have any workaround about this?
To reply to your comment. It is achievable. You just haven't implemented the protocol in the first place. The following will work:
protocol InvokeProtocol {
func invokeA()
func invokeB()
}
class Controller : InvokeProtocol {
func invokeA(){
}
func invokeB() {
}
}
extension InvokeProtocol where Self: SpecificController{
func invokeC() {
}
}
class SpecificController : Controller {
override func invokeA() {
super.invokeA()
}
override func invokeB() {
super.invokeB()
}
func invokeC() {
}
}
I guess the where Self: SpecificController, the Self means var invoke: InvokeProtocol?
if invoke conform the protocol and invoke is SpecificController, that invoke can call the method named invokeC()

Resources