Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a read-only property in Swift

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:

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


Option 1 - Protocols:

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

Option 2 - Composition:

I add a var view = UIView to my Child class and drop the UIView inheritance from my Parent class. 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)

Option 3 - Subclass UIView on the Child class

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

like image 468
Jason Avatar asked Jul 12 '16 00:07

Jason


People also ask

How do I declare a read only property in Swift?

In swift, we can create a read-only property by only defining a getter for a variable. Meaning no setter! Since the variable only has a getter, the compiler will throw an error when we try to assign a value to “sum”.

What read only properties?

Read only means that we can access the value of a property but we can't assign a value to it. When a property does not have a set accessor then it is a read only property. For example in the person class we have a Gender property that has only a get accessor and doesn't have a set accessor.

What is willSet and didSet in Swift?

willSet is called before the data is actually changed and it has a default constant newValue which shows the value that is going to be set. didSet is called right after the data is stored and it has a default constant oldValue which shows the previous value that is overwritten.

What is type property in Swift?

There are two kinds of properties: stored properties and computed properties. Stored properties are properties that are stored in the class's instance. Stored properties store constant and variable values. Computed properties are for creating custom get and set methods for stored properties.


3 Answers

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

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!") }
}
like image 83
Luca Angeletti Avatar answered Oct 07 '22 10:10

Luca Angeletti


You can declare setter as private while getter is public.

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

Refer to this link

like image 37
Tina Avatar answered Oct 07 '22 11:10

Tina


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.

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

    func doSomething()  {
       count += 1
    }
}
like image 40
invoodoo Avatar answered Oct 07 '22 10:10

invoodoo