1

I currently have two structs and a class in my sample code. One struct is for Tiger, one is for Balloon and on is for Lion. Each are below:

struct Tiger {
    var name = ""
    var age = 0
    var breed = ""
    var image = UIImage(named: "")
    func chuff() {
        println("\(name): Chuff Chuff")
    }
    func chuffNumberOfTimes(numberOfTimes:Int) {
        for (var i = 0; i < numberOfTimes; i++) {
            self.chuff()
        }
    }
    func ageInTigerYearsFromAge(regularAge:Int) -> Int {
        return regularAge * 3
    }
    func randomFact() -> String {
        let randomFactNumber = Int(arc4random_uniform(UInt32(3)))
        var randomFact:String
        switch randomFactNumber {
        case 0:
            randomFact = "Tigers are 10 feet tall."
        case 1:
            randomFact = "Tigers are amazing."
        case 2:
            randomFact = "Tigers have 10 feet."
        default:
            randomFact = ""
        }
        return randomFact
    }
}
struct Balloon {
    var number = 0
    var image = UIImage(named: "")
}
class Lion {
    var name = ""
    var age = 0
    var isAlphaMale = false
    var image = UIImage(named: "")
    var subSpecies = ""
}

Each one is in its own file named identically to the struct/class & ".swift". However, the only one that autocompletes itself while typing in ViewController.swift is the Tiger struct. For instance, if I were to set myTiger = Tiger( it would suggest name: String, age: Int etc. How can I get the other two to do the same? I really like defining the variables inline instead of having 5 lines of code to define all of the variables.

Is anyone familiar with how that works or why the Tiger struct would do it while the Balloon and Lion structs don't?

Icaro
  • 14,585
  • 6
  • 60
  • 75
jaysoncopes
  • 805
  • 3
  • 13
  • 26

1 Answers1

2

Classes don't get the initializers for free as the Struct does, if you want to have an initializer for a class you have to create it:

class Lion {
    var name = ""
    var age = 0
    var isAlphaMale = false
    var image = UIImage(named: "")
    var subSpecies = ""

    init(name: String, age:Int, isAlphaMale: Boolean, image: UIImage, subSpecies: String){
        self.name = name 
        self.age = age 
        self.isAlphaMale = isAlphaMale 
        self.image = image 
        self.subSpecies = subSpecies 
    }
} 

The Balloon should work automatically, probably a clean and build in your code so xcode can be updated will fix the problem.

From Apple Documentation:

Memberwise Initializers for Structure Types

All structures have an automatically-generated memberwise initializer, which you can use to initialize the member properties of new structure instances. Initial values for the properties of the new instance can be passed to the memberwise initializer by name:


copied from a previus part of the document to add context

struct Resolution { var width = 0 var height = 0 }


let vga = Resolution(width: 640, height: 480)

Unlike structures, class instances do not receive a default memberwise initializer.

With the class above you will have the initializer as in the screenshot below:

enter image description here

As you can see in the screenshot Balloon works perfectly fine as well

enter image description here

No need to enter once to start work, xcode is not perfect and sometimes it needs a clean and a new build to make it be in sync with your classes and structs.

Icaro
  • 14,585
  • 6
  • 60
  • 75
  • Partially answers, but does this explain why the Balloon struct isn't doing it either? – jaysoncopes Jun 22 '15 at 06:16
  • Nope, just ran the code and still doesn't suggest Balloon variables. – jaysoncopes Jun 22 '15 at 06:26
  • @jaysoncopes did you clean and build? Just run does not recompile any precompiled code – Icaro Jun 22 '15 at 06:27
  • @Icaro if a class is a base class(no superclass) then it does receive a free init(), but all properties must have defaults – fingaz Jun 22 '15 at 06:31
  • @fingaz, no that is not true, I just update the answer with the reference from apple documentation – Icaro Jun 22 '15 at 06:34
  • @Icaro from the same documentation: Default Initializers Swift provides a default initializer for any structure or base class that provides default values for all of its properties and does not provide at least one initializer itself. The default initializer simply creates a new instance with all of its properties set to their default values.[Apple Documentation](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html) – fingaz Jun 22 '15 at 06:38
  • @Icaro still can't mark this as answer: Clean and Build took away a lot of random options for the Lion class, but didn't give initializer (as @fingaz pointed out), but it didn't work for the Balloon struct in another project. When typing `var testBalloon = Balloon(` I still do not receive the option to autocomplete for the struct. – jaysoncopes Jun 22 '15 at 06:41
  • @fingaz it does create a initializer but not a memberwise initializer where you can pass values but an empty initializer that set the values to the default values "init()" where you call "let lion = Lion()" – Icaro Jun 22 '15 at 06:50
  • @jaysoncopes what fingaz point out is an default initializer you want a memberwise initializer, they are different things – Icaro Jun 22 '15 at 06:54
  • @Icaro still does not answer my question about the Balloon **struct**. – jaysoncopes Jun 22 '15 at 06:54
  • @jaysoncopes its working on my end for the autocomplete only after I have manually entered the init once so if I do `var tiger = Tiger(name: ...)` but entering the line manually and then go `var tiger2 = Tiger(` then I get the auto complete to occur. It seems to not be happening unless I have previously used the initializer. Tiger was an example it works for both structs. For Lion @Icaro was correct, sorry for the distraction – fingaz Jun 22 '15 at 07:46
  • Yeah, not sure why mine wasn't working. I haven't tested in a while, but went ahead and gave you the answer because of completeness. – jaysoncopes Jun 30 '15 at 14:32
  • 1
    @jaysoncopes Thanks, sometimes XCode can do strange things, normally a clean and build and/or a restart solve it. Sometimes is just easier to start a whole new project. Good luck finish your app! – Icaro Jun 30 '15 at 22:06