0

I'm trying to use Swift to create an instance of a class (the class being the desired type) but it would seem that when I initialize the instance the class var is not applied to the new instance. I'm sure there's an init call or something that I'm missing, so any help would be greatly appriciated.

class Person: NSObject {

    private struct personNameStruct { static var _personName: String = "" }

    class var personName: String
        {
        get { return personNameStruct._personName }
        set { personNameStruct._personName = newValue }
    }

}

var testPerson: Person
testPerson.personName = "Foo"   //"'person' does not have a member named 'personName'"
mattchue
  • 121
  • 2
  • 10
  • Nowhere in your code do you make any instances of the `person` class, so what exactly is the question? – matt Jan 28 '15 at 19:12
  • Also, your code would be a lot easier for others to understand if you would obey the rules: type names start with a capital letter. – matt Jan 28 '15 at 19:13
  • I thought that `var testPerson: person` would create an instance. [This link](http://stackoverflow.com/questions/24353254/how-to-pass-a-class-and-method-to-create-an-instance-of-a-class) seems to discuss how to create an instance but seems clunky...what is the best way to do this? – mattchue Jan 28 '15 at 19:15
  • Okay, so you don't even know how to instantiate? You need to start right at the beginning! Here's my Swift tutorial: the very first chapter will clear up a lot of confusions you seem to have: http://www.apeth.com/swiftBook/ch01.html – matt Jan 28 '15 at 19:18
  • Working through the basics, the spot I'm in has led to a very sporadic understanding of the basics. I will give this a read, and your answer solved my issue. Thank you! – mattchue Jan 28 '15 at 19:23

2 Answers2

1

An instance member is referred to through a reference to an instance.

A class member is referred to through a reference to the class.

So, for example:

class Dog {
    class var whatDogsSay : String {
        return "Woof"
    }
    func bark() {
        println(Dog.whatDogsSay)
    }
}

To make a dog bark, make a dog instance and tell it to bark:

let d = Dog()
d.bark()

To find out what dogs say, talk to the dog class:

let s = Dog.whatDogsSay
matt
  • 515,959
  • 87
  • 875
  • 1,141
0

It works for me in a Playground if you access the personName variable using the class name person, not the instance name: person.personName = "Foo".

This is because a class variable in Swift is similar to a static variable in languages like Java and C#, in that it is shared between all instances of that class. If you just want a property in your class you shouldn't declare it as class var but just var.

Joru
  • 4,406
  • 1
  • 19
  • 13
  • Yes, but this is referencing the `person` class, not the `testPerson` object, meaning that you cannot have more than one `person` object, or at least that's how it would seem to me. – mattchue Jan 28 '15 at 19:11
  • 1
    If I remember correctly, a class variable in Swift is similar to a static variable in languages like Java and C#, in that it is shared between all instances of that class. If you just want a property in your class you shouldn't declare it as `class var` but just `var`. – Joru Jan 28 '15 at 19:13
  • 1
    This is also the correct answer, if you modify your answer to reflect this I'll mark it as a secondary solution – mattchue Jan 28 '15 at 19:25