xcode 在 Swift 中定义只读属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38318117/
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-09-15 09:02:06  来源:igfitidea点击:

Define a read-only property in Swift

iosswiftxcodeuiviewxcode8

提问by Jason

How do you define a read-only property in Swift? I have one parent class which needs to define a public property eg. itemCount. Here's my code:

你如何在 Swift 中定义只读属性?我有一个需要定义公共属性的父类,例如。itemCount. 这是我的代码:

Class Parent: UIView {
  private(set) var itemCount: Int = 0
}

class Child {
  private(set) override var itemCount {
    get {
      return items.count
    }
  }
}

I get the error: Cannot override mutable property with read-only property

我收到错误: Cannot override mutable property with read-only property



Option 1 - Protocols:

选项 1 - 协议:

Well I can't use a protocol because they can't inherit from classes (UIView)

好吧,我不能使用协议,因为它们不能从类 ( UIView)继承

Option 2 - Composition:

选项 2 - 组成:

I add a var view = UIViewto my Child class and drop the UIViewinheritance from my Parentclass. This seems to be the only possible way, but in my actual project it seems like the wrong thing to do, eg. addSubview(myCustomView.view)

我将 a 添加var view = UIView到我的 Child 类并UIView从我的Parent类中删除继承。这似乎是唯一可能的方法,但在我的实际项目中,这似乎是错误的做法,例如。addSubview(myCustomView.view)

Option 3 - Subclass UIViewon the Childclass

选择3 -子类UIViewChild

I can't do this either because I intend to have multiple related Childclasses with different properties and behaviour, and I need to be able to declare instances of my Childclasses as the Parentclass to take advantage of UIView's properties and Parent's public properties.

我不能这样做,因为我打算拥有多个Child具有不同属性和行为的相关类,并且我需要能够将我的Child类的实例声明为Parent类以利用UIView的属性和Parent公共属性。

采纳答案by Luca Angeletti

You can use a Computed Propertywhich (like a method) can be overridden.

您可以使用Computed Property可以覆盖的which(如方法)。

class Parent: UIView {
    var itemCount: Int { return 0 }
}

class Child: Parent {
    override var itemCount: Int { return 1 }
}

Update (as reply to the comment below)

更新(作为对下面评论的回复)

This is how you declared and override a function

这就是您声明和覆盖函数的方式

class Parent: UIView {
    func doSomething() { print("Hello") }
}

class Child: Parent {
    override func doSomething() { print("Hello world!") }
}

回答by Tina

You can declare setter as private while getter is public.

您可以将 setter 声明为私有,而 getter 是公共的。

public class someClass {
    public private(set) var count: String
}

Refer to this link

参考这个链接

回答by invoodoo

As one more option you can use private variable for read/write and another for read-only. Count you're using for internal class changes, and numberOfItems for public access. Little bit weird, but it solves the problem.

作为另一种选择,您可以将私有变量用于读/写,而另一个用于只读。计算您用于内部类更改的计数,以及用于公共访问的 numberOfItems。有点奇怪,但它解决了问题。

class someClass {
    private var count: Int = 0
    var numberOfItems: Int { return count }

    func doSomething()  {
       count += 1
    }
}