这是我的代码:
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的一个实例吗?不需要打印阵列,但是很难测试我的数据并确保我得到正确的结果。
答案 0 :(得分:0)
您应override
或description
debugDescription
中的一个var
。由于RunDetail
是您的自定义类,因此您有责任为您的班级编写任何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)
打印出来。