Ensure swift conforms to the objective protocol class c, which defines properties

I'm having trouble getting the fast class to conform to the objective protocol c. It is easy to implement methods in an objective c-protocol in swift, but I cannot implement properties in the following protocol.

Protocol

@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 made this quick class that should match it, but Xcode says it is not.

 class ConversationParticipant: NSObject, ATLParticipant { var firstName: NSString? var lastName: NSString? var fullName: NSString? var participantIdentifier: NSString? override init() { super.init() } } 

I tried to make member variables optional (as mentioned above) and unpacked and prefixed with private (set) to make them readonly, but none of these options work.

+6
source share
3 answers

Found a solution, in Swift you should not 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() } } 
+7
source

I implemented this solution and still got the error:

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

I added the following to solve it:

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

and worked perfectly.

+1
source

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() } } 
0
source

Source: https://habr.com/ru/post/984486/


All Articles