我目前有一个函数,它从数据库收集一个时间并返回给其他函数使用。它需要一个参数,该参数存储在应用程序的另一部分中,以便从数据库中收集值。IBAction函数上的多个参数
我想在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)使用标签属性(它是一个'Int')来指示哪个发件人被调用,或者(2)使您的'GMSPlace'实例在IBAction中可用。 – dfd