Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中创建类数组_Ios_Swift - Fatal编程技术网

Ios 如何在Swift中创建类数组

Ios 如何在Swift中创建类数组,ios,swift,Ios,Swift,我创建了两个类,StepsCell和WeightCell import UIKit class StepsCell { let name = "Steps" let count = 2000 } import UIKit class WeightCell { let name = "Weight" let kiloWeight = 90 } 在我的VC中,我尝试创建一个数组Cellray来保存对象 import UIKit class TableVi

我创建了两个类,StepsCell和WeightCell

import UIKit

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


import UIKit

class WeightCell {

    let name = "Weight"
    let kiloWeight = 90
}
在我的VC中,我尝试创建一个数组Cellray来保存对象

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)
}
但当我索引到数组中时:

println(cellArray[0].name)

我得零分。。为什么?我如何创建一个数组来“保存”这些类,并将其编入索引以获取各种变量(以及稍后添加的函数)。我以为这会是一件非常简单的事情,但我找不到任何答案。有什么想法吗?

问题是您创建了一个混合类型的数组。因此,编译器不知道由
cellaray[0]
返回的对象的类型。它推断此对象的类型必须为
AnyObject
。显然,它有一个名为
name
的属性,返回nil

解决方案是强制转换它
println((Cellray[0]作为StepsCell).name)
,或者使用公共协议或超类:

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)

正如@Rengers在回答中所说,你可以使用这种方法, 要深入到您的代码,您可以这样解决它

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)
您的数组保存您创建的类的实例,因此您可以使用temp变量来提取这些值,或者为了使其更简单,您也可以这样做

((myArray[0]) as StepsCell ).name
由于您有两个类,并且在运行时我们喜欢保持事物的动态性,所以您可以添加一个条件运算符,它将标识您想要使用的对象的类型

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

}

你的数组没有保存“类”,它保存的是“类的实例”。好的,我明白了。cellArray[0]作为StepsCell.name)对我不起作用,因为cellArray[0]也可能是WeightCell。我知道什么是协议,但不知道什么是超类。然后我建议你先开始学习OOP:谢谢-我已经阅读了你推荐的教程。还感谢您以如此直观的方式解释编译器的逻辑。好吧,我喜欢您的思路,但实际上我有18个这样的类,所以也许协议更好?@karlml这真的很伤脑筋,我认为最好是为所有这18个类创建一个基类,然后将它们添加到数组中,这样可以简化很多事情,然后可以在运行时识别对象。好的,这很有趣。假设伤害意味着,伤害性能?一个基类有很多子类比很多单独的类好吗?@karlml如果你有一个基类有很多子类,那就很好了,性能取决于我们编码和迭代的方式,如果您非常关心内存相关的问题,您可以使用这些工具。