Building a (Core) Foundation. Rob Napier

Similar documents
CS193P - Lecture 13. iphone Application Development. Address Book - Putting People in Your App

Pushing the Limits. ios 6 Programming TOUCH ADVANCED APPLICATION DEVELOPMENT FOR APPLE IPHONE, IPAD, AND IPOD WILEY. Rob Napier and Mugunth Kumar

ios Application Development Course Details

Cross Platform Nearby Networking

Kevin van Vechten Core OS

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

Intro to Native ios Development. Dave Koziol Arbormoon Software, Inc.

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

Object-Oriented Programming in Objective-C

iphone App Basics iphone and ipod touch Development Fall 2009 Lecture 5

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case)

CSE 509: Computer Security

Announcement. Final Project Proposal Presentations and Updates

Mobile Application Development

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Fall Stanford CS193p Fall 2010

ios Developer s Guide Version 1.0

Objective-C Runtime. Cocoa s Jewel in the Crown. NSConference Nicolas

Data Management: Data Types & Collections

Address Book for iphone

CS193p Spring 2010 Thursday, April 29, 2010

Monday, 1 November The ios System

Cocoa. Last Week... Music 3SI: Introduction to Audio/Multimedia App. Programming. Today... Why Cocoa? Wikipedia - Cocoa

Memory Management: The Details

Playing & Recording Audio Audio Queue Services Recording - AQRecorder Class

Audio Unit Properties Reference

Finding Bugs Using Xcode Runtime Tools

Cocoa Programming. David Chisnall. March 18,

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Spring Stanford CS193p Spring 2011

CS 371L - Mobile Computing (ios) Dr. William C. Bulko. CS 371L Mobile Computing (ios) Introduction

Operating System Services. User Services. System Operation Services. User Operating System Interface - CLI. A View of Operating System Services

Four Components of a Computer System

CS193P - Lecture 16. iphone Application Development. Audio APIs Video Playback Displaying Web Content Settings

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Machine-Level Programming V: Unions and Memory layout

Advanced Memory Analysis with Instruments. Daniel Delwood Performance Tools Engineer

application components

Xcode 6 and ios 8 What s New for Software Developers

Getting Started with Apple ios Development Link-OS SDK Objective-C

CSE 124 Discussion (10/3) C/C++ Basics

Maxime Defauw. Learning Swift

COSC Software Engineering. Lecture 16: Managing Memory Managers

COCOA WORKSHOP PART 1. Andreas Monitzer

ITP 342 Mobile App Dev. Animation

The C++ Object Lifecycle. EECS 211 Winter 2019

MPATE-GE 2618: C Programming for Music Technology. Unit 5.1

Game Center Techniques, Part 1

Intro to Development for ios. Dave Koziol Arbormoon Software, Inc.

Advanced Computer Networks Smashing the Stack for Fun and Profit

Mastering Xcode 7 and Swift

Topics in Mobile Computing

CHAPTER 2: SYSTEM STRUCTURES. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

ITP 342 Mobile App Dev. Animation

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

Mobile Apps 2010 iphone and Android

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

A heap, a stack, a bottle and a rack. Johan Montelius HT2017

Objective-C and Cocoa User Guide and Reference Manual

Examples and solutions for iphone & ipad. J. Harrington B. Trebitowski C. Allen S. Appelcline MANNING

ITP 342 Advanced Mobile App Dev. Memory

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSC 1600 Memory Layout for Unix Processes"

The MVC Design Pattern

Objective-C Primer. iphone Programmer s Association. Lorenzo Swank September 10, 2008

C Bounds Non-Checking. Code Red. Reasons Not to Use C. Good Reasons to Use C. So, why would anyone use C today? Lecture 10: *&!

IOS 9 App Development Essentials: Learn To Develop IOS 9 Apps Using Xcode 7 And Swift 2 PDF

GNUstep: What is it? gnustep.org

CS193P: HelloPoly Walkthrough

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Review. iphone Application Programming Lecture 2: Objective-C, Cocoa. History. Objective-C. Device restrictions. Interaction paradigm changes

ITP 342 Mobile App Dev

CS193p Spring 2010 Wednesday, March 31, 2010

Building Isomorphic Javascript Apps

When we re first learning Cocoa (or Java, or Qt, or any other application framework),

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

ios vs Android By: Group 2

Memory Corruption 101 From Primitives to Exploit

CSC 581: Mobile App Development Spring 2019

CS193E Lecture #3 Categories and Protocols Cocoa Memory Management

About 1. Chapter 1: Getting started with iphone 2. Remarks 2. Versions 2. Examples 2. Installation or Setup 2. What is iphone. 3

Review. iphone Application Programming Lecture 2: Objective-C, Cocoa. History. Objective-C. Device restrictions. Interaction paradigm changes

Intermediate Programming, Spring 2017*

Publisher v3 Documentation

PusleIR Multitouch Screen Software SDK Specification. Revision 4.0

Processes. Johan Montelius KTH

Contents. iphone Training. Industry Trainers. Classroom Training Online Training ON-DEMAND Training. Read what you need

Jython. An introduction by Thinh Le

Bevezetés a mobil alkalmazáskészítés világába

High Performance Computing and Programming, Lecture 3

A process. the stack

Science. Computer Science

Learn to Code in Swift 4

CSE 303: Concepts and Tools for Software Development

Praktikum Entwicklung von Mediensystemen mit

IPHONE DEVELOPMENT. Getting Started with the iphone SDK

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

Having fun with apple s IOKit. Ilja van sprundel

Stanford CS193p. Developing Applications for ios Fall Stanford CS193p. Fall 2013

Organize Your iphone: Icons and Folders

Itunes Manually Transfer Ipod Music To Library Mac Software

Transcription:

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