What is a class member?

Apple's documentation(https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html#//apple_ref/doc/uid/TP40014097-CH41-ID3) says: "Class members with public access, or any more restrictive access level, can be overridden by subclasses only within the module where they’re defined."


I know what a subclass is. but what is a class member?

Are you already clear on type / class ...?


See 'Custom Types' at that link.

A func, a property (var) of a class are class members.

To break this down:

  • A member is either a property or a method.

  • An instance member is a member of an instance of a class or a struct; sometimes folks say member when you they mean instance member, but it’s best to only do that when the context is clear.

  • A class member is a member of a class rather than an instance.

  • A static member is a member of a class or a struct; static members can’t that be overriden.

So, for example:

class MyClass {
    class var myClassProperty: Int { return 42 }
    class func myClassMethod() {
    }
    static var myStaticProperty: Int { return 42 }
    static func myStaticMethod() {
    }
    let myInstanceProperty: Int = 42
    func myInstanceMethod() {
    }
}

struct MyStruct {
    // Class members are only allowed within classes.
    //
    // class var myClassProperty: Int { return 42 }
    // class func myClassMethod() {
    // }
    static var myStaticProperty: Int { return 42 }
    static func myStaticMethod() {
    }
    let myInstanceProperty: Int = 42
    func myInstanceMethod() {
    }
}

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
What is a class member?
 
 
Q