43
COCOA WORKSHOP PART 1 Andreas Monitzer 2009-02-17

COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

COCOA WORKSHOPPART 1

Andreas Monitzer2009-02-17

Page 2: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

WORKSHOP SCHEDULE

1. Introduction, Foundation

2. GUI Programming

3. Hands-On

4. Advanced

2009-02-17

2009-02-19

2009-02-24

2009-02-26

Page 3: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

STRUCTURE

• Introduction

• Objective C

• Memory Management

• Important Foundation Classes

• Creating Classes

• Big Example 1

Page 4: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

INTRODUCTION

Page 5: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

ABOUT MYSELF

• Dipl.-Ing. Andreas Monitzer

• 27 years old

• Working as programmer

• Cocoa Programming since Mac OS X Public Beta (2000)

- pptp-gui

- CocoaDevCentral

Page 6: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

MY WORK

• DigiTunnel

• PulpFiction

• MailDrop 2 (never left beta)

• Adium (GSoC 2006 & 2007)

Page 7: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

TARGET

• Write programs for Mac OS X in its native environment

• “How to Make a Zillion Dollars and Not Lose Your Soul” (Whil Shipley)

• Cocoa programming is fun!

Page 8: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

THINGS TO LEARN

• Tools

• Frameworks

• Language

Foundation AppKit

Objective C

Page 9: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

LET’S GET STARTED

Page 10: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

OBJECTIVE C

• Dynamic Object-Oriented Programming Language

• First published in 1986, pretty much simultaneously to C++

• Popularized by NeXT: NeXTSTEP/OpenStep

• Fully backwards-compatible with C

- Objective C-specific keywords use @-prefix

Page 11: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

COCOA

• API for user interface development

• Written in Objective C, but can be accessed from many languages

- Objective C, Perl, Python, Ruby, F-Script, formerly Java

Page 12: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

MAC OS X ARCHITECTURE

Page 13: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

COCOA STRUCTURE

• Divided into two main frameworks (libraries)

- Foundation

- AppKit

• Additional frameworks available

- WebKit, CoreFoundation, QTKit, …

Page 14: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

TODAY: FOUNDATION

• Memory Management, Collections, Event Handling, …

• Can be used for command line tools

• Objective C is pretty useless without Foundation

Page 15: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

OBJECTIVE C

Page 16: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

CLASSES

• Similar to Java

- No multiple inheritance

- Java interfaces / C++ pure abstract classes = ObjC protocols

- Object introspection (very frequently used)

• Split into interface and implementation

• type “id” = any object

Page 17: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

OBJC MESSAGING

• Parameters to methods are interleavedwith the name

- “:” prefix

[object abc:param1 def:param2];

Method name: “abc:def:”

Declaration:- (void)abc:(type)param1 def:(type)param2

Page 18: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

• Convention only!

• Constructor

[[classname alloc] init] or [[classname alloc] initWith...]

• Destructor

- (void)dealloc

CONSTRUCTORS/DESTRUCTORS

Page 19: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

MEMORY MANAGEMENT

Page 20: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

MEMORY MANAGEMENT

• Two modes:

- Reference Counting

- Garbage Collection

• Reference Counting keywords

-“alloc”, “new”, “copy”

-“retain”

-“release”

- Autorelease Pool

Page 21: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

GARBAGE COLLECTION

• Does the memory management for you

• Not transparent like in Java!

- Keeps a list of root objects and their dependencies

• strong references, weak references

- (void)finalize

__strong

Page 22: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

IMPORTANT FOUNDATION CLASSES

Page 23: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSOBJECT

• Base class

• Implements reference counting, object introspection, etc

Page 24: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSSTRING

- (NSUInteger)length

- (unichar)characterAtIndex:(NSUInteger)index

+ (NSString*)stringWithFormat:(NSString*)fmt, …

- (BOOL)isEqualToString:(NSString*)aString

• NSMutableString

- (void)appendString:(NSString*)aString

Page 25: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

