1

This might be really generic and rather about the framework in general than a programming question. But, in the light of Swift, and the tedious and sometimes impossible tasks you have interacting with C APIs, that question is very relevant.

When building a new app for iOS, I discovered that you can really have a hard time working with address book framework. First, there is the uncomfortable pointer passing that you have to do for many CoreFoundation Methods. Secondly, the functions mostly return that ugly Unmanaged objects, where you have to figure out if they are retained or not (ARC is several years old now!). Accessing the properties through their identifiers is terribly cumbersome and far from typesafe. And lastly, since there is no C Function Pointer Support yet, you can´t even call ABAddressBookRegisterExternalChangeCallback(addressBook: ABAddressBook!, callback: ABExternalChangeCallback, context: UnsafeMutablePointer<Void>) because the ABExternalChangeCallback is so far only defined in Objective-C!

Then I found that there is some nice Objective-C Api in the Mac OS Version of AddressBookFramework. How unfair! Isn´t the iOS Frameworks younger? Why do you think Apple did this? And when will they change this in your opinion? Did I miss something, and is there an Objective-C Api for iOS, too?

Any suggestions for how to tackle above problems in the most convenient and beautiful way are welcome, too! For my part, I´m writing a complete wrapper to obscure all the nasty pointer- C-Function- and global constants uglyness. As soon as it´s ready I´ll commit it to StackExchange and maybe Github to let others benefit and discuss my solution.

EDIT: finally managed to upload my wrapper to GitHub. See https://github.com/SocialbitGmbH/SwiftAddressBook

TAKeanice
  • 511
  • 3
  • 14

1 Answers1

2

I agree with you about what iOS provides to access to the address book.

I've posted an answer explaining how I handled the problem, using some functional aspects of swift, and how I dealt with extracting unmanaged objects.

Briefly:

  • I defined a custom operator to allow me chaining function calls to transform some input data
  • I implemented 2 generic functions to extract unmanaged objects
  • posted some code to show how I access to the address book, loop through all contacts, and retrieve some data for each one
Community
  • 1
  • 1
Antonio
  • 71,651
  • 11
  • 148
  • 165
  • Thank you, I really like that approach. Especially the `>>>` operator, since it is a really beautifying one (in the spirit of Haskell bind operator?). I´ll leave that question open for another couple of days though. Maybe we´ll be able to gather more interesting ideas like that! – TAKeanice Oct 13 '14 at 22:22
  • I don't know Haskell, but I implemented it that way after reading something about Swift and functional programming, so I wouldn't be surprised it's been brought from other languages :) – Antonio Oct 14 '14 at 05:50
  • Did I mention any document? What I did is read the API specs, read about unmanaged stuffs, a few blogs, stackoverflow Q/A, etc. The actual implementation has been subject to several refinements - the first version was much longer and non optimized. So, no top/down documentation explaining how I got there. – Antonio Oct 14 '14 at 15:08
  • Sorry for the misunderstanding, I wrongly interpreted that you have read some particular document/website/blog that shows the functional programming capabilities of Swift. I would have loved to read that, because I like both, Swift and Functional programming – TAKeanice Oct 14 '14 at 15:32
  • Take a look at [objc.io no. 16](http://www.objc.io/issue-16/) for an intro - they also have recently published an ebook, you should find the link on their website – Antonio Oct 14 '14 at 16:24
  • 1
    Also this [blog post](http://robots.thoughtbot.com/efficient-json-in-swift-with-functional-concepts-and-generics) was very inspiring. Good intro to functional, and the iterative approach helps understanding how he moved from procedural to functional programming – Antonio Oct 14 '14 at 16:29