【发布时间】:2017-04-24 14:25:24
【问题描述】:
我目前有一个从数据库中收集时间并将其返回以供其他函数使用的函数。它需要一个参数,该参数存储在应用程序的另一部分,以便从数据库中收集值。
当我想在 IBAction 函数中调用此函数时,我的问题就出现了。
这是我的函数代码:
func getDBValue(place: GMSPlace) -> Int {
var expectedValue = 0
databaseRef.child("values").child(place.placeID).observe(.value, with: { (snapshot) in
let currentValue = snapshot.value as? [Int]
if currentValue == nil {
self.noValue()
expectedValue = 0
} else {
let sumValue = currentValue?.reduce(0, +)
let avgValue = sumValue! / (currentValue?.count)!
print("The current value is \(String(describing: avgValue))")
expectedValue = avgValue
self.valueLabel.text = String(describing: avgValue)
}
})
print("This is the expected WT: \(expectedWaitTime)")
return expectedValue
}
这是我的 IBAction 函数的代码,它存在多个参数的问题:
@IBAction func addValuePressed(_ sender: Any, place: GMSPlace) {
print("This is the place ID: \(place.placeID)")
var expectedValue = getDBValue(place: place)
expectedValue = expectedValue + 1
print("The expectedValue is now: \(expectedValue)")
self.valueLabel.text = String(describing: expectedValue)
}
这给了我一个libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) 错误。经过一些测试,该错误似乎是由我的 IBAction 函数中添加的参数place: GMSPlace 引起的。有关如何解决此问题的任何想法?
【问题讨论】:
-
您不能将参数添加到 IBAction。时期。根据您的需要,通常的解决方法是 (1) 使用 tag 属性(它是
Int)来指示调用了哪个发件人,或者 (2) 使您的GMSPlace实例在 IBAction 中可用。
标签: swift parameters ibaction