Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从单元格中检索detailedText,然后在函数中使用它?_Ios_Swift_Uitableview_Uiapplication - Fatal编程技术网

Ios 如何从单元格中检索detailedText,然后在函数中使用它?

Ios 如何从单元格中检索detailedText,然后在函数中使用它?,ios,swift,uitableview,uiapplication,Ios,Swift,Uitableview,Uiapplication,我试图检索UITableView手机中包含的detailedText(这是一个电话号码“字符串”),然后将其用于打电话的函数中 问题: 我的应用程序不断崩溃,错误为“致命错误:在展开可选值时意外发现零” 即使变量中有一个值 我敢肯定我在强行展开可选组件时出错了 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cel

我试图检索UITableView手机中包含的detailedText(这是一个电话号码“字符串”),然后将其用于打电话的函数中

  • 问题:

    我的应用程序不断崩溃,错误为“致命错误:在展开可选值时意外发现零” 即使变量中有一个值

我敢肯定我在强行展开可选组件时出错了

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变量的值?可能是零

你可以检查这个答案:我想这会解决你的问题

特别是这个答案


您是否检查了phoneNumber变量的值?可能是零

你可以检查这个答案:我想这会解决你的问题

特别是这个答案


请使用电话://scheme并随时拨打

UIApplication.sharedApplication().canOpenURL

之前,请使用电话://scheme并始终拨打

UIApplication.sharedApplication().canOpenURL

之前,如果您不能100%确定可以展开,请不要使用强制展开。如果可能的话,尽量避免

您的
openPhoneApp
函数必须接收一个非零字符串,因此到那时一切都正常

尝试用以下内容替换“强制展开”:

func openPhoneApp(phoneNum: String) {
    guard let url = NSURL(string: "tel:\(phoneNum)") else {
        print("badly formed telephone url")
        return
    }
    UIApplication.sharedApplication().openURL(url)
}
func openPhoneApp(phoneURL: NSURL) {
    UIApplication.sharedApplication().openURL(phoneURL)
}

虽然我认为你的函数名意味着它将打开手机应用程序,但也许你应该去请求一个格式正确的URL,如下所示:

func openPhoneApp(phoneNum: String) {
    guard let url = NSURL(string: "tel:\(phoneNum)") else {
        print("badly formed telephone url")
        return
    }
    UIApplication.sharedApplication().openURL(url)
}
func openPhoneApp(phoneURL: NSURL) {
    UIApplication.sharedApplication().openURL(phoneURL)
}
在打电话之前检查一下这类东西:

if let phone = getPhone, phoneURL = NSURL(string: "tel:\(phone)") {
    openPhoneApp(phoneURL)
}

如果不能100%确定可以展开,请不要使用强制展开。如果可能的话,尽量避免

您的
openPhoneApp
函数必须接收一个非零字符串,因此到那时一切都正常

尝试用以下内容替换“强制展开”:

func openPhoneApp(phoneNum: String) {
    guard let url = NSURL(string: "tel:\(phoneNum)") else {
        print("badly formed telephone url")
        return
    }
    UIApplication.sharedApplication().openURL(url)
}
func openPhoneApp(phoneURL: NSURL) {
    UIApplication.sharedApplication().openURL(phoneURL)
}

虽然我认为你的函数名意味着它将打开手机应用程序,但也许你应该去请求一个格式正确的URL,如下所示:

func openPhoneApp(phoneNum: String) {
    guard let url = NSURL(string: "tel:\(phoneNum)") else {
        print("badly formed telephone url")
        return
    }
    UIApplication.sharedApplication().openURL(url)
}
func openPhoneApp(phoneURL: NSURL) {
    UIApplication.sharedApplication().openURL(phoneURL)
}
在打电话之前检查一下这类东西:

if let phone = getPhone, phoneURL = NSURL(string: "tel:\(phone)") {
    openPhoneApp(phoneURL)
}

在函数的开始处放置一个断点,如果I为nil,则检查每个属性。我看到的唯一强制展开是在NSURL中,因此您可能创建了一个格式错误的URL。顺便说一句:不要强制展开内容Xcode建议展开它:“可选类型'NSURL'的值?未展开”我假设您使用的是UITableView,因此您有一个数组来填充数据,那么,你为什么不直接从数组或数据结构中检索indexPath处的数据呢?使用一个漂亮的
if let
guard
语句展开它。在函数的开头放一个断点,如果I为零,则检查每个属性。我看到的唯一强制展开是在NSURL中,因此,您可能创建了一个格式错误的URL。顺便说一句:不要强制展开内容Xcode建议展开它:“可选类型'NSURL'的值?未展开”我假设您使用的是UITableView,因此您有一个数组来填充数据,那么,您不直接从数组或数据结构中检索该索引XPath处的数据吗?如果let或
guard
语句,请使用漂亮的
将其展开。phoneNumber不是nil,它包含我要使用的字符串phoneNumber不是nil,它包含我要使用的字符串我不想使用textLabel,因为它只是一个标题,我需要检索包含我要使用的电话号码的detailTextLabel我不想使用textLabel,因为它只是一个标题,我需要检索包含我要使用的电话号码的detailTextLabel是,我从手机中检索到的电话号码的格式如下“(510)2222222”。现在我去掉了数字之间的空格,在调用“openPhoneApp”之前,我也去掉了(510)周围的两个括号。是的,我从手机中检索到的电话号码的格式是这样的“(510)222222”。现在我去掉了数字之间的空格,在调用“openPhoneApp”之前,我也去掉了(510)周围的两个括号。