为什么我的数组打印对象位置而不是值?

Why is my array printing object locations and not the value?

这是我的代码:

import Foundation
import MapKit
import Contacts

class RunDetail: NSObject, MKAnnotation {

let runName: String?
let coordinate: CLLocationCoordinate2D
let user: String
let distance: Int
let time: Int

init(runName: String, coordinate: CLLocationCoordinate2D, user: String, distance: Int, time: Int)   {

    self.runName = runName
    self.coordinate = coordinate
    self.user = user
    self.distance = distance
    self.time = time

}

var title: String?   {

    return runName

}

var subtitle: String?   {

    return ("Distance: \(distance)m in \(time) seconds, completed by \(user)")

   }

}  

这是来自视图控制器:

var runs = [RunDetail]()
func test()  {

    databaseHandle = databaseRef.child("RunList").observe(.value, with: { (snapshot) in

        self.count = Int(snapshot.childrenCount)

        for child in snapshot.children.allObjects as! [FIRDataSnapshot]  {

            let childDict = child.value as? [String: Any]
            let name = childDict?["name"] as? String
            let rundistance = childDict?["distance"] as? Int
            let runtime = childDict?["time"] as? Int
            let runuser = childDict?["user"] as? String
            let runlat = childDict?["startLat"] as? Double
            let runlong = childDict?["startLong"] as? Double
            let runcoordinate = CLLocationCoordinate2D(latitude: runlat!, longitude: runlong!)

            let runDet = RunDetail(runName: name!, coordinate: runcoordinate, user: runuser!, distance: rundistance!, time: runtime!)
            self.runs.append(runDet)
            print(self.runs)


        }
    })

}

我知道数组将在循环的每次迭代中打印出来,但我想看看它是否正常工作。问题是,我希望数组打印出我添加的内容的实际值。这是我在 运行 test():

之后循环的最后一次迭代中得到的输出示例
[<Sprint3.RunDetail: 0x608000ecbd00>, <Sprint3.RunDetail: 0x6080010c07e0>, <Sprint3.RunDetail: 0x6080010c0850>, <Sprint3.RunDetail: 0x6080010c08c0>, <Sprint3.RunDetail: 0x6080010c0930>, <Sprint3.RunDetail: 0x6080010c0a10>]

为什么它返回我认为是值的位置而不是值本身?目标是数组包含一堆 MKAnnotations,我将能够通过数组 'runs' 添加每个注释来轻松显示它们。是因为 RunDetail 也是 MKAnnotation 的实例吗?不需要打印数组,但它很难测试我的数据并确保我得到正确的结果。

您应该 override descriptiondebugDescription 之一 var。由于 RunDetail 是您的习惯 class,您有责任为您的 class 编写任何 description。例子如下:

override var description : String { 
    return "the values in String you want to be printed"  //Make a meaningful String with the values that you want to be printed
}

override var debugDescription : String { 
    return "the values in String you want to be printed"  //Make a meaningful String with the values that you want to be printed
}

然后在你的循环中,使用print(self.runs.description)打印出来。