Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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 当没有电话号码分配给联系人时,获取联系人无崩溃_Ios_Swift_Cncontact - Fatal编程技术网

Ios 当没有电话号码分配给联系人时,获取联系人无崩溃

Ios 当没有电话号码分配给联系人时,获取联系人无崩溃,ios,swift,cncontact,Ios,Swift,Cncontact,我正在尝试读取联系人的电话号码。如果号码可用,下面的代码可以正常工作,但是如果没有为联系人分配电话号码,则会崩溃。Anu能告诉我怎么处理吗 ((currentContact.phoneNumbers.first?.value)! as CNPhoneNumber).stringValue 您应该了解Swift中的可选值 if let contactPhoneNumber = currentContact.phoneNumbers.first?.value?.stringValue {

我正在尝试读取联系人的电话号码。如果号码可用,下面的代码可以正常工作,但是如果没有为联系人分配电话号码,则会崩溃。Anu能告诉我怎么处理吗

((currentContact.phoneNumbers.first?.value)! as CNPhoneNumber).stringValue

您应该了解Swift中的可选值

if let contactPhoneNumber = currentContact.phoneNumbers.first?.value?.stringValue {
    // do something with the value
} 
else {
    // the value isn't there 
}

你应该没问题!尽管使用Swift最重要的是可选链接,但如果您不了解这一点(可选链接,如果…),我强烈建议您深入阅读文献。