如何从单元格中检索 detailedText 然后在函数中使用它?
How to retrieve the detailedText from a cell then use it in a function?
我正在尝试检索包含在 UITableView 单元格中的 detailedText(这是一个 phone 数字“字符串”),然后在将进行 phone 调用的函数中使用它。
问题:
我的应用程序不断崩溃并出现错误 “致命错误:在展开可选值时意外发现 nil”
即使变量中有一个值。
我确定我在强制解开可选选项时做错了什么
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
let getPhone = cell?.detailTextLabel?.text
if indexPath.section == 0 && indexPath.row == 0 {
if let phoneNumber = getPhone{
openPhoneApp(phoneNumber)
}
}
// Open Phone App
func openPhoneApp(phoneNum: String){
UIApplication.sharedApplication().openURL(NSURL(string: "tel:\(phoneNum)")!)
}
您检查过 phoneNumber 变量的值了吗?可能是零。
你可以看看这个答案:我想这会解决你的问题。
特别是这个答案
请使用 tel:// 方案并始终拨打
UIApplication.sharedApplication().canOpenURL
之前
如果您不能 100% 确定它可以解包,请不要使用强制解包。并且尽可能避免它!
您的 openPhoneApp
函数必须接收一个非 nil 字符串,所以到那时为止一切正常。
尝试用这样的东西替换你的力展开:
func openPhoneApp(phoneNum: String) {
guard let url = NSURL(string: "tel:\(phoneNum)") else {
print("badly formed telephone url")
return
}
UIApplication.sharedApplication().openURL(url)
}
尽管我认为您的函数名称暗示它会打开 phone 应用程序,所以也许您应该去请求一个正确格式的 URL,如下所示:
func openPhoneApp(phoneURL: NSURL) {
UIApplication.sharedApplication().openURL(phoneURL)
}
并在调用之前检查此类内容:
if let phone = getPhone, phoneURL = NSURL(string: "tel:\(phone)") {
openPhoneApp(phoneURL)
}
我正在尝试检索包含在 UITableView 单元格中的 detailedText(这是一个 phone 数字“字符串”),然后在将进行 phone 调用的函数中使用它。
问题:
我的应用程序不断崩溃并出现错误 “致命错误:在展开可选值时意外发现 nil” 即使变量中有一个值。
我确定我在强制解开可选选项时做错了什么
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
let getPhone = cell?.detailTextLabel?.text
if indexPath.section == 0 && indexPath.row == 0 {
if let phoneNumber = getPhone{
openPhoneApp(phoneNumber)
}
}
// Open Phone App
func openPhoneApp(phoneNum: String){
UIApplication.sharedApplication().openURL(NSURL(string: "tel:\(phoneNum)")!)
}
您检查过 phoneNumber 变量的值了吗?可能是零。
你可以看看这个答案:我想这会解决你的问题。
特别是这个答案
请使用 tel:// 方案并始终拨打
UIApplication.sharedApplication().canOpenURL
之前
如果您不能 100% 确定它可以解包,请不要使用强制解包。并且尽可能避免它!
您的 openPhoneApp
函数必须接收一个非 nil 字符串,所以到那时为止一切正常。
尝试用这样的东西替换你的力展开:
func openPhoneApp(phoneNum: String) {
guard let url = NSURL(string: "tel:\(phoneNum)") else {
print("badly formed telephone url")
return
}
UIApplication.sharedApplication().openURL(url)
}
尽管我认为您的函数名称暗示它会打开 phone 应用程序,所以也许您应该去请求一个正确格式的 URL,如下所示:
func openPhoneApp(phoneURL: NSURL) {
UIApplication.sharedApplication().openURL(phoneURL)
}
并在调用之前检查此类内容:
if let phone = getPhone, phoneURL = NSURL(string: "tel:\(phone)") {
openPhoneApp(phoneURL)
}