0

I recently started learning Swift and for my first app (Master-Detail Template), I am trying to pull data (add contacts to a list) from my Address Book. I am trying to connect my showAddressBook method to the + button in the Master. The build fails on the init line (error: "Initializers may only be declared within a type"), so I'm guessing my code is wrong.

func showAddressBook() {
    var addressBookController = ABPeoplePickerNavigationController.alloc()
    init(addressBookController = ABPeoplePickerNavigationController) {
        self.addressBookController = presentViewController(presentViewController: addressBookController, animated: true, completion: nil)
    }
}

I can upload the code for connecting Address Book data if necessary. Thanks!

1 Answers1

1

this isn't the way you init the ABPeoplePickerNavigationController simply write :

    func showAddressBook() {

    var addBook = ABPeoplePickerNavigationController()
    self.presentViewController(addBook, animated: true) { () -> Void in}
}

or you can use another initialiser and then use the presentViewController. note : using the same name for two variables (addressBookController) is bad programming habit :D

user3703910
  • 624
  • 1
  • 5
  • 25
  • So I replaced the 3 lines in the func to what you wrote and the app continued to crash. Thoughts? – user3345130 Dec 30 '14 at 01:30
  • Thanks, it's still throwing an error, perhaps there is something wrong with my other code. error msg below – user3345130 Dec 30 '14 at 01:52
  • 2014-12-29 20:46:01.663 Networker[3999:1463978] -[Networker.MasterViewController showAddressBook:]: unrecognized selector sent to instance 0x155864a0 2014-12-29 20:46:01.666 Networker[3999:1463978] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Networker.MasterViewController showAddressBook:]: unrecognized selector sent to instance 0x155864a0' – user3345130 Dec 30 '14 at 01:52
  • *** First throw call stack: (0x2464a49f 0x31e40c8b 0x2464f8b9 0x2464d7d7 0x2457f058 0x27b3d9fb 0x27ca149d 0x27b3d9fb 0x27b3d9a1 0x27b28613 0x27b3d40d 0x27b3d0e7 0x27b369b1 0x27b0d15d 0x27d80ab9 0x27b0bbb9 0x24610d57 0x24610167 0x2460e7cd 0x2455c3c1 0x2455c1d3 0x2b95a0a9 0x27b6bfa1 0xf8614 0xf8650 0x323c0aaf) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) – user3345130 Dec 30 '14 at 01:53
  • oooh if you connecting this function to a button you should write it like this @IBAction func showAddressBook(sender: AnyObject) and then connect it to the button (programmatically or by ctrl drag) – user3703910 Dec 30 '14 at 01:58
  • ahhh makes sense. The one issue is that the button doesn't seem to be visible via main.storyboard – user3345130 Dec 30 '14 at 02:06