4

I have following code working in a playground. The didSet observer is working as expected.

struct itemStruct {
  var name : String = "" {
    didSet (newName) {
      didSetNameTest(name)
    }
  }
}

func didSetNameTest (name : String) {
  println("didSet itemStruct: \(name)")
}
var item = itemStruct()
item.name = "test"

If I move the code within a class I get an compiler error:

class itemClass {
  struct classItemStruct{
    var name : String = "" {
      didSet(newName) {
        didSetClassNameTest(name)
      }
    }
  }

  func didSetClassNameTest(name:String) {
    println("didSet itemClass: \(name)")
  }

  var structItem = classItemStruct()
}

var cItem = itemClass()
cItem.structItem.name = "Test"

Error: Cannot invoke 'didSelectClassNameTest' with an argument list of type '(String)'.

All code could be copied in playground.

madcat
  • 322
  • 3
  • 14
  • 2
    Have you had a look at http://stackoverflow.com/questions/26806932/swift-nested-class-properties. It appears an instance of an inner type is independent of any instance of the outer type. – ABakerSmith Jun 02 '15 at 16:29
  • Adding static to didSetClassNameTest fixes the error – madcat Jun 02 '15 at 19:54

2 Answers2

1

Since instance of an inner class is independent of any instance of the outer class as described in a link of @ABakerSmith comment a possible workaround for this would be by makeing didSetClassNameTest function private and static and then call it statically itemClass.didSetClassNameTest(name) on didSet method

Zell B.
  • 10,266
  • 3
  • 40
  • 49
0

Inner types are independent of their outer types. Unlike in java classItemStruct knows nothing about its outer type itemClass.

mustafa
  • 15,254
  • 10
  • 48
  • 57