bubakazouba
bubakazouba

Reputation: 1683

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)

I get an error message now

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

what's the alternative then?

Upvotes: 4

Views: 2780

Answers (2)

Nick
Nick

Reputation: 71

I actually had a similar problem with the line:

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

Which compiled after I simply removed the .takeRetainedValue().

Probably because dataTypeRef! is unwrapped already.

Upvotes: 3

matt
matt

Reputation: 535306

The error message is very clear. You can't say takeUnretainedValue to an ABRecord?.

Here's the declaration:

ABRecordWithAddressBook(_ addressBook: ABAddressBook) -> ABRecord?

So this thing yields an Optional. You have to unwrap it. This might work:

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

But then I expect you'll have a new problem; you now have the actual ABRecord, completely with memory management. So you should also cut the takeUnretainedValue(), leaving this:

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

Upvotes: 1

Related Questions