data types ios: objective-c...

27
iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation Collections iOS: Objective-C Tommy MacWilliam Harvard University March 22, 2011

Upload: trantuyen

Post on 15-Mar-2018

222 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

iOS: Objective-C

Tommy MacWilliam

Harvard University

March 22, 2011

Page 2: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Announcements

I Lectures: http://cs76.net/LecturesI Sections: http://cs76.net/SectionsI n-Puzzle feedback this week

Page 3: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Today

I XCode + GDBI Data typesI Classes and objectsI Foundation collectionsI Designing a Class

Page 4: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

XCode

I download from the Mac App Store, $4.99I :(

Page 5: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

XCode

I view project information in navigator viewI project: filesI symbol: classes and methodsI search: search classes, methods, and implementationsI issue: compilation errors and warningI debug: debug informationI breakpoint: view/remove breakpointsI log: build/run list

Page 6: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Getting Help

I installing documentation: XCode → Preferences →Documentation → Check and install now

I view all documentation: Organizer (in the top right) →Documentation

I view documentation for class/method: option-click

Page 7: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Debugging

I GDB built into XCodeI print object information: po <object>

I create breakpoint by clicking on line numberI in the console: b function or b line

I list breakpoints with info b

I delete nth breakpoint with delete <n>

Page 8: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Debugging

I at breakpoint, go to next lineI next (execute any called function)I step (go into any called function)

I at breakpoint, continue to next breakpoint: continue

Page 9: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

The Language

I strict superset of CI any C program is also an Objective-C program

I major implementations: Clang (with LLVM) and GCCI not just for OS X! see: GNUstep

Page 10: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Primitives

I int: integers like 1, -2, 123

I float: floating point decimals like 1.0f, 3.14f,-5.f

I double: larger-capacity floatsI char: single character like ’a’,’Z’, ’8’

I id: object of any type

Page 11: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Strings

I not a primitive type (just like in Java)I implemented by NSStringI strings defined via @”the string”

Page 12: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Formatting

I NSLog is the new Log.i or console.logI special characters in NSLog string can be replaced with

valuesI int: %dI float: %fI char: %cI NSObject: %@

Page 13: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Interface

I declares class instance variables and methodsI .h file

@interface <class> : <parent> {<type> <ivar name>;

}- (<type>) <method name>;@end

Page 14: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Implementation

I defines class methodsI .m file

@implementation <class>- (<type>) <method name> {

// implementation goes here}@end

Page 15: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Properties

I getters and setters are necessary to access classmember variables

I getter

- (int) bar { return bar; }

I setter

- (void) setBar:(int)newBar {bar = newBar;

}

Page 16: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Properties

I starting with Objective-C 2.0, getters/setters can begenerated for you

I interface: @property (attributes) <propertyname>

I implementation: @synthesize <property name>

I foo.bar = 4;

Page 17: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Property Attributes

I nonatomic: unsynchronized, but faster accessI readonly: only getter generatedI readwrite: both getter/setted generated (default)

Page 18: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Method Arguments

I no arguments:

- (void) foo

I single argument:

- (void) foo:(int)bar

I multiple arguments:

- (void) foo:(int)bar baz:(int)qux

Page 19: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Calling Methods

I message-passing used to “call” methodsI message sent to object, and object responds to

message

I message receiver resolved at runtimeI no type-checking at compile timeI object may not respond to message!

I [object method:argument another:value];

Page 20: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Instantiating Classes

I alloc: reserve memory for object (like malloc in C)I init: set up the created object (like a constructor in

Java)I initialize attributes via custominitWith<Something>: methods

I both return pointers to objects

Page 21: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Memory Management

I no automatic garbage collection :(I reference counting

I alloc or retain: count++I release: count--

I dealloc called when the reference count is 0I release object’s fields

Page 22: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

More Property Attributes

I assign: nothing extra, just assignmentI retain: retain sent to new value, release to

previous valueI copy: new object is allocated via copy messaged (old

value released)

Page 23: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Using Other Classes

I interfaces and implementations need to know aboutother classes

I interface: @class <class>

I forward class declaration: tells compiler <class> exists

I implementation: #import “<class>.h”

I like #include: uses interface to tell compiler what<class> looks like

Page 24: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

NSString

I initWithString: create a new NSString objectfrom @“string”

I length: number of characters in the stringI subStringFromIndex, substringToIndex: get a

substring from a stringI isEqualToString: compare stringsI stringByReplacingOccurrencesOfString:

replace substring with another stringI told you Apple liked long method names

Page 25: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

NSMutableArray

I initWithObjects: create an NSMutableArrayfrom a comma-separated list of objects

I count: number of elements in the arrayI containsObject: whether or not an object is in the

arrayI indexOfObject: index of given object in arrayI objectAtIndex: object at given index in arrayI addObject, removeObject: add/remove an object

from the array

Page 26: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

NSMutableDictionary

I initWithObjects: create anNSMutableDictionary from a list of keys and values

I count: number of elements in the dictionaryI objectForKey: get value associated with keyI allKeys, allValues: get an NSArray of all

keys/valuesI setObject, removeObjectForKey: add/remove

an object from the dictionary

Page 27: Data Types iOS: Objective-C Objectsd3igjl1xhpabxf.cloudfront.net/2011/spring/sections/ios-0/section... · iOS: Objective-C Tommy MacWilliam XCode Data Types Classes and Objects Foundation

iOS:Objective-C

TommyMacWilliam

XCode

Data Types

Classes andObjects

FoundationCollections

Designing a Class

I example time!