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'"
5
  • Nowhere in your code do you make any instances of the person class, so what exactly is the question? Commented Jan 28, 2015 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. Commented Jan 28, 2015 at 19:13
  • I thought that var testPerson: person would create an instance. This link seems to discuss how to create an instance but seems clunky...what is the best way to do this? Commented Jan 28, 2015 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: apeth.com/swiftBook/ch01.html Commented Jan 28, 2015 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! Commented Jan 28, 2015 at 19:23

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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.
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.
This is also the correct answer, if you modify your answer to reflect this I'll mark it as a secondary solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.