KML
KML

Reputation: 2322

How can I create an array of classes in Swift

I have created two classes, StepsCell and WeightCell

import UIKit

class StepsCell {
    let name = "Steps"
    let count = 2000
}


import UIKit

class WeightCell {

    let name = "Weight"
    let kiloWeight = 90
}

In my VC I attempt to create an array, cellArray, to hold the objects.

import UIKit

class TableViewController: UIViewController {

    var stepsCell = StepsCell()
    var weightCell = WeightCell()

    override func viewDidLoad() {
        super.viewDidLoad()

        var cellArray = [stepsCell, weightCell]
        println(StepsCell().name)
        println(stepsCell.name)
        println(cellArray[0].name)
}

but when I index into the array :

println(cellArray[0].name)

I get nil.. Why? How can I create an array that "holds" these classes and that I can index into to get the various variables (and functions to be added later). I thought this would be a super simple thing but I can't find any answers for this. Any ideas?

Upvotes: 3

Views: 11384

Answers (2)

Bug Hunter Zoro
Bug Hunter Zoro

Reputation: 1915

As @Rengers said in his answer you may use the approach, To drill down to your code you can solve it like this,

class StepsCell {
let name = "Steps"
let cellCount = 2000
}

class WeightCell {
let name = "Weight"
let weightCount = 90
}


var stepcell = StepsCell() // creating the object
var weightcell = WeightCell()


// Your array where you store both the object
var myArray = [stepcell,weightcell];

// creating a temp object for StepsCell
let tempStepCell = myArray[0] as StepsCell

println(tempStepCell.name)

Your array is holding the instance of the classes which you created so you may use a temp variable to extract those values OR to make it more simpler you may also do something like this

((myArray[0]) as StepsCell ).name

Since you have two classes and at run time we just love to keep things dynamic you can add a conditional operator which would identify the type of object you want to work with

if let objectIdentification = myArray[0] as? StepsCell {
println("Do operations with steps cell object here")
}else{
println("Do operation with weight cell object here")

}

Upvotes: 2

Rengers
Rengers

Reputation: 15218

The problem is that you create an array with mixed types. Because of this, the compiler doesn't know the type of the object returned by cellArray[0]. It infers that this object must be of type AnyObject. Apparently this has a property named name, which returns nil.

The solution is to either cast it println((cellArray[0] as StepsCell).name), or use a common protocol or superclass:

protocol Nameable {
    var name: String { get }
}

class StepsCell: Nameable {
    let name = "Steps"
    let count = 2000
}

class WeightCell: Nameable {
    let name = "Weight"
    let kiloWeight = 90
}

var stepsCell = StepsCell()
var weightCell = WeightCell()

var cellArray: [Nameable] = [stepsCell, weightCell]
println(StepsCell().name)
println(stepsCell.name)
println(cellArray[0].name)

Upvotes: 10

Related Questions