ios - Swift 在另一个文件中调用公共(public)类

标签 ios class swift public

在 Swift 编程中。我创建了一个名为 Array3d 的公共(public)类,它位于 Array3d.swift 文件中。我的 ViewController2.swift 通过使用 Array3d 类和我的 println(dog) 创建了一个名为 dog 的变量。但是,我的控制台不显示 dog 数组的值,而是显示 FYP_Table.Array3D。

我创建了一个名为 dog 的 3D 数组,并向其中插入了一些值。现在我想将它打印到我的调试器输出。但不起作用。

// Array3d.swift 
    public class Array3D {
        var zs:Int, ys:Int, xs:Int
        var matrix: [Float]

        init(zs: Int, ys:Int, xs:Int) {
            self.zs = zs
            self.ys = ys
            self.xs = xs
            matrix = Array(count:zs*ys*xs, repeatedValue:0)
        }

        subscript(z:Int, y:Int, x:Int) -> Float {
            get {
                return matrix[ z * ys * xs + y * xs + x ]
            }
            set {
                matrix[ z * ys * xs + y * xs + x ] = newValue
            }
        }

//ViewController2.swift

    class ViewController2: UITableViewController {

    var dog = Array3D(zs: 3, ys: 3, xs: 3)


    override func viewDidLoad() {

    dog[1,0,0] = 1
    dog[0,4,0] = 2
    dog[0,0,4] = 3
    dog[0,4,4] = 4

      for z in 0..<3 {
       for y in 0..<3 {
        for x in 0..<3 {
         println(self.dog)
               }
             }
           }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

    }

最佳答案

您的类没有描述,所以它只是打印出默认表示。

func description() -> String {
  return String(format: "Array3D zs:%d ys:%d xs:%d", self.zs, self.ys, self.zs)
}

关于ios - Swift 在另一个文件中调用公共(public)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28092887/

相关文章:

ios - 每 10 个单元格重复 TableView

ios - 带有 spritebuilder 的无标签,无法弄清楚如何修复它

ios - 自定义 UITableViewCell,单元格中的按钮不起作用

ios - 无 url 编码的 Moya Alamofire 请求

python - 为什么 __del__ 不能正常工作

javascript 无法设置私有(private)变量

c++ - 删除指向流的类成员指针的段错误

ios - Swift 3 - 线程 1 : EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, 子代码 = 0x0)

ios - Swift:UILabel,闪烁文本颜色动画

ios - 将项目复制到新笔记本电脑后出现 Apple Mach-O 链接器错误