M A N N I N G IN ACTION Brendan G. Lim Jerry Cheung WITH Jeremy McAnally AND www.it-ebooks.info MacRuby in Action www.it-ebooks.info www.it-ebooks.info MacRuby in Action BRENDAN G. LIM WITH JERRY CHEUNG AND JEREMY MCANALLY MANNING SHELTER ISLAND www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: [email protected] ©2012 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without elemental chlorine. Development editor: Sara Onstine Manning Publications Co. Technical proofreader: Nick Howard 20 Baldwin Road Copyeditors: Lianna Wlasiuk, Tiffany Taylor PO Box 261 Proofreader: Melody Dolab Shelter Island, NY 11964 Typesetter: Marija Tudor Cover designer: Marija Tudor ISBN: 9781935182498 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 12 www.it-ebooks.info brief contents PART 1 STARTING WITH MACRUBY ...........................................1 1 ■ Introducing MacRuby 3 2 ■ Using Macirb and the Apple development tools 37 3 ■ Going beyond the basics with Xcode Interface Builder 64 PART 2 TAKE IT FOR A SPIN ....................................................85 4 ■ Using the delegate pattern 87 5 ■ Notifications and implementing the observer pattern 104 6 ■ Using key-value coding and key-value observing 120 7 ■ Implementing persistence with Core Data 141 8 ■ Core Animation basics 168 PART 3 MACRUBY EXTRAS ....................................................187 9 ■ HotCocoa 189 10 ■ MacRuby testing 203 11 ■ MacRuby and the Mac App Store 216 v www.it-ebooks.info www.it-ebooks.info contents preface xiii acknowledgments xiv about this book xvi about the authors xix about the cover illustration xx PART 1 STARTING WITH MACRUBY ...............................1 1 Introducing MacRuby 3 1.1 Introducing MacRuby 4 The MacRuby difference 4 ■ Setting up your environment 5 Hello World, part 1 6 1.2 Cocoa: What you need to know 7 Important classes and concepts 8 ■ How Cocoa implements common design patterns 10 1.3 Objective-C and Ruby: what you need to know 11 A shared heritage 12 ■ Objective-C 101 13 Ruby 101 17 vii www.it-ebooks.info viii CONTENTS 1.4 Diving into MacRuby 21 Class structure 21 ■ Creating MacRuby classes 23 Syntax and method signatures 24 ■ Using Ruby and Objective-C methods 26 ■ Creating user interfaces 27 1.5 Hello World, part 2 28 Creating an Xcode project 29 ■ Creating the interface 30 Creating the controller 32 ■ Connecting the interface and controller 34 1.6 Summary 35 2 Using Macirb and the Apple development tools 37 2.1 Using external libraries with MacRuby 38 Loading frameworks 38 ■ Loading Objective-C libraries as bundles 39 ■ Loading Ruby gems 41 2.2 Exploring Macirb 42 Comparing the Ruby and MacRuby consoles 42 Working in the MacRuby console 43 ■ Macirb tips and tricks 43 2.3 Building a Pomodoro application in Xcode 47 Creating a new MacRuby project 47 ■ Constructing the interface 50 ■ Creating the controller 53 ■ Connecting the controller and the interface 56 ■ Running the application 58 Releasing the application 58 2.4 Summary 63 3 Going beyond the basics with Xcode Interface Builder 64 3.1 About Interface Builder 65 History of Interface Builder 65 ■ Getting around Interface Builder 65 3.2 Creating connections 69 Understanding outlets 69 ■ Understanding actions 71 3.3 Creating the Todo List application 73 Constructing the user interface 73 ■ Creating the model 79 ■ Creating the controller 79 ■ Connecting outlets and actions 82 ■ Running and packaging the application 83 3.4 Summary 84 www.it-ebooks.info CONTENTS ix PART 2 TAKE IT FOR A SPIN .........................................85 4 Using the delegate pattern 87 4.1 What are delegates? 88 How do delegate methods work? 88 ■ Implementing the delegate pattern 89 4.2 Delegation as an extension technique 92 Delegation the Cocoa way 93 ■ Delegation using Forwardable 93 4.3 Using delegation in a custom MacRuby web browser 94 Creating the browser interface 94 ■ Setting up the controller 95 ■ Implementing delegate methods in the controller 98 ■ Connecting outlets and actions 100 Taking MacRuby Browser for a spin 102 4.4 Summary 103 5 Notifications and implementing the observer pattern 104 5.1 Notifying multiple objects 105 When to use notifications 105 ■ Managing notifications 106 5.2 Setting up notifications 107 Creating notifications 108 ■ Posting notifications to the notification center 108 5.3 Queuing notifications 109 Using posting styles 109 ■ Coalescing notifications 110 Queuing multiple notifications 112 ■ Removing notifications 113 5.4 Responding to notifications 114 Adding notification observers 114 ■ Removing notification observers 116 5.5 Building an iTunes-notification observer 116 Creating the script 116 ■ Running the script 118 5.6 Summary 118 6 Using key-value coding and key-value observing 120 6.1 Simplifying code with key-value coding 121 Accessing object properties with KVC 121 ■ Handling unknown keys 123 ■ Understanding key paths and collection operators 125 www.it-ebooks.info