Swift - 检查nil的非托管地址簿单值属性 [英] Swift - Checking unmanaged address book single value property for nil

查看:154
本文介绍了Swift - 检查nil的非托管地址簿单值属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对iOS-Development和swift相对较新。但到目前为止,我总是能够通过对stackoverflow和一些文档和教程的研究来帮助自己。
但是,有一个问题我还没找到任何解决方案。

I'm relative new to iOS-Development and swift. But up to this point I was always able to help myself by some research on stackoverflow and several documentations and tutorials. However, there is a problem I couldn't find any solution yet.

我想从用户地址簿中获取一些数据(例如单值) property kABPersonFirstNameProperty )。因为如果此联系人在地址簿中没有firstName值, .takeRetainedValue()函数会抛出错误,我需要确保 ABRecordCopyValue ()函数确实返回一个值。我试图在闭包中检查这个:

I want to get some data from the users addressbook (for example the single value property kABPersonFirstNameProperty). Because the .takeRetainedValue() function throws an error if this contact doesn't have a firstName value in the addressbook, I need to make sure the ABRecordCopyValue() function does return a value. I tried to check this in a closure:

let contactFirstName: String = {
   if (ABRecordCopyValue(self.contactReference, kABPersonFirstNameProperty) != nil) {
      return ABRecordCopyValue(self.contactReference, kABPersonFirstNameProperty).takeRetainedValue() as String
   } else {
      return ""
   }
}()

contactReference 是一个变量键入 ABRecordRef!

当地址簿联系人提供firstName值时,一切正常。但是如果没有firstName,则应用程序会因 .takeRetainedValue()函数而崩溃。似乎if语句没有帮助,因为 ABRecordCopyValue()函数的非托管返回值不是nil,尽管没有firstName。

When an addressbook contact provides a firstName value, everything works fine. But if there is no firstName, the application crashes by the .takeRetainedValue() function. It seems, that the if statement doesn't help because the unmanaged return value of the ABRecordCopyValue() function is not nil although there is no firstName.

我希望我能够清楚地解决问题。如果有人能帮助我解决一些脑波问题会很棒。

I hope I was able to make my problem clear. It would be great if anyone could help me out with some brainwave.

推荐答案

如果我想要与各种属性相关的值,我使用以下语法:

If I want the values associated with various properties, I use the following syntax:

let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String
let last  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String

或者您可以使用可选绑定:

Or you can use optional binding:

if let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String {
    // use `first` here
}
if let last  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String {
    // use `last` here
}

如果你真的想要返回一个非可选的,其中缺失值是零长度字符串,你可以使用 ?? 运算符:

If you really want to return a non-optional, where missing value is a zero length string, you can use the ?? operator:

let first = ABRecordCopyValue(person, kABPersonFirstNameProperty)?.takeRetainedValue() as? String ?? ""
let last  = ABRecordCopyValue(person, kABPersonLastNameProperty)?.takeRetainedValue() as? String ?? ""

这篇关于Swift - 检查nil的非托管地址簿单值属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