Building a (Core) Foundation Rob Napier
A little background Mac OS X since 10.4 iphoneos since release Cisco Jabber, The Daily, RNCryptor Focus on low-level Today: Mac developer for...
KACE
NAPIER KUMAR egory COMPUTERS/PROGRAMMING/SOFTWARE DEVELOPMENT ing Pushing the Limits is your total guide to creating standout apps for the ipad, iphone, and ipod Touch. Veteran mobile developers Mugunth Kumar take you beyond the basics to cover advanced topics you won t find in most other ios development books. From of the Core Foundation API, to maximizing speed and performance with Grand Central Dispatch, to storyboarding your UI flow, they y step through all the knotty stuff you need to master to program fun, fully-functional, high-performance apps. : with the Objective-C Runtime g objects and modifying classes multitasking multiple platforms mal use of Security Services azing animations Using advanced text layout with Core Text Creating complex reusable table view layouts Using blocks to implement functional programming Creating high performance apps for any RESTful web service Implementing superfast caching for regions with spotty network connectivity Optimizing cash flow with In-App Purchases dequate mobile apps when you can create extraordinary ones? Get ios 5 Programming Pushing the Limits and expand your izons. wiley.com/go/ptl/ios5programming to download code files. KUMAR is an independent ios consultant working on a variety of ios apps, including some for Goodyear and Mastercard. Two ve reached the Top 5 apps in the Singapore App Store and have been featured in various magazines and newspapers. He writes n his blog, blog.mugunthkumar.com. Jubb Price 29.99//US$44.99/CAN$53.99 PUSHING THE LIMITS R has been developing for ios platforms since the first SDK release, working on products such as The Daily and Cisco Mobile. contributor to Stack Overflow and writes the Cocoaphony blog (cocoaphony.com). ios 5 PROGRAMMING LD AMAZING MOBILE APPS WITH THE ios SDK ROB NAPIER MUGUNTH KUMAR ios 5 PROGRAMMING PUSHING THE LIMITS Advanced Application Development for Apple iphone, ipad, and ipod Touch Chapter 19
Core Foundation? Data structures for all those powerful frameworks with Core in their name. The awesomeness of Objective-C. The speed of C.
Where are we?* UIKit Foundation Core Foundation Core OS / Darwin *Simplified, but close enough
Who cares? You want to use those powerful frameworks, right? Did I mention, it can do a lot of things Cocoa can t? And C is fast. Yes, very fast.
The real types Introspection Strings Collections Toll-free Bridging ARC The Path Memory management
The real types Introspection Strings Collections Toll-free Bridging ARC The Path Memory management
Real Types typedef void * CFTypeRef; typedef const struct CFString * CFStringRef; typedef struct CFString * CFMutableStringRef;
The real types Introspection Strings Collections Toll-free Bridging ARC The Path Memory management
Memory Management If you Create or Copy an object, you are an owner If you do not Create or Copy an object, you are not an owner. If you want to prevent the object from being destroyed, you must become an owner by calling CFRetain() If you are an owner of an object, you must call CFRelease() when you are done with it
CFRelease!= -release CFRelease makes us cry CFRelease(NULL) crashes There are a dozen wrappers on CFRelease() that fix that You will certainly make your own And call them SAFE_RELEASE() like everyone else
autorelease? There is no autorelease :(
Allocators How you want your memory? CFCreateBlah(Allocator, param, param) 99.9% of the time you want NULL Sometimes you want kcfallocatormalloc for memory that was created with malloc() Occasionally you want kcfallocatornull to do nothing Everything else is incredibly obscure
The real types Introspection Strings Collections Toll-free Bridging ARC The Path Memory management
Introspection CFGetTypeID() <=> CFArrayGetTypeID() CFCopyDescription() CFShow() CFShowStr()
The real types Introspection Strings Collections Toll-free Bridging ARC The Path Memory management
Strings Constants: CFSTR() CFStringRef foo = CFSTR( foo ); CFStringCreateWithCString()
Convert CFStringRef to cstring char * MYCFStringCopyUTF8String(CFStringRef astring) { if (astring == NULL) { return NULL; } CFIndex length = CFStringGetLength(aString); CFIndex maxsize = CFStringGetMaximumSizeForEncoding(length, kcfstringencodingutf8); char *buffer = (char *)malloc(maxsize); if (CFStringGetCString(aString, buffer, maxsize, kcfstringencodingutf8)) { return buffer; } } free(buffer); return NULL;
Convert non-const cstring to CStringRef Consider the ownership: const char *cstr = Hello ; char *bytes = malloc(strlen(cstr) + 1); strcpy(bytes, cstr); CFStringRef str = CFStringCreateWithCStringNoCopy(NULL, bytes, kcfstringencodingutf8, kcfallocatormalloc); CFShow(str); CFRelease(str);
The real types Introspection Strings Collections Toll-free Bridging ARC The Path Memory management
CFArray CFStringRef strings[3] = { CFSTR( One ), CFSTR( Two ), CFSTR( Three ) }; CFArrayRef array = CFArrayCreate(NULL, (void *)strings, 3, &kcftypearraycallbacks); CFShow(array); CFRelease(array); CFMutableArrayRef array = CFArrayCreateMutable(NULL, 0, &kcftypearraycallbacks);
CFDictionary #define kcount 3 CFStringRef keys[kcount] = { CFSTR( One ), CFSTR( Two ), CFSTR( Three ) }; CFStringRef values[kcount] = { CFSTR( Foo ), CFSTR( Bar ), CFSTR( Baz ) }; CFDictionaryRef dict = CFDictionaryCreate(NULL, (void *)keys, (void *)values, kcount, &kcftypedictionarykeycallbacks, &kcftypedictionaryvaluecallbacks);
Others CFTree CFBinaryHeap CFBitVector
Callbacks retain release copydescription equal hash
Non-retaining CFArray CFArrayCallBacks nrcallbacks = kcftypearraycallbacks; nrcallbacks.retain = NULL; nrcallbacks.release = NULL; CFMutableArrayRef nrarray = CFArrayCreateMutable(NULL, 0, &nrcallbacks); CFStringRef string = CFStringCreateWithCString(NULL, Stuff, kcfstringencodingutf8); CFArrayAppendValue(nrArray, string); CFRelease(nrArray); CFRelease(string);
The real types Introspection Strings Collections Toll-free Bridging ARC The Path Memory management
Toll-free Bridging NSArray *nsarray = [NSArray arraywithobject:@ Foo ]; printf( %ld\n, CFArrayGetCount(( bridge CFArrayRef)nsArray)); CFMutableArrayRef cfarray = CFArrayCreateMutable(NULL, 0, &kcftypearraycallbacks); CFArrayAppendValue(cfArray, CFSTR( Foo )); NSLog(@ %ld, [( bridge id)cfarray count]); CFRelease(cfArray);
How does that even work? ObjC: CF: typedef struct objc_object { Class isa; } *id; typedef struct CFRuntimeBase { uintptr_t _cfisa... }
The CF Magic CFIndex CFStringGetLength(CFStringRef str) { CF_OBJC_FUNCDISPATCH0( kcfstringtypeid, CFIndex, str, length ); CFAssertIsString(str); return CFStrLength(str) }
The real types Introspection Strings Collections Toll-free Bridging ARC The Path Memory management
Converting to ARC - (NSString *)firstname { CFStringRef cfstring = CFStringCreate...; return CFBridgingRelease(cfString); } CFStringRef cfstr = CFBridgingRetain([nsString copy]);... CFRelease(cfStr);
Bringing It Home Core Foundation is your friend 90% of Core Foundation is Foundation minus autorelease (and minus ARC) Core Foundation, as a rule, is more flexible and faster than the ObjC equivalent Go Forth and Core!
http://iosptl.com