about objects · objective-c basics morning session b objects, classes, and methods messages and...
Post on 13-Oct-2020
7 Views
Preview:
TRANSCRIPT
About Objects
Introduction to Objective-CSECTION 1
Developer–oriented training.
www.AboutObjects.com
Friday, October 9, 2009
About Objects
Reston, VA — Cupertino, CA
Training
ANSI C
Objective-C
iPhone SDK
Cocoa/Cocoa touch
Friday, October 9, 2009
360iDev Denver
Thanks to Conference organizers
Tom Ortega
John Wilker
And everyone else who helped: Great job!
About Objects delighted to take part
Look forward to continued sponsorship of future events.
How about Washington, D.C. for future conference?
3
Friday, October 9, 2009
About The Instructors
Jonathan Lehr
President, About Objects
Developer, author, speaker
Objective-C developer/trainer since 1991
4
Developer–oriented training.
Friday, October 9, 2009
About You
Are you familiar with other dev technologies?
Kinds of iPhone features you like to learn to develop?
Have you been trying to learn some things on your own?
5
Friday, October 9, 2009
Topics, Morning Day 1
Given the one-day format, we'll limit the scope.
Morning Session A
Using Xcode
Objective-C Basics
Morning Session B
Objects, Classes, and Methods
Messages and the Objective-C runtime system
Memory Management
Declared Properties
6
Friday, October 9, 2009
Topics, Afternoon, Day 1
Afternoon Session A
Overview of UIKit and Interface Builder
Applications, Windows, Views
ViewControllers and UIControls
Afternoon Session B
TableViews, Delegates, and DataSources
IB Outlets and Actions
Multiple Nib Files
Customizing UI Components
7
Friday, October 9, 2009
Overview
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
What is Objective-C?
ANSI C + Object-Oriented extensions
Combines the best of static and dynamic languages
GNU C compiler compiles both C and Objective-C code
Apple donated Objective-C to the GNU open source project
9
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
What are Cocoa and Cocoa touch?
Cocoa is Mac UI layer
Cocoa touch is Cocoa on the iPhone
Each contains two Objective-C frameworks
Foundation (collections, value types, utility classes, etc.)
UIKit (iPhone) or AppKit (Mac)
10
Friday, October 9, 2009
Developer Tools
Friday, October 9, 2009
XCode IDE
Build
Run (Simulator, device)
Debug (gdb)
Source code management (SCM)
Documentation
Friday, October 9, 2009
Xcode
All major platforms
Mac OS X
iPhone OS
Features
Syntax-aware code editor with completion, etc.
Automates builds
Context-sensitive documentation browser
Friday, October 9, 2009
Interface Builder
Visual GUI design tool
Doesn't generate code
Works with ‘Freeze-dried’ objects
Archived (serialized) in .nib files
Dynamically loaded
Objects deserialized at load time
Friday, October 9, 2009
Instruments
Profiling
Performance Monitoring
Garage-Band 'multi-track' interface
Profiling
Performance Monitoring
Garage-Band 'multi-track' interface
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
History of Objective-C
Smalltalk
Earliest OO language (circa 1972) — Allen Kay, Xerox PARC
Objective-C
Developed in early eights to address Smalltalk performance (Brad Cox)
Licensed in '85 by NeXT Computer
Steve Jobs, Avie Tevanian
Used in UI layer, dev tools, and apps.
16
Friday, October 9, 2009
Friday, October 9, 2009
User Interface, Circa 1996
Friday, October 9, 2009
Apple/NeXT Merger, '96
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Objective-C at Apple
Apple acquires NeXT in 1996 for modern OS to replace Mac OS 9
Apple then ported OpenStep to Motorola's PowerPC architecture
Added Quartz 2D rendering engine to replace Adobe Display PostScript
Ported Mac Toolbox APIs (Carbon)
New OS released 2001 as OS X 10.0
20
Friday, October 9, 2009
iPhone OS
Port of Mac OS X to mobile platform
First release (2.0b2) March, 2008
Friday, October 9, 2009
Objective-C Basics
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Message Syntax
Square brackets for message expressions
Java:
myString.toString()
Objective-C
[myString description]
23
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Method Arguments
Arguments are delimited by colons
Java:
person.setFirstName("Fred");
Objective-C
[person setFirstName:@"Fred"];
24
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Constants
Constant string object different from constant string
Java:
"Hello"
Objective-C
@"Hello" // String object"Hello" // C string
25
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Object Data Types
Objective-C objects are dynamically allocated structs
Variable types are therefore pointers to struct defined by class
Java:
Employee emp = new Employee();
Objective-C
Employee *emp = [[Employee alloc] init];
Obj-C also provides generic object type, id
id emp2 = [[Employee alloc] init];
26
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Constructors vs. Creation Methods
No constructors; creation methods are just methods
Ordinary return statements provide more flexibility
Calls to super can occur anywhere within a method
Inheritance is straight-forward
Memory allocation and initialization are separate steps
Java:
Employee emp = new Employee();
Objective-C
Employee *emp = [[Employee alloc] init];
27
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Prefix vs. Package Path
Obj-C language doesn't provide namespaces
Frameworks and libraries use prefixes by convention to avoid collisions
Java:
java.lang.String s = new String("hello");
Objective-C
NSString *s = [[NSString alloc] initWithString:"hi"];
28
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Classes
Two separate files
Class declared in .h file
Methods implemented in .m file
Compiler directives
@interface ... @end
@implementation ... @end
Curly braces
Instance variable section inside curly braces
Methods defined outside curly braces
29
Friday, October 9, 2009
Anatomy of a Class Declaration
@interface Person : NSObject { int _age; NSString * _firstName;}
...
@end
compiler directive
class we're declaring class it inherits from
name of instance variabledata type
Friday, October 9, 2009
Class Declaration: Methods
@interface Person : NSObject { ... // Instance variables go here}
// Method declarations start here...
- (int)age;
- (void) setAge: (int)anAge;
...
@end
return type
method name
arg type
arg name
Friday, October 9, 2009
© Copyright 2009 About Objects, Inc. All rights reserved worldwide.
#import <Foundation/Foundation.h>
// Following line declares the Person class as a subclass of NSObject
@interface Person : NSObject { // Instance Variables. Note: Underscore prefix is conventional but not required. int _age; NSString *_firstName;}
// Methods. Note: Getter methods are not prefixed with 'get'.
- (int)age; // Returns person's age.- (void)setAge:(int anAge); // Sets person's age to anAge.
- (NSString *)firstName; // Returns person's first name.- (void)setFirstName:(NSString *)aName; // Set person's first name to aName.
@end
Class Declaration
32
Friday, October 9, 2009
© Copyright 2009 About Objects, Inc. All rights reserved worldwide.
#import "Person.h"
@implementation Person
- (int)age{ return _age;}
- (void)setAge:(int age){ _age = age;}
- (NSString *)firstName{ return firstName;}
- (void)setFirstName:(NSString *)firstName{ _firstName = firstName; // Note: This code omits some memory management details...}
@end
Class Implementation
33
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Method Prototypes
Methods declared in .h, implemented in .m
Data types enclosed in parens
Instance methods prefixed with -
Class methods prefixed with +
// Method declarations- (id)init;+ (id)alloc;
34
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
No Method Overloading
Runtime system looks up methods by name rather than signature
Makes introspection simpler and more efficient
Java:
manager.addEmployee(emp);manager.addEmployee(emp, "Developer");
Objective-C
[manager addEmployee:emp];[manager addEmployee:emp withTitle:@"Developer"];
35
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Multi-Argument Methods
Method names can be composed of multiple sections
Each section ends with a colon that delimits the next arg
Java:
public void addEmployee(Employee emp, String title)
Objective-C
- (void)addEmployee:(Employee *)emp withTitle:(NSString *)title
Name of method is addEmployee:withTitle:
Args are emp and title
36
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Visibility Modifiers
Compiler directives
@private
@protected
@public
No visibility modifiers for methods
Methods made 'private' by omitting declarations from .h file.
To emphasize 'privacy', prefix method name with underscore.
37
Friday, October 9, 2009
© Copyright 2009 About Objects, Inc. All rights reserved worldwide.
@interface Person : NSObject{
// Visibility modifier. Applies to all ivars that follow.@private // Private instance variables. NSString *_firstName; NSNumber *_salary;
@protected // Protected instance variables. int _age; }
// Instance methods....
@end
Using Visibility Modifiers
38
Friday, October 9, 2009
Writing a Program
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
World's Shortest Obj-C Program
What makes this Objective-C?
It's in a file named main.m
String constant prefixed with @ (which makes it a string object in Obj-C)
More than just an Objective-C program; it's a Foundation tool!
Imports Foundation.h
Calls Foundation's NSLog function (a C function)
40
#import <Foundation/Foundation.h>
int main(){ NSLog(@"Hello, World!");}
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Working with Xcode
Setting Preferences
Creating a Project
Adding Groups
Adding Files
Building and Fixing Compilation Errors
Running
41
Friday, October 9, 2009
Preferences: IDE Layout
Select All-In-One. If drop down disabled, close any open projects !rst.
Friday, October 9, 2009
Preferences: Building
Set to Always.
Friday, October 9, 2009
Preferences: Debugging
Select Show Console & Debugger.
Friday, October 9, 2009
Preferences: Fonts & Colors
➊ Click Duplicate.
➋ Click on a row and choose Select All from the menu.
➌ Type ⌘T to bring up Font Panel and click to select font size.
Friday, October 9, 2009
Preferences: Indentation
➋ Select Always.
➊ Check Syntax-aware indenting.
Friday, October 9, 2009
Preferences: SCM
➋ Fill in repository info.
➊ To set up SCM (not needed for this course), click + to add repositories.
➌ If using SSH, go to SSH tab to supply identities.
Friday, October 9, 2009
Add New Group
➋ Select New Group...
➊ Right-click project name.
Friday, October 9, 2009
Add New File
➋ Select New File...
➊ Right-click Group name.
➌ Select a !le template.
Friday, October 9, 2009
Start Editing
Class declaration code generated from template.
.h and .m !les added automatically.
Friday, October 9, 2009
Build
Inline error messages.
Click Build and Run when ready to build.
Compiler output.
Friday, October 9, 2009
Run/Debug
Console.
Code editor. Debugger pane.
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Lab 0
1. Create a project named Hello World
2. Build and Run
3. Change the ‘Hello World!’ message to ‘Farewell World!’ and build and run again
53
Friday, October 9, 2009
© Copyright 2008 About Objects, Inc. All rights reserved worldwide.
Resources
Example Code:
http://www.aboutobjects.com/tutorials.html
Documentation:
Objective-C: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html
iPhone: http://developer.apple.com/iphone/library/navigation/index.html
Courses:
http://www.aboutobjects.com/training/courses.html
54
Friday, October 9, 2009
top related