2

I'd like to logically organize class properties to signify that they are one logical unit and to distinguish them from other class properties that are less tightly related.

I thought of doing this using a struct within my class. However, it seems that I can't call class methods from the struct property setters. I get what seems to be an inappropriate compile error: "missing argument for parameter #1 in call"

This seems to be different from calling method from struct in swift where the function is within the struct. In my case, the methods are generic and don't just apply to my struct but to all class properties. Therefore, I don't want to move them within the struct.

Do you have ideas on how to organize properties into tight(er) logical units within classes?

class MyClass {
    struct MyStruct {
        static var aVarInMyStruct: String? {
            didSet {
                anotherVarInMyStruct = foo()        // This gives compile error "missing argument for parameter #1 in call"
            }
        }
        static var anotherVarInMyStruct: String?
    }

    func foo() {
        println("I am foo()")
    }
}
Community
  • 1
  • 1
Daniel
  • 3,758
  • 3
  • 22
  • 43

2 Answers2

4

The inner type MyStruct knows nothing about its outer type MyClass. So there is no foo function to call from MyStruct. To better organize your code I suggest you to use // MARK: - whatever this section is comments. Types are not here to organize codes. Types are here to create right abstractions for the program.

mustafa
  • 15,254
  • 10
  • 48
  • 57
1

I fix your bug:

     class MyClass {
    struct MyStruct {
        static var aVarInMyStruct: String? {
            didSet {
                anotherVarInMyStruct = MyClass.foo()        // This gives compile error "missing argument for parameter #1 in call"
            }
        }
        static var anotherVarInMyStruct: String?
    }

    static  func foo()->String {
        println("I am foo()")
        return "it is ok"
    }
}
AllanXing
  • 174
  • 1
  • 14