forked from fullonrager/rys-objective-c-tutorial-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintroduction.html
More file actions
381 lines (308 loc) · 17.2 KB
/
introduction.html
File metadata and controls
381 lines (308 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Introduction - Ry’s Objective-C Tutorial - RyPress</title>
<meta charset='UTF-8' />
<meta name='description' content="Objective-C is the native programming language for Apple’s iOS and
OS X operating systems. It’s a compiled, general-purpose language
capable of building everything from command line utilities to animated GUIs to
domain-specific libraries. It also provides many tools for maintaining large,
scalable frameworks." />
<meta name='viewport'
content='width=device-width, initial-scale=1.0, maximum-scale=1.0' />
<link rel="icon" type="image/png" href="media/favicon.png" />
<link rel="stylesheet" href="media/style.css" />
<link rel="stylesheet" href="media/pygments.css" />
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-37121774-2', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<div id='page'>
<div id='content'>
<nav id='main-nav'>
<a href='/'><img src='media/logo-small.png'
width='120px'
alt='RyPress - Quality Software Tutorials'/></a>
<ul>
<li><a href='/'>Tutorials</a></li>
<li><a href='/secure/purchases.php'>Purchases</a></li>
<li><a href='/about.php'>About</a></li>
</ul>
</nav>
<div class='divider'></div>
<table class="icon-and-text"><tr>
<td><a href="index"><img src="media/icons/index.png" width="40px" height="40px"></a></td>
<td><p>You’re
reading
<a href="index.html"><em>Ry’s Objective-C Tutorial</em></a></p></td>
</tr></table><div class="divider"></div>
<h1 id="introduction">Introduction</h1>
<p>Objective-C is the native programming language for Apple’s iOS and
OS X operating systems. It’s a compiled, general-purpose language
capable of building everything from command line utilities to animated GUIs to
domain-specific libraries. It also provides many tools for maintaining large,
scalable frameworks.</p>
<figure>
<img style="max-width: 370px" src="media/introduction/obj-c-overview.png">
<figcaption>Types of programs written in Objective-C</figcaption>
</figure>
<p>Like C++, Objective-C was designed to add object-oriented features
to C, but the two languages accomplished this using fundamentally distinct
philosophies. Objective-C is decidedly more dynamic, deferring most of its
decisions to run-time rather than compile-time. This is reflected in many of
the design patterns underlying iOS and OS X development.</p>
<p>Objective-C is also known for its verbose naming conventions. The resulting
code is so descriptive that it’s virtually impossible to misunderstand or
misuse it. For example, the following snippet shows a C++ method call with its
Objective-C equivalent.</p>
<pre><code class="c1">// C++</code>
<code class="n">john</code><code class="o">-></code><code class="n">drive</code><code class="p">(</code><code class="s">"Corvette"</code><code class="p">,</code> <code class="s">"Mary's House"</code><code class="p">)</code>
<code class="c1">// Objective-C</code>
<code class="p">[</code><code class="n">john</code> <code class="nf">driveCar:</code><code class="s">@"Corvette"</code> <code class="nf">toDestination:</code><code class="s">@"Mary's House"</code><code class="p">]</code>
</pre>
<p>As you can see, Objective-C methods read more like a human language than a
computer one. Once you get used to this, it becomes very easy to orient
yourself in new projects and to work with third-party code. If you’re a
little bit disarmed by the square-brackets, don’t worry. You’ll be
quite comfortable with them by the end of the tutorial.</p>
<h2 id="frameworks">Frameworks</h2>
<p>As with most programming languages, Objective-C is a relatively simple
syntax backed by an extensive standard library. This tutorial focuses mostly on
the language itself, but it helps to have at least some idea of the tools that
you’ll be interacting with in the real world.</p>
<p>There are a few different “standard libraries” out there, but
Apple’s <a href="https://developer.apple.com/technologies/mac/cocoa.html">Cocoa</a> and <a href="https://developer.apple.com/technologies/ios/cocoa-touch.html">Cocoa
Touch</a> frameworks are by far the most popular. These define the API for
building OS X and iOS apps, respectively. The table below highlights some
of the key frameworks in Cocoa and Cocoa Touch. For a more detailed discussion,
please visit the <a href="https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/OSX_Technology_Overview/About/About.html">Mac
Technology Overview</a> or <a href="https://developer.apple.com/library/ios/documentation/miscellaneous/conceptual/iphoneostechoverview/Introduction/Introduction.html">iOS
Technology Overview</a>.</p>
<table class="multiline">
<thead>
<tr>
<th>Framework</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/ObjC_classic/_index.html">Foundation</a></td>
<td>Defines core object-oriented data types like strings, arrays,
dictionaries, etc. We’ll explore the essential aspects of this
framework in the <a href="data-types/index.html">Data Types</a> module.</td>
</tr>
<tr>
<td><a href="https://developer.apple.com/library/ios/documentation/uikit/reference/UIKit_Framework/_index.html">UIKit</a></td>
<td>Provides dozens of classes for creating and controlling the user
interface on iOS devices.</td>
</tr>
<tr>
<td><a href="https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/objc_classic/_index.html">AppKit</a></td>
<td>Same as UIKit, but for OS X devices.</td>
</tr>
<tr>
<td><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/CoreData_ObjC/_index.html">CoreData</a></td>
<td>Provides a convenient API for managing object relationships,
supporting undo/redo functionality, and interacting with persistent
storage.</td>
</tr>
<tr>
<td><a href="https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MediaPlayer_Framework/_index.html">MediaPlayer</a></td>
<td>Defines a high-level API for playing music, presenting videos, and
accessing the user’s iTunes library.</td>
</tr>
<tr>
<td><a href="https://developer.apple.com/library/mac/documentation/AVFoundation/Reference/AVFoundationFramework/_index.html">AVFoundation</a></td>
<td>Provides lower-level support for playing, recording, and
integrating audio/video into custom applications.</td>
</tr>
<tr>
<td><a href="https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/QuartzCoreRefCollection/_index.html">QuartzCore</a></td>
<td>Contains two sub-frameworks for manipulating images. The
<code>CoreAnimation</code> framework lets you animate UI components,
and <code>CoreImage</code> provides image and video
processing capabilities (e.g., filters).</td>
</tr>
<tr>
<td><a href="https://developer.apple.com/library/ios/documentation/coregraphics/reference/coregraphics_framework/_index.html">CoreGraphics</a></td>
<td>Provides low-level 2D drawing support. Handles path-based drawing,
transformations, image creation, etc.</td>
</tr>
</tbody>
</table>
<p>After you’re comfortable with Objective-C, these are some of the tools
that you’ll be leveraging to build iOS and OS X applications. But
again, this tutorial is not meant to be a comprehensive guide to app
development—it’s designed to <em>prepare you</em> to use the above
frameworks. With the exception of the Foundation Framework, we won’t
actually be working with any of these libraries.</p>
<p>If you’re interested in Mac App development, you should take a look at
<a href="http://rypress.com/tutorials/cocoa">Ry’s Cocoa Tutorial</a> after
you have a solid grasp on Objective-C. It shows you how to build OS X apps
using the same hands-on methodology as this tutorial.</p>
<h2 id="xcode">Xcode</h2>
<p><a href="https://developer.apple.com/xcode/">Xcode</a> is Apple’s
integrated development environment (IDE) for Mac, iPhone, and iPad app
development. It includes not only a source code editor, but also an interface
builder, a device simulator, a comprehensive testing and debugging suite, the
frameworks discussed in the previous section, and everything else you need to
make apps.</p>
<p>While there are other ways to compile Objective-C code, Xcode is definitely
the easiest. We strongly recommended that you install Xcode now so you can
follow along with the examples in this tutorial. It is freely available through
the <a href="https://itunes.apple.com/us/app/xcode/id497799835?ls=1&mt=12">Mac
App Store</a>.</p>
<h3 id="creating-an-application">Creating an Application</h3>
<p>Xcode provides several templates for various types of iOS and OS X
applications. All of them can be found by navigating to <em>File > New >
Project...</em> or using the <em>Cmd+Shift+N</em> shortcut. This will open a
dialog window asking you to select a template:</p>
<figure>
<img style="max-width: 543px" src="media/introduction/creating-cmd-line-app.png">
<figcaption>Creating a command line application</figcaption>
</figure>
<p>For this tutorial, we’ll be using the <em>Command Line Tool</em>
template found under <em>OS X > Application</em>, highlighted in the
above screenshot. This lets us strip away all of the elements specific to
iOS/OS X and focus on Objective-C as a language. Go ahead and create a new
<em>Command Line Tool</em> now. This opens another dialog asking you to
configure the project:</p>
<figure>
<img style="max-width: 522px" src="media/introduction/configuring-cmd-line-app.png">
<figcaption>Configuring a command line application</figcaption>
</figure>
<p>You can use whatever you like for the <em>Product Name</em> and
<em>Organization Name</em> fields. For the <em>Company Identifier</em> use
<code>edu.self</code>, which is the canonical private use identifier. For
production applications, you’ll need to get a real company ID from Apple
by <a href="https://developer.apple.com/programs/register/">registering as a
developer</a>.</p>
<p>This tutorial utilizes several classes defined in the Foundation Framework,
so be sure to select <em>Foundation</em> for the <em>Type</em> field. Finally,
the <em>Use Automatic Reference Counting</em> checkbox should always be
selected for new projects.</p>
<p>Clicking <em>Next</em> prompts you for a file path to store the project
(save it anywhere you like), and you should now have a brand new Xcode project
to play with. In the left-hand column of the Xcode IDE, you’ll find a
file called <code>main.m</code> (along with some other files and folders). At
the moment, this file contains the entirety of your application. Note that the
<code>.m</code> extension is used for Objective-C source files.</p>
<figure>
<img style="max-width: 258px" src="media/introduction/xcode-project-navigator.png">
<figcaption><code>main.m</code> in the Project Navigator</figcaption>
</figure>
<p>To compile the project, click the <em>Run</em> button in the upper-left
corner of the IDE or use the <em>Cmd+R</em> shortcut. This should display
<code>Hello, World!</code> in the <em>Output Panel</em> located at the bottom
of the IDE:</p>
<figure>
<img style="max-width: 485px" src="media/introduction/xcode-output-panel.png" alt="Xcode's Output Panel displaying 'Hello, World!'">
<figcaption>Xcode’s Output Panel</figcaption>
</figure>
<h3 id="the-main-function">The main() Function</h3>
<p>As with plain old C programs, the <code>main()</code> function serves as the
root of an Objective-C application. Most of the built-in Xcode templates create
a file called <code>main.m</code> that defines a default <code>main()</code>
function. Selecting our <code>main.m</code> in Xcode’s <em>Project
Navigator</em> panel should open the editor window and display the
following.</p>
<pre><code class="cp">#import</code> <code class="l"><Foundation/Foundation.h></code><code class="cp"></code>
<code class="kt">int</code> <code class="nf">main</code><code class="p">(</code><code class="kt">int</code> <code class="n">argc</code><code class="p">,</code> <code class="kt">const</code> <code class="kt">char</code> <code class="o">*</code> <code class="n">argv</code><code class="p">[])</code> <code class="p">{</code>
<code class="k">@autoreleasepool</code> <code class="p">{</code>
<code class="c1">// insert code here...</code>
<code class="nb">NSLog</code><code class="p">(</code><code class="s">@"Hello, World!"</code><code class="p">);</code>
<code class="p">}</code>
<code class="k">return</code> <code class="mi">0</code><code class="p">;</code>
<code class="p">}</code>
</pre>
<p>Inside of the <code>@autoreleasepool</code> block is where you can write
code and experiment with the snippets from this tutorial. The above
<code>main()</code> function simply calls the global <code>NSLog()</code>
function defined by the Foundation Framework. This is Objective-C’s
general-purpose tool for outputting messages to the console. Also note that
Objective-C strings are always prefixed with an at (<code>@</code>) symbol.</p>
<p>Throughout this tutorial, we’ll directly edit the above
<code>main.m</code> to see how new language features work, but in the real
world, you’ll probably never have to alter the <code>main()</code>
function provided by the template. For most applications, the only thing
<code>main()</code> needs to do is pass control of the program to the
“application delegate.” For example, the default
<code>main()</code> function for a Mac App project looks like the
following.</p>
<pre><code class="cp">#import</code> <code class="l"><Cocoa/Cocoa.h></code><code class="cp"></code>
<code class="kt">int</code> <code class="nf">main</code><code class="p">(</code><code class="kt">int</code> <code class="n">argc</code><code class="p">,</code> <code class="kt">const</code> <code class="kt">char</code> <code class="o">*</code> <code class="n">argv</code><code class="p">[])</code> <code class="p">{</code>
<code class="k">return</code> <code class="nb">NSApplicationMain</code><code class="p">(</code><code class="n">argc</code><code class="p">,</code> <code class="n">argv</code><code class="p">);</code>
<code class="p">}</code>
</pre>
<p>But, since we’ll be sticking with command line tools, this is somewhat
outside the scope of this tutorial. Application delegates are, however, an
integral part of OS X and iOS development. The first several chapters of
<a href="http://rypress.com/tutorials/cocoa">Ry’s Cocoa Tutorial</a>
explain them in full detail.</p>
<h2 id="get-ready">Get Ready!</h2>
<p>The next two modules explore the basic C syntax. After that, we’ll be
ready to dive into classes, methods, protocols, and other object-oriented
constructs. This tutorial is chock-full of hands-on examples, and we encourage
you to paste them into the template project we just created, mess with some
parameters, and see what happens.</p>
<div class="divider" style="margin-top: 2em; margin-bottom: 1.3em;"></div><table class="icon-and-text advert"><tr>
<td><a href="http://rypress.com/tutorials/cocoa"><img src="media/advert-image.png" width="120px" height="165px" class="cover"></a></td>
<td><p id="advert-text">Be sure to check out <a href="http://rypress.com/tutorials/cocoa">Ry’s Cocoa Tutorial</a>. This
brand new guide is a complete walkthrough of Mac App development, and it
leverages all of the Objective-C skills that we just discussed. <a href="http://rypress.com/tutorials/cocoa">Learn more ›</a></p></td>
</tr></table>
<div id='mailing-list'>
<div class='divider' style='margin: 1.8em -22px;'></div>
<h3>Mailing List</h3>
<p>Sign up for my low-volume mailing list to find out when new content is
released. Next up is a comprehensive <a
href='https://developer.apple.com/swift/'>Swift</a> tutorial planned for late
January.</p>
<form id='mailing-list-form'
action='/secure/mailing-list/request-add' method='get'>
<label>
<div class='label'>Email Address:</div>
<div class='input'>
<input name='email'
style='width: 200px;'
type='email' />
</div>
</label>
<input id='mailing-list-button'
class='button'
style='margin-top: .5em;'
type='submit' value='Subscribe'/>
</form>
<div style='clear: both'></div>
<p class='fine-print'>You’ll only receive emails when new tutorials are
released, and your contact information will never be shared with third
parties. <a href='/secure/mailing-list/unsubscribe'>Click here</a> to
unsubscribe.</p>
</div>
<div class='divider'></div>
<footer id='footer' class='fine-print'>
<ul>
<li><a href='/licensing.php'>© 2012-2014</a></li>
<li><a href='/'>RyPress.com</a></li>
<li>
<a href='/licensing.php'>All Rights Reserved</a>
</li>
<li>
<a href='/tos.php'>Terms of Service</a>
</li>
<li>
<a href='/privacy.php'>Privacy Policy</a>
</li>
</ul>
</footer>
</div> <!-- .content -->
</div> <!-- .page -->
</body>
</html>