6
votes

I am having trouble making a swift class conform to an objective c protocol. It is easy to implement the methods in an objective c protocol in swift, but I can't implement the properties in the following protocol.

The protocol is

@protocol ATLParticipant <NSObject>
@property (nonatomic, readonly) NSString *firstName;
@property (nonatomic, readonly) NSString *lastName;
@property (nonatomic, readonly) NSString *fullName;
@property (nonatomic, readonly) NSString *participantIdentifier;
@end

I have made this swift class which should conform to it, but Xcode says it doesn't.

class ConversationParticipant: NSObject, ATLParticipant {
    var firstName: NSString?
    var lastName: NSString?
    var fullName: NSString?
    var participantIdentifier: NSString?

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

I have tried making the member variables optional (as above), and unwrapped, and prefixed with private(set) to make them readonly, but none of these variations work.

3

3 Answers

7
votes

Found the solution, in Swift you shouldn't use NSString, but the String type.

class ConversationParticipant: NSObject, ATLParticipant {

    var firstName: String!
    var lastName: String!
    var fullName: String!
    var participantIdentifier: String!
    var avatarImage: UIImage!

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

I implemented this solution and still got an error:

"Type 'ConversationParticipant' does not conform to protocol 'ATLAvatarItem'"

I added the following to solve it:

var avatarImageURL: NSURL!
var avatarImage: UIImage!
var avatarInitials: String!

and worked just fine.

0
votes

For ATLParticipant...

class ConversationParticipant: ConversationAvatarItem, ATLParticipant {

   var firstName: String!
   var lastName: String!
   var fullName: String!
   var participantIdentifier: String!

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

For ATLAvatarItem...

class ConversationAvatarItem: NSObject, ATLAvatarItem {

   var avatarImageURL: NSURL!
   var avatarImage: UIImage!
   var avatarInitials: String!

   override init() {
      super.init()
   }

}