0

I am new in swift anyone help me to understand

what is the purpose ? I should be used class label type !

is it possible to declare Computed property and Observers with class Label type ?

both ,either or neither ?

thank you

2 Answers2

3

Type Properties can be created using either static or class keyword

  1. Computed properties can be created using class as well as static keyword.
  2. Property observers are not allowed using class keyword. They can only be used with static.

Example:

class First
{
    class var x : Int{
        return 3
    }

    static var y : Int{
        return 2
    }

//    error: class stored properties not supported in classes
//    class var z = 10 {
//        willSet{
//            print(newValue)
//        }
//    }

    static var w = 30 {
        willSet{
            print(newValue)
        }
    }
}
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • is it possible to Property observers without initialization? –  Sep 21 '16 at 13:51
  • 1
    Property observers can only be used with stored properties. So you need to initialize the property to some value before you start observing it for changes. – PGDev Sep 22 '16 at 00:43
0

Class Computed Property

You can declare a class computed property

class Foo {
    class var p0: String { return "p0" }
}

Class Static Stored property

It is somehow possible but you need to use the static keyword

class Foo {
    static var p0: String = "p0" {
        didSet {
            print("Did set p0")
        }
    }
}
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • Stored property possible to without initialisation ? –  Sep 21 '16 at 03:20
  • 1
    Lazy stored properties can be created without initialization before the object is created. But still you have to give it some value/expression that can be computed when it is first used. For normal stored properties you need to initialize them either with default values or in init() – PGDev Sep 22 '16 at 00:46