[英]Fetching contact no crashes when there is no phone no assigned to the contact
I am trying to read the contact phone no.我正在尝试阅读联系电话号码。 The below code works fine if the number is available but if there are no phone no assigned to the contact , it is crashing.如果号码可用,下面的代码工作正常,但如果没有分配给联系人的电话,它就会崩溃。 Can Anu let me know how to handle this. Anu 能否让我知道如何处理这个问题。
((currentContact.phoneNumbers.first?.value)! as CNPhoneNumber).stringValue
You should learn about optional values in Swift...您应该了解 Swift 中的可选值...
if let contactPhoneNumber = currentContact.phoneNumbers.first?.value?.stringValue {
// do something with the value
}
else {
// the value isn't there
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.