0

I have protocol which simplifies objects initialization by standardizing initializer names. Here is how it looks like in Objective - C

@protocol CSModelInitialization <NSObject>

@required
+ (id)modelWithParameters:(NSDictionary *)parameters;

@optional
- (instancetype)initWithParameters:(NSDictionary *)parameters;

@end

Now I have created Swift class, let's call it TestClass which will implement this protocol. In Objective - C implementation would look something like this:

+ (instancetype)modelWithParameters:(NSDictionary *)parameters {

    SomeClass *object = [[self alloc] init];
    //Setting some properties from parameters NSDictionary...
    return object;
}

Now when I try to use similar approach in Swift I'm getting errors, like

Type 'TestClass' does not conform to protocol 'MyProtocol'

or `

'TestClass' is not convertible to 'Self'

Swift implementation looks like this:

import UIKit

public class TestClass: NSObject, MyProtocol {

    //MARK: MyProtocol

    public class func modelWithParameters(parameters: [NSObject : AnyObject]!) -> Self? {

        var object: TestClass = TestClass()
        //Setting some properties from parameters dictionary...
        return object
    }
}

I'm relatively new to Swift and linking to some example would be really awesome.

Josip B.
  • 2,434
  • 1
  • 25
  • 30
  • Marking this as duplicate was really easy but in the end it didn't give me direct answer. Providing direct answer would be appreciate solution. – Josip B. Mar 17 '15 at 13:43
  • 1
    I have found an answer at this question: http://stackoverflow.com/questions/25645090/protocol-func-returning-self – Josip B. Mar 17 '15 at 13:53

0 Answers0