4

I'm having a problem while save the RecordID into NSUserDefault and than retrieving it and list on a table view.

in the method:

func peoplePickerNavigationController( peoplePicker: ABPeoplePickerNavigationController!, didSelectPerson person: ABRecordRef!) {

i Have the follow code to store de id of the person selected

let contactID: ABRecordID = ABRecordGetRecordID(firstName)
if contactID != kABRecordInvalidID{
    var numberID: NSNumber = NSNumber(int: contactID)
    println(numberID.integerValue)
    contatosRecentes.append(numberID.integerValue)
}
NSUserDefaults.standardUserDefaults().setObject(contatosRecentes, forKey: "rf")

contatosRecentes is

var contatosRecentes = [Int]()

I've tested the code and it is storing the id. the problem is when i try to get the name of the person using the code:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        var recordID: Int = contatosRecentes[indexPath.row]
        var numberID: ABRecordID = NSNumber(integer: recordID).intValue

        var errorCM: UnsafePointer<Unmanaged<CFError>?> = nil

        var addressBook = ABAddressBookCreateWithOptions(nil, errorCM)
        var addressBookRef: ABAddressBookRef? = Unmanaged<NSObject>.fromOpaque(addressBook.toOpaque()).takeUnretainedValue()

        var person = ABAddressBookGetPersonWithRecordID(addressBookRef, numberID)
        var personRef: ABRecordRef = Unmanaged<NSObject>.fromOpaque(person.toOpaque()).takeUnretainedValue() as ABRecordRef

        var personName = ABRecordCopyValue(personRef, kABPersonFirstNameProperty)
        var firstName: ABRecordRef = Unmanaged.fromOpaque(personName.toOpaque()).takeUnretainedValue() as NSString as ABRecordRef

        cell.textLabel.text = firstName as? String
        cell.imageView.image = UIImage(named: "img_profile.png")
        cell.imageView.layer.cornerRadius = 21
        cell.imageView.clipsToBounds = true;

        return cell
    }

The problem is the variable firstName is Empty.

ps.: i've a lot of contacts in iPhone simulator

Thanks!

rob5408
  • 2,886
  • 2
  • 37
  • 50
marchiore
  • 544
  • 1
  • 5
  • 20

0 Answers0