0

我正在使用 contactPicker 函数从联系人中挑选并将它们添加到我创建的 tableview 中。但是,当我运行我的应用程序并单击所需的联系人时,我的应用程序崩溃了。问题似乎是 contactPicker(CNContactPickerViewController, didSelect...) 功能。虽然当我查看 swift 文件时没有出现错误。有人可以帮助解决问题吗?

Lookout`Lookout.ViewTwo.contactPicker (Lookout.ViewTwo)(__ObjC.CNContactPickerViewController, didSelectContact : __ObjC.CNContact) -> ():
0x103ffff10 <+0>:   pushq  %rbp
0x103ffff11 <+1>:   movq   %rsp, %rbp
0x103ffff14 <+4>:   subq   $0x90, %rsp
0x103ffff1b <+11>:  movq   0x92ae(%rip), %rax        ; (void *)0x0000000106eae2e0: swift_isaMask
0x103ffff22 <+18>:  movq   %rdi, -0x8(%rbp)
0x103ffff26 <+22>:  movq   %rsi, -0x10(%rbp)
0x103ffff2a <+26>:  movq   %rdx, -0x18(%rbp)
0x103ffff2e <+30>:  movq   (%rdx), %rcx
0x103ffff31 <+33>:  andq   (%rax), %rcx
0x103ffff34 <+36>:  movq   %rdi, -0x28(%rbp)
0x103ffff38 <+40>:  movq   %rdx, %rdi
0x103ffff3b <+43>:  movq   %rsi, -0x30(%rbp)
0x103ffff3f <+47>:  movq   %rdx, -0x38(%rbp)
0x103ffff43 <+51>:  callq  *0x78(%rcx)
0x103ffff46 <+54>:  movq   %rax, -0x20(%rbp)
0x103ffff4a <+58>:  cmpq   $0x0, -0x20(%rbp)
0x103ffff52 <+66>:  jne    0x103ffff82               ; <+114> at ViewTwo.swift:88
0x103ffff54 <+68>:  jmp    0x103ffff56               ; <+70> at ViewTwo.swift:88
0x103ffff56 <+70>:  leaq   0x6ddd(%rip), %rdi        ; "fatal error"
0x103ffff5d <+77>:  movl   $0xb, %eax
0x103ffff62 <+82>:  movl   %eax, %esi
0x103ffff64 <+84>:  movl   $0x2, %eax
0x103ffff69 <+89>:  leaq   0x6d90(%rip), %rcx        ; "unexpectedly found nil while unwrapping an Optional value"
0x103ffff70 <+96>:  movl   $0x39, %edx
0x103ffff75 <+101>: movl   %edx, %r8d
0x103ffff78 <+104>: movl   %eax, %edx
0x103ffff7a <+106>: movl   %eax, %r9d
0x103ffff7d <+109>: callq  0x10400438e               ; symbol stub for: function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()
0x103ffff82 <+114>: movq   -0x20(%rbp), %rax
0x103ffff86 <+118>: movq   %rax, %rdi
0x103ffff89 <+121>: movq   %rax, -0x40(%rbp)
0x103ffff8d <+125>: callq  0x1040043d0               ; symbol stub for: objc_retain
0x103ffff92 <+130>: movq   0x9237(%rip), %rdi        ; (void *)0x0000000106eae2e0: swift_isaMask
0x103ffff99 <+137>: movq   -0x40(%rbp), %rcx
0x103ffff9d <+141>: movq   (%rcx), %rdx
0x103ffffa0 <+144>: andq   (%rdi), %rdx
0x103ffffa3 <+147>: movq   0x118(%rdx), %rdx
0x103ffffaa <+154>: movq   %rax, -0x48(%rbp)
0x103ffffae <+158>: movq   %rdx, -0x50(%rbp)
0x103ffffb2 <+162>: callq  0x104002800               ; type metadata accessor for __ObjC.CNContact
0x103ffffb7 <+167>: movl   $0x1, %esi
0x103ffffbc <+172>: movl   %esi, %edi
0x103ffffbe <+174>: movq   %rax, %rsi
0x103ffffc1 <+177>: movq   %rax, -0x58(%rbp)
0x103ffffc5 <+181>: callq  0x10400436a               ; symbol stub for: Swift._allocateUninitializedArray <A> (Builtin.Word) -> (Swift.Array<A>, Builtin.RawPointer)
0x103ffffca <+186>: movq   -0x30(%rbp), %rdi
0x103ffffce <+190>: movq   %rax, -0x60(%rbp)
0x103ffffd2 <+194>: movq   %rdx, -0x68(%rbp)
0x103ffffd6 <+198>: callq  0x1040043d0               ; symbol stub for: objc_retain
0x103ffffdb <+203>: movq   -0x68(%rbp), %rcx
0x103ffffdf <+207>: movq   -0x30(%rbp), %rdx
0x103ffffe3 <+211>: movq   %rdx, (%rcx)
0x103ffffe6 <+214>: movq   -0x60(%rbp), %rdi
0x103ffffea <+218>: movq   -0x58(%rbp), %rsi
0x103ffffee <+222>: movq   %rax, -0x70(%rbp)
0x103fffff2 <+226>: callq  0x10400434c               ; symbol stub for: Swift.Array.init <A> (Swift.Array<A>.Type)(arrayLiteral : Swift.Array<A>...) -> Swift.Array<A>
0x103fffff7 <+231>: movq   %rax, %rdi
0x103fffffa <+234>: movq   -0x40(%rbp), %rsi
0x103fffffe <+238>: movq   -0x50(%rbp), %rax
0x104000002 <+242>: callq  *%rax
0x104000004 <+244>: movq   -0x40(%rbp), %rdi
0x104000008 <+248>: callq  0x1040043ca               ; symbol stub for: objc_release
0x10400000d <+253>: movq   -0x20(%rbp), %rdi
0x104000011 <+257>: callq  0x1040043ca               ; symbol stub for: objc_release
0x104000016 <+262>: movq   -0x38(%rbp), %rdi
0x10400001a <+266>: callq  0x1040043d0               ; symbol stub for: objc_retain
0x10400001f <+271>: movq   -0x38(%rbp), %rcx
0x104000023 <+275>: movq   0xcbae(%rip), %rsi        ; "navigationController"
0x10400002a <+282>: movq   %rcx, %rdi
0x10400002d <+285>: movq   %rax, -0x78(%rbp)
0x104000031 <+289>: callq  0x1040043be               ; symbol stub for: objc_msgSend
0x104000036 <+294>: movq   %rax, %rdi
0x104000039 <+297>: callq  0x1040043dc               ; symbol stub for: objc_retainAutoreleasedReturnValue
0x10400003e <+302>: cmpq   $0x0, %rax
0x104000044 <+308>: sete   %r8b
0x104000048 <+312>: xorb   $0x1, %r8b
0x10400004c <+316>: testb  $0x1, %r8b
0x104000050 <+320>: movq   %rax, -0x80(%rbp)
0x104000054 <+324>: jne    0x104000061               ; <+337> at ViewTwo.swift:89
0x104000056 <+326>: movq   -0x38(%rbp), %rdi
0x10400005a <+330>: callq  0x1040043ca               ; symbol stub for: objc_release
0x10400005f <+335>: jmp    0x1040000d2               ; <+450> at ViewTwo.swift:91
0x104000061 <+337>: movl   $0x1, %edx
0x104000066 <+342>: movq   0xcb73(%rip), %rsi        ; "popViewControllerAnimated:"
0x10400006d <+349>: movq   -0x80(%rbp), %rax
0x104000071 <+353>: movq   %rax, %rdi
0x104000074 <+356>: callq  0x1040043be               ; symbol stub for: objc_msgSend
0x104000079 <+361>: movq   %rax, %rdi
0x10400007c <+364>: callq  0x1040043dc               ; symbol stub for: objc_retainAutoreleasedReturnValue
0x104000081 <+369>: movq   -0x80(%rbp), %rdi
0x104000085 <+373>: movq   %rax, -0x88(%rbp)
0x10400008c <+380>: callq  0x1040043ca               ; symbol stub for: objc_release
0x104000091 <+385>: movq   -0x38(%rbp), %rdi
0x104000095 <+389>: callq  0x1040043ca               ; symbol stub for: objc_release
0x10400009a <+394>: movq   -0x88(%rbp), %rax
0x1040000a1 <+401>: movq   %rax, -0x90(%rbp)
0x1040000a8 <+408>: movq   -0x90(%rbp), %rax
0x1040000af <+415>: movq   %rax, %rdi
0x1040000b2 <+418>: callq  0x104004460               ; symbol stub for: swift_unknownRelease
0x1040000b7 <+423>: movq   -0x30(%rbp), %rdi
0x1040000bb <+427>: callq  0x1040043ca               ; symbol stub for: objc_release
0x1040000c0 <+432>: movq   -0x28(%rbp), %rdi
0x1040000c4 <+436>: callq  0x1040043ca               ; symbol stub for: objc_release
0x1040000c9 <+441>: addq   $0x90, %rsp
0x1040000d0 <+448>: popq   %rbp
0x1040000d1 <+449>: retq   
0x1040000d2 <+450>: xorl   %eax, %eax
0x1040000d4 <+452>: movl   %eax, %ecx
0x1040000d6 <+454>: movq   %rcx, -0x90(%rbp)
0x1040000dd <+461>: jmp    0x1040000a8               ; <+408> at ViewTwo.swift:89

我的代码

protocol ViewTwoDelegate {
    func didFetchContacts(contacts: [CNContact])
}

class ViewTwo: UIViewController, UITableViewDataSource, UITableViewDelegate, ViewTwoDelegate, ADBannerViewDelegate, CNContactPickerDelegate {


    @IBOutlet weak var Banner: ADBannerView!

    var contacts = [CNContact]()

    var delegate: ViewTwo!

    @IBOutlet weak var saveLookout: UITableView!

    func configureTableView() {
        saveLookout.delegate = self
        saveLookout.dataSource = self
        saveLookout.registerNib(UINib(nibName: "LookoutCell", bundle: nil), forCellReuseIdentifier: "idCellContactLookout")

    func viewDidLoad() {
        super.viewDidLoad()

            Banner.hidden = true
            Banner.delegate = self
            self.canDisplayBannerAds = true

        delegate = self

        configureTableView()

        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }


    func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        return true
    }

    func bannerViewDidLoadAd(banner: ADBannerView!) {
        Banner.hidden = false
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        NSLog("Error")
    }

    func bannerViewWillLoadAd(banner: ADBannerView!) {
    }



    //IBAction functions
    @IBAction func importLookout(sender: AnyObject) {
        let contactPickerViewController = CNContactPickerViewController()

        contactPickerViewController.predicateForEnablingContact = NSPredicate(format: "phoneNumbers != nil")

        contactPickerViewController.delegate = self

        presentViewController(contactPickerViewController, animated: true, completion: nil)

    }

    func contactPicker(picker: CNContactPickerViewController, didSelectContact contacts: CNContact){
        delegate.didFetchContacts([contacts])
        navigationController?.popViewControllerAnimated(true)

    }


    func refetchContact(contact contact: CNContact, atIndexPath indexPath: NSIndexPath) {
        AppDelegate.getAppDelegate().requestForAccess { (accessGranted) -> Void in
            if accessGranted {
                // let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey]
                let keys = [CNContactFormatter.descriptorForRequiredKeysForStyle(CNContactFormatterStyle.FullName), CNContactPhoneNumbersKey]

                do {
                    let contactRefetched = try AppDelegate.getAppDelegate().contactStore.unifiedContactWithIdentifier(contact.identifier, keysToFetch: keys)
                    self.contacts[indexPath.row] = contactRefetched

                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        self.saveLookout.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
                    })
                }
                catch {
                    print("Unable to refetch the contact: \(contact)", separator: "", terminator: "\n")
                }
            }
        }
    }


    // Tableview data source methods


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return contacts.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("idCellContactLookout") as! LookoutCell

        let currentContact = contacts[indexPath.row]

        cell.lblFirstname.text = CNContactFormatter.stringFromContact(currentContact, style: .FullName)

        if !currentContact.isKeyAvailable(CNContactPhoneNumbersKey) {
            refetchContact(contact: currentContact, atIndexPath: indexPath)
        }
        else {
            // Set the phone info.
            var homePhoneNumber: String!
            for mobileNumber in currentContact.phoneNumbers{
                if mobileNumber.label == CNLabelPhoneNumberMobile {
                    homePhoneNumber = mobileNumber.value as! String
                    break
                }
            }

            if homePhoneNumber != nil {
                cell.lblMobileNumber.text = CNLabelPhoneNumberMobile
            }
            else {
                cell.lblMobileNumber.text = "Not available mobile number"
            }

        }


        return cell
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 100.0
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let selectedContact = contacts[indexPath.row]

        let keys = [CNContactFormatter.descriptorForRequiredKeysForStyle(CNContactFormatterStyle.FullName), CNContactPhoneNumbersKey]

        if selectedContact.areKeysAvailable([CNContactViewController.descriptorForRequiredKeys()]) {
            let contactViewController = CNContactViewController(forContact: selectedContact)
            contactViewController.contactStore = AppDelegate.getAppDelegate().contactStore
            contactViewController.displayedPropertyKeys = keys
            navigationController?.pushViewController(contactViewController, animated: true)
        }
        else {
            AppDelegate.getAppDelegate().requestForAccess({ (accessGranted) -> Void in
                if accessGranted {
                    do {
                        let contactRefetched = try AppDelegate.getAppDelegate().contactStore.unifiedContactWithIdentifier(selectedContact.identifier, keysToFetch: [CNContactViewController.descriptorForRequiredKeys()])

                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            let contactViewController = CNContactViewController(forContact: contactRefetched)
                            contactViewController.contactStore = AppDelegate.getAppDelegate().contactStore
                            contactViewController.displayedPropertyKeys = keys
                            self.navigationController?.pushViewController(contactViewController, animated: true)
                        })
                    }
                    catch {
                        print("Unable to refetch the selected contact.", separator: "", terminator: "\n")
                    }
                }
            })
        }
    }

    //ViewTwoDelegate Function
    func didFetchContacts(contacts: [CNContact]) {
        for contacts in contacts {
            self.contacts.append(contacts)
        }

        saveLookout.reloadData()
    }

}
4

1 回答 1

1

你声明你delegate喜欢这样var delegate: ViewTwo!,但你从来没有给它赋值。所以当你打电话时,delegate.didFetchContacts([contacts])你的应用程序会崩溃。(看来您想在中的某处添加)。delegatenildelegate = selfviewDidLoad


您需要viewDidLoad正确编写方法。将其移出以确保它不是内部函数。

override func viewDidLoad() {}
于 2016-04-04T00:48:02.673 回答