COLLECTION CLASSES

• NSString

- Unicode string handling

• NSURL

- URL handling

• NSData

- Binary data

• NSArray

- An ordered list of objects

• NSDictionary

- A key/value mapping

• NSSet

- An unordered list of objects

Page 26: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

EXAMPLE

int main(int argc, const char *argv[]) { NSString *myString = @"abc"; [myString length]; [myString substringToIndex:2]; NSLog(@"string: %@", myString); return [myString length];}

Page 27: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSURL

+ (NSURL*)URLWithString:(NSString*)URLString

+ (NSURL*)fileURLWithPath:(NSString*)path

Page 28: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSDATA

+ (NSData*)dataWithBytes:(const void *)bytes length:(NSUInteger)length

- (NSUInteger)length

- (const void *)bytes

Page 29: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSARRAY

+ (NSArray*)arrayWithObjects:…

- (id)objectAtIndex:(NSUInteger)index

- (NSUInteger)count

• NSMutableArray

- (void)addObject:(id)obj

- (void)removeObjectAtIndex:(NSUInteger)index

Page 30: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSDICTIONARY

• “Hashtable”

+ (NSDictionary*)dictionaryWithObjectsAndKeys:…

- (id)objectForKey:(id)key

• NSMutableDictionary

- (void)setObject:(id)object forKey:(id)key

- (void)removeObjectForKey:(id)key

Page 31: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSSET

+ (NSSet*)setWithObjects:…

- (BOOL)containsObject:(id)obj

- (id)anyObject

• NSMutableSet

- (void)addObject:(id)obj

- (void)removeObject:(id)obj

Page 32: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSFILEHANDLE

• Reference to a file

- (NSData *)readDataToEndOfFile

- (void)writeData:(NSData *)data

+ (id)fileHandleWithStandardInput

+ (id)fileHandleWithStandardOutput

Page 33: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSRUNLOOP

• Main loop replacement

• Handles timers and sockets

- (void)run

Page 34: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

NSNOTIFICATION

• Observer pattern

[[NSNotificationCenter defaultCenter] addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender]

[[NSNotificationCenter defaultCenter] postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo]

Page 35: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

CREATING CLASSES

Page 36: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

INTERFACE

@interface <class> : <superclass> { <ivars>}

<methods>

@end

Page 37: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

IMPLEMENTATION

@implementation <class>

<methods>

@end

Page 38: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

PROPERTIES

• New in Objective C 2.0 and Leopard

• Automatic accessor generation

• interface: @property (<options>) <type> <name>;

• implementation: @synthesize <name>;

Page 39: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

PROTOCOL

@protocol NSCopying

- (id)copyWithZone:(NSZone *)zone;

@end

@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>

- (NSUInteger)count;- (id)objectAtIndex:(NSUInteger)index; @end

Page 40: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

CATEGORIES

• Add methods to an existing class

@interface NSData (XMPPStreamAdditions)

- (NSData *)md5Digest;

- (NSData *)sha1Digest;

- (NSString *)hexStringValue;

- (NSString *)base64Encoded;- (NSData *)base64Decoded;

@end

Page 41: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

BIG EXAMPLE 1

Page 42: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

TASK FOR THURSDAY

• Write a tool that reads in a file, encrypts it using ROT13 and writes the result back to a different file

- Hint: Read in the whole file at once and write everything at once

- Be careful about memory leaks!

Page 43: COCOA WORKSHOP PART 1 - jsug.atjsug.at/w/images/3/3f/JSUG-CocoaWorkshop-Andreas_Monitzer-Part… · WORKSHOP SCHEDULE 1.Introduction, Foundation 2. GUI Programming 3. Hands-On 4

REFERENCES

• Hillegas, Aaron: “Cocoa Programming for Mac OS X”ISBN: 978-0321503619 (Deutsch 978-3826659607)

• http://www.wilshipley.com/blog/WWDC_Student_Talk.pdf

• http://developer.apple.com/mac/