Thông tin tài liệu
MANNING
Brendan G. Lim
WITH Jerry Cheung
AND Jeremy McAnally
IN ACTION
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: orders@manning.com
©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
v
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
www.it-ebooks.info
www.it-ebooks.info
vii
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
www.it-ebooks.info
CONTENTS
viii
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
[...]... HotCocoa Getting started 9.2 189 190 Built -in mappings 191 Applications and menus 191 More advanced layouts 198 9.3 Building a speech application using HotCocoa Laying out the views 200 speak to you 201 9.4 10 Windows and controls ■ Summary ■ 193 200 Making your application 202 MacRuby testing 203 10.1 10.2 10.3 10.4 Testing MacRuby applications with MiniTest 203 Installing and configuring MiniTest 205... at www.manning.com/MacRubyinAction Software requirements An Intel-based Macintosh running OS X 10.6 or higher is required to develop MacRuby applications You also need to download MacRuby, but it’s freely available at http:/ /macruby. org The book offers full coverage of MacRuby and Xcode 4 Author Online Purchase of MacRuby in Action includes free access to a private web forum run by Manning Publications... can’t follow along? www.it-ebooks.info Introducing MacRuby 1.1.2 5 Setting up your environment Install the Xcode development environment first It’s best to install MacRuby after you have Xcode set up on your machine If you don’t install in this order, tools such as Interface Builder (which is now built into Xcode 4) won’t be able to recognize your MacRuby code INSTALLING XCODE Registered Apple Developers... you’ll be able to pick any sections in the rest of the book and learn about the areas that are most interesting or relevant to you www.it-ebooks.info www.it-ebooks.info Introducing MacRuby This chapter covers ■ Exploring and installing MacRuby ■ Important Cocoa concepts ■ Objective-C and Ruby fundamentals ■ MacRuby syntax and methods ■ Developing with the Xcode IDE MacRuby gives you the ability to write... Using KVO to implement observers 128 Adding and removing observers 128 Manually notifying observers of changes 129 Responding to observed objects 130 ■ ■ 6.3 Building out the Product Inventory application 131 Creating the user interface 131 Using KVC to retrieve product information 132 Adding features with KVC and KVO 137 ■ ■ 6.4 7 Summary 140 Implementing persistence with Core Data 141 7.1 Introducing... 154 157 Filtering and sorting with predicates and descriptors 157 Fetching objects from Core Data 158 7.5 Creating a Core Data version of the Todo List application Building the user interface 160 Creating the tasks controller 161 Connecting the interface to the controller 164 Running the application and inspecting the persistent store 166 ■ ■ 7.6 8 Summary 167 Core Animation basics 168 8.1 Introduction... you, the reader, creating amazing Cocoa applications using MacRuby by the end of the book Throughout the book, you’ll learn in the ins and outs of MacRuby while exploring the Cocoa framework, design patterns, system scripting, testing, and getting your application into the Mac App Store We know that sometimes the best way to learn is to get your feet wet That’s why you’ll be creating useful Mac applications... system 3 www.it-ebooks.info 4 1.1 CHAPTER 1 Introducing MacRuby Introducing MacRuby MacRuby is an Apple-sponsored development project Over the years, Apple has shown support for Ruby as a language, and, since 2002, Apple has included Ruby as part of the Mac OS X operating system Apple bundled a Ruby Scripting Bridge implementation called RubyCocoa with Mac OS X Leopard Prior to MacRuby, RubyCocoa was... and the Cocoa framework together In this section, you’ll learn how MacRuby is different from past attempts at combining Ruby and Objective-C and what makes it such a great language We’ll also jump right into getting MacRuby installed onto your system and introduce you to MacRuby s class structure Let’s set the stage for MacRuby 1.1.1 The MacRuby difference The goal of MacRuby is to provide an implementation... of the book MacRuby in Action is also a more approachable introduction to Cocoa development than traditional Objective-C books Throughout the book, we explore practical code examples that you’ll face when creating your own applications MacRuby in Action can act as a guide for using MacRuby and Cocoa from the ground up, or you can use it as a reference if you’re looking to dive deeper into MacRuby xvi . MANNING
Brendan G. Lim
WITH Jerry Cheung
AND Jeremy McAnally
IN ACTION
www.it-ebooks.info
MacRuby in Action
www.it-ebooks.info
www.it-ebooks.info
MacRuby. 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
Ngày đăng: 17/02/2014, 15:20
Xem thêm: Tài liệu MacRuby in Action pptx, Tài liệu MacRuby in Action pptx, 2 Cocoa: What you need to know, 3 Objective-C and Ruby: what you need to know, A.1 Before, there was AppleScript