data types ios: objective-c...
TRANSCRIPT
iOS:Objective-C
TommyMacWilliam
XCode
Data Types
Classes andObjects
FoundationCollections
iOS: Objective-C
Tommy MacWilliam
Harvard University
March 22, 2011
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
iOS:Objective-C
TommyMacWilliam
XCode
Data Types
Classes andObjects
FoundationCollections
Today
I XCode + GDBI Data typesI Classes and objectsI Foundation collectionsI Designing a Class
iOS:Objective-C
TommyMacWilliam
XCode
Data Types
Classes andObjects
FoundationCollections
XCode
I download from the Mac App Store, $4.99I :(
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
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
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>
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
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
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
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”
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: %@
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
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
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;
}
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;
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)
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
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];
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
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
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)
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
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
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
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
iOS:Objective-C
TommyMacWilliam
XCode
Data Types
Classes andObjects
FoundationCollections
Designing a Class
I example time!