Trying to use takeUnretainedValue () in swift 2

that was my code in swift 1.2

let record = attendee.ABRecordWithAddressBook(addressBookController.adbk)!
let unmanagedValue = ABRecordCopyValue(record.takeUnretainedValue(), kABPersonEmailProperty)

Now i get an error

Value of type 'ABrecord' has no member 'takeUnretainedValue'

what is the alternative?

+4
source share
2 answers

I had a similar problem with the line:

let retrievedData : NSData = dataTypeRef!.takeRetainedValue() as! NSData

Compiled after I just deleted .takeRetainedValue ().

Perhaps because dataTypeRef! already deployed.

+2
source

The error message is very clear. You can not say takeUnretainedValue ABRecord?.

Here's the announcement:

ABRecordWithAddressBook(_ addressBook: ABAddressBook) -> ABRecord?

So this thing gives an optional. You have to deploy it. This might work:

if let record = attendee.ABRecordWithAddressBook(addressBookController.adbk) {
    let unmanagedValue = ABRecordCopyValue(record.takeUnretainedValue(), kABPersonEmailProperty)
}

But then I expect that you will have a new problem; Now you have a real ABRecord, completely with memory management. Therefore, you should also cut off takeUnretainedValue(), leaving this:

if let record = attendee.ABRecordWithAddressBook(addressBookController.adbk) {
    let unmanagedValue = ABRecordCopyValue(record, kABPersonEmailProperty)
}
+1

Source: https://habr.com/ru/post/1607925/


All Articles