Extensions May not contain Stored properties

前端 未结 3 1746
后悔当初
后悔当初 2020-12-24 05:30

Can I implement this in Swift with Extensions without the need to inheritance?. I get this error Extensions May not contain Stored properties

extension UIB         


        
3条回答
  •  [愿得一人]
    2020-12-24 06:16

    In Swift, I'm importing a static library written in Objective-C. The protocol below, in that library, has a method and a property.

    @class Message;
    
    @protocol LocalService 
    
    @property (readonly) int action;
    
    - (Message *)getResponse:(Message *)request;
    
    @end
    

    Trying to have a class conform to that protocol, delivers the messages below:

    1-) Type 'ShowInitialViewLocalService' does not conform to protocol 'LocalService

    2-) Extensions may not contain stored properties

    The code provided below, fixes this issue:

    import UIKit
    
    class ShowInitialViewLocalService: NSObject{
    
    }
    
    extension ShowInitialViewLocalService : LocalService {
        
        var action: Int32 {
            get { return View.InitialView.rawValue }
        }
        
        func getResponse(_ request: Message) -> Message {
            let response = Response(source: request.target, target: request.source, relatedView: View.InitialView.rawValue, action: request.action, data: request.data)
            return response
        }
    }
    

    I hope this will help someone.

提交回复
热议问题