如何在Swift子类中覆盖ReadOnly属性,使其阅读写入并分配给超类?

发布于 2025-02-06 08:42:20 字数 1299 浏览 2 评论 0原文

我如何从子类中覆盖Swift超级类的Readonly属性,以使属性阅读写入?

我想这样做,因为那是iOS uiresponder文档所说的内容。但是,当我尝试实现我认为Swift 5文档所说的内容时,我会遇到错误:

Swift 5 Documentation

sashitance

Overriding Property Getters and Setters:
您可以通过提供两者,将其作为读写属性出现作为读写属性。您的子类属性中的getter和一个固定器覆盖。

出了什么问题:

基于上述Swift Docs语句,我没有发现随附的示例,我创建了以下子类,Xcode在其中生成此错误消息:

   '无法分配给属性:“ InputAccessoryViewController”是一个只属性属性

我的子类:

class InputAccessoryEnabledTextView : UITextView {
    override var inputAccessoryViewController: UIInputViewController? {
        get { super.inputAccessoryViewController }
        set { super.inputAccessoryViewController = newValue }
    }
}

uiresponder

声明

var InputAccessoryViewController:uiinputViewController:uiinputviewController? {get}

讨论

此属性通常用于将附件视图控制器连接到针对UITEXTFIELD和UITEXTVIEW对象的系统供键盘上。 此仅阅读属性的价值为零。如果要将自定义控件附加到系统允许的输入视图控制器(例如系统键盘)或自定义输入视图(您在InputViewController属性中提供的一个),请在Uiresponder子类中重新记录此属性为Read-Write。然后,您可以使用此属性来管理自定义附件视图。 接收器成为第一响应者时,响应者基础架构将附件视图附加到适当的输入

注意:我确实在6年前的堆栈溢出中看到一个问题,该问题没有得到接受的答案,并且似乎无法正确回答这个问题,因此请不要在没有充分理由的情况下将其标记为DUP。也许以后可以合并这些问题

How can I override readonly property of a Swift superclass, from a subclass, to make the property read-write?

I want to do that, because that is what the iOS UIResponder documentation says is required. However, I'm getting an error when I try to implement what I think the Swift 5 documentation says can be done:

Swift 5 documentation

Inheritance

Overriding Property Getters and Setters:

You can present an inherited read-only property as a read-write property by providing both a getter and a setter in your subclass property override.


What Went Wrong:

Based on aforementioned Swift docs statement, for which I found no accompanying example, I created the following subclass, at which XCode generates this error message:

   "Cannot assign to property: 'inputAccessoryViewController' is a get-only property"

My Subclass:

class InputAccessoryEnabledTextView : UITextView {
    override var inputAccessoryViewController: UIInputViewController? {
        get { super.inputAccessoryViewController }
        set { super.inputAccessoryViewController = newValue }
    }
}

UIResponder

Declaration

var inputAccessoryViewController: UIInputViewController? { get }

Discussion

This property is typically used to attach an accessory view controller to the system-supplied keyboard that is presented for UITextField and UITextView objects.
The value of this read-only property is nil. If you want to attach custom controls to a system-supplied input view controller (such as the system keyboard) or to a custom input view (one you provide in the inputViewController property), redeclare this property as read-write in a UIResponder subclass. You can then use this property to manage a custom accessory view. When the receiver becomes the first responder, the responder infrastructure attaches the accessory view to the appropriate input view before displaying it.

Note: I do see a question from 6 years ago on Stack Overflow that got no accepted answers and doesn't seem to properly answer the question, so please don't flag this as a dup without a good reason. Perhaps these questions can be merged later

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唱一曲作罢 2025-02-13 08:42:20

super.inputAccessoryViewController无法设置。

您在子类中的覆盖实现,self.inputAccessoryViewController

通过在子类中的属性中添加设置器,您也不会在超级类中自动添加相同的东西。子类中的内容留在子类中。

因此,这并不是说您不能通过添加设置器来覆盖属性,而是您无法在这里设置此设置:

override var inputAccessoryViewController: UIInputViewController? {
    get { super.inputAccessoryViewController }
    set { super.inputAccessoryViewController = newValue }
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

您可以做其他事情,例如:

override var inputAccessoryViewController: UIInputViewController? {
    get { super.inputAccessoryViewController }
    set { print("I just go set to \(newValue)") }
}

但这不是很有用。您想要的是:

private var myInputAccessoryController: UIInputViewController?

override var inputAccessoryViewController: UIInputViewController? {
    get { myInputAccessoryController }
    set { myInputAccessoryController = newValue }
}

super.inputAccessoryViewController is not settable.

Your overridden implementation in the subclass, self.inputAccessoryViewController is.

By adding a setter to the property in a subclass, you don't automatically also add the same thing in the superclass. What's in the subclass stays in the subclass.

So it's not that you can't override a property by adding a setter, you just can't set this here:

override var inputAccessoryViewController: UIInputViewController? {
    get { super.inputAccessoryViewController }
    set { super.inputAccessoryViewController = newValue }
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}

You can do other things, like:

override var inputAccessoryViewController: UIInputViewController? {
    get { super.inputAccessoryViewController }
    set { print("I just go set to \(newValue)") }
}

But that's not very useful. What you want is probably:

private var myInputAccessoryController: UIInputViewController?

override var inputAccessoryViewController: UIInputViewController? {
    get { myInputAccessoryController }
    set { myInputAccessoryController = newValue }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文