Episode 4 - Objective-C
Posted 07/25/2006 - 12:21 by cocoacast
This Podcast will cover the Introduction to Objective-C as described in “Cocoa Programming for Mac OS X” by Aaron Hillegass. This is the first part of the podcast that covers the chapter 3. There a lot of things I needed to say about Objective-C and even while trying to make it as short as possible, I was still unable to fit within 30 minutes. I have already recorded most of the 2nd part of this chapter’s podcast, so in a few days expect to see the conclusion. Topics covered today include Foundation tool, Memory management, and some basic Foundation classes.
Here are the show notes for this Episode: Show Notes
View the Podcast
Show Notes:
Introduction to Objective-C
Episode 4
This episode introduces Objective-C programming language
Chapter 3 - Objective-C
Introduction to Objective-C
Episode 4
This episode introduces Objective-C programming language
Chapter 3 - Objective-C
Podcast Announcements
• Thanks for your comments
• Julien, Mike, Eric, and others
• Adapting WWDC Presentation format
• New Format is 960x600
• Updated section of Forums
• Suggestion for a New Podcast
Call for Help
• Help with Move to Wordpress
• Help with Content
• Suggestions for Topics
• Format of Podcast (video audio)
• Move to Libsyn
• Issue with iTunes download
• Move to Libsyn
• Call for site Administators
• email me: cocoacast@gmail.com
Podcast Conventions
The Following are Some Conventions for the Screencast
I’m trying to follow WWDC Presentations Format - Use Forums to Comment
Conventions
Method, Class, and Sub-Section Names
- (void)setRadius:(id)sender {
radius = [sender floatValue];
[self setNeedsDisplay:YES];
}
- (void)setRadius:(id)sender {
radius = [sender floatValue];
[self setNeedsDisplay:YES];
}
- (void)setRadius:(id)sender {
radius = [sender floatValue];
[self setNeedsDisplay:YES];
}
Code
Section
Code with
Highlights
Code with
Discussion
Console Applications
Use of Xcode for creation of Console Application
Xcode Foundation Tool
• Pure Objective-C Application
• Foundation Framework
• Foundation Tool/Project in Xcode
Demo
Creating Xcode Foundation Tool Projects
Objective-C Syntax
Getting Used to Objective-C
Creating and Using Instances
NSMutableArray *foo;
foo = [NSMutableArray alloc];
NSMutableArray *foo;
foo = [NSMutableArray alloc];
[foo init];
Allocation without Initialization
Initialize Object before Using
NSMutableArray *foo;
foo = [[NSMutableArray alloc] init];
Combined Initialization
Calling Methods
Method Calls in C and Java
x.insertObject(bar, i);
[x insertObject:bar atIndex:i];
[foo center];
[foo addObject:bar];
Method Calls in Objective-C
Non Descriptive C
if (x.intersectsArc(35.0, 19.0, 23.0, 90.0, 100.0))
vs.
if ([x intersectsArcWithRadius: 35.0centeredAtX: 19.0
Y: 23.0
fromAngle: 90.0toAngle:100.0])
In Objecive-C
Demo
First Foundation Tool application - Lottery
Memory Management
Reference Counting in Objective-C
Retain Count, Releasing, and Retaining
Reference Counting
TrainerOwerGroomretainCount=3Dog
Reference Counting
TrainerOwerGroomretainCount=2Dog
Reference Counting
TrainerOwerGroomretainCount=1Dog
TrainerOwerGroomretainCount=0DogRelease
TrainerOwerGroomretainCount=0DogRelease
Retain and Release
[foo retain];
for (i=0; i<10; i++) {
newNumber = [[NSNumber alloc] initWithInt:(i*3)];
[array addObject:newNumber];
[newNumber release];
}
retain message
release message
Sending Messages to nil
safe to send to nil
id foo;
foo = nil;
[foo count];
id foo = [[NSMutableArray aloc] init];
[foo release];
[foo count];
id foo = [[NSMutableArray aloc] init];
[foo release];
foo = nil;
[foo count];
not safe
now it is safe again
NSObject, NSArray, etc.
Hierarchy and the difference between Mutable and Regular Containers
Object hierarchy
• NSObject is the root of all objects in Cocoa
-(id)init+(id)new-(NSString *)description-(id)retain-(void)release-(void)dealloc-(BOOL)isEqual:(id)anObject
NSArray Class
• NSArray is the first of collection classes available in Foundation Framework
• NSArray is immutable
-(unsigned)count-(id)objectAtIndex:(unsigned)i-(id)lastObject-(BOOL)containsObject:(id)anObject-(unsigned)indexOfObject:(id)anObject
// If object is not found NSNotFound is returned
NSMutableArray Class
• NSMutableArray is editable (inherited) version of NSArray
• NSArray has mutableCopy method
• Adds the Ability to add and remove objects
-(void)addObject:(id)anObject-(void)addObjectsFromArray:(NSArray *)otherArray-(void)insertObject:(id)anObject atIndex:(unsigned)index-(void)removeAllObjects-(void)removeObject:(id)anObject
NSString Class
• NSString can hold Unicode characters
• Uses @”...“ format for distinguishing with C-strings
-(id)initWithFromat:(NSString *)format, ... // Like sprintf-(unsigned)length-(NSString *)stringByAppendingString:(NSString *)aString
// There are many other methods -- see documentation
Demo
Creating Custom Classes
More about Memory Management
Autoreleased Objects
Autorelease
- (NSString *)description {
NSString *result;
result=[[NSString alloc] initWithFormat:@”%@ = %d and %d”,
entryDate, firstNumber, secondNumber];
return result;
}
- (NSString *)description {
NSString *result;
result=[[NSString alloc] initWithFormat:@”%@ = %d and %d”,
entryDate, firstNumber, secondNumber];
[result release];
return result;
}
Autorelease
- (NSString *)description {
NSString *result;
result=[[NSString alloc] initWithFormat:@”%@ = %d and %d”,
entryDate, firstNumber, secondNumber];
[result autorelease];
return result;
}
Autorelease Rules
• Objects created by alloc, new, copy, or mutableCopy have retain
count of 1 and are not in autorelease pool
• If you get an object by any other method, assume that it has s retain
count of 1 and is in the autorelease pool. If you do not with to be
deallocated with the current autorelease pool, you must retain it
3 Ways to Retain Objects
- (void)setFoo:(NSCalendarDate *)x {
[x retain];
[foo release];
foo = x;
}
- (void)setFoo:(NSCalendarDate *)x {
if(foo !=x){
[foo release];
foo = [x retain];
}
“Retain, then Release”
“Check before change”
3 Ways to Retain Objects (cont.)
- (void)setFoo:(NSCalendarDate *)x {
[foo autorelease];
foo = [x retain];
}
“Autorelease old Value”
Demo
NSCalendarDate and custom Initializers
Demo
Debugger
Summary
• Basic Objective-C constructs
• Initializers
• Basic Containers and NSString
• Memory Management
• Foundation Tool
• Basic Debugging
Demo
Chapter 3 Challenge Solution





23 weeks 3 days ago
23 weeks 4 days ago
23 weeks 5 days ago
23 weeks 5 days ago
23 weeks 6 days ago
23 weeks 6 days ago
23 weeks 6 days ago
23 weeks 6 days ago
23 weeks 6 days ago
23 weeks 6 days ago