ios 扩展可能不包含存储属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36337509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:49:22  来源:igfitidea点击:

Extensions May not contain Stored properties

iosswiftinheritance

提问by Mohamed DiaaEldin

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

我可以使用扩展在 Swift 中实现它而不需要继承吗?。我收到此错误扩展可能不包含存储的属性

extension UIButton
{
    @IBInspectable var borderWidth : CGFloat
        {
        didSet{
            layer.borderWidth = borderWidth
        }
    }

}

回答by dan

You can override the setter/getter so that it isn't a stored property and just forwards the set/get to the layer.

您可以覆盖 setter/getter,使其不是存储属性,而只是将 set/get 转发到图层。

extension UIButton {
    @IBInspectable var borderWidth : CGFloat {
        set {
            layer.borderWidth = newValue
        }

        get {
            return layer.borderWidth
        }
    }
}

回答by Alexander

Extensions cannot add stored properties. From the docs(Computed Properties section):

扩展不能添加存储的属性。从文档(计算属性部分):

Note

Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.

笔记

扩展可以添加新的计算属性,但它们不能添加存储的属性,或向现有属性添加属性观察者。

If you have a need for stored properties, you should create a subclass, like so:

如果您需要存储属性,您应该创建一个子类,如下所示:

class CustomButton : UIButton
{
    @IBInspectable var borderWidth : CGFloat
        {
        didSet{
            layer.borderWidth = borderWidth
        }
    }

}

回答by Emre Gürses

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

在 Swift 中,我导入了一个用 Objective-C 编写的静态库。下面的协议,在那个库中,有一个方法和一个属性。

@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

1-) 类型“ShowInitialViewLocalService”不符合协议“LocalService”

2-) 扩展可能不包含存储的属性

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.

我希望这会帮助某人。