0

I am trying to implement the below protocol in Swift which is written in objective-c however I am still receiving compiler errors saying that the class does not conform to the protocol. What am I doing wrong? I have tried setting getters and setters and applying the @NSCopying flag but to no avail.

#import <Foundation/Foundation.h>

@protocol AIConfiguration <NSObject>

@property(nonatomic, copy) NSURL *baseURL;

@property(nonatomic, copy) NSString *clientAccessToken;

@property(nonatomic, copy) NSString *subscriptionKey;

@end

Here is my implementation

class AIMyConfiguration : NSObject, AIConfiguration {

    var baseURL : NSURL
    var clientAccessToken : NSString
    var subscriptionKey : NSString

    override init() {
        super.init()
    }
}

Thank you

1
  • Objective-C properties are very different from Swift instance variables. Have you tried adding a getter and setter method that match what Objective-C's properties actually create? Commented Mar 4, 2015 at 21:31

1 Answer 1

1

This Objective-C protocol:

@protocol AIConfiguration <NSObject>
@property(nonatomic, copy) NSURL *baseURL;
@property(nonatomic, copy) NSString *clientAccessToken;
@property(nonatomic, copy) NSString *subscriptionKey;
@end

is translated to Swift as:

protocol AIConfiguration : NSObjectProtocol {
    @NSCopying var baseURL: NSURL! { get set }
    var clientAccessToken: String! { get set }
    var subscriptionKey: String! { get set }
}

So you have to implement like:

as ImplicitlyUnwrappedOptional

class AIMyConfiguration: NSObject, AIConfiguration {
    @NSCopying var baseURL: NSURL!
    var clientAccessToken: String!
    var subscriptionKey: String!

    override init() {
        super.init()
    }
}

OR as Optional:

class AIMyConfiguration: NSObject, AIConfiguration {
    @NSCopying var baseURL: NSURL?
    var clientAccessToken: String?
    var subscriptionKey: String?

    override init() {
        super.init()
    }
}

OR non optional:

class AIMyConfiguration: NSObject, AIConfiguration {
    @NSCopying var baseURL: NSURL
    var clientAccessToken: String
    var subscriptionKey: String

    init(baseURL:NSURL, clientAccessToken:String, subscriptionKey:String) {
        self.baseURL = baseURL
        self.clientAccessToken = clientAccessToken
        self.subscriptionKey = subscriptionKey
        super.init()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.