Ios development


Published on

it is a course summery using the iPhone Application Development - Stanford Winter 2010 - CS193P materials.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ios development

  1. 1. IOS Development game
  2. 2. iPhone OS Overview
  3. 3. Cocoa TouchArchitecture
  4. 4. Objects
  5. 5. Object
  6. 6. Behavior
  7. 7. Message
  8. 8. State
  9. 9. Other Objects As State
  10. 10. Outlets
  11. 11. Target / Action
  12. 12. Demo
  13. 13. OOP VocabularyClassInstanceMethodInstance VariableEncapsulationPolymorphismInheritance
  14. 14. Inheritance
  15. 15. Objective-C
  16. 16. Objective-CStrict superset of C Mix C with ObjC Or even C++ with ObjC (usually referred to as ObjC++)Single inheritanceLoosely typed, if you’d like
  17. 17. Dynamic RuntimeObject creation All objects allocated out of the heap No stack based objectsMessage dispatch
  18. 18. Class and Instance MethodsInstances respond to instance methods (-)Classes respond to class methods (+)
  19. 19. Message syntax[receiver message][receiver message:argument][receiver message:arg1 andArg:arg2]
  20. 20. DemoShow Class creationInstance variableSetter and getterMessage ExamplesMethod definition examplesSelector Example and messaging it
  21. 21. Dot Syntax Objective-C 2.0 introduced dot syntax Convenient shorthand for invoking accessor methodsfloat height = [person height];float height = person.height;[person setHeight:newHeight];person.height = newHeight; Follows the dots...[[person child] setHeight:newHeight]; // exactly the same asperson.child.height = newHeight;
  22. 22. Objective-C Types
  23. 23. Dynamic and static typingDynamically-typed objectStatically-typed object
  24. 24. The null object pointer Test for nil explicitlyif (person == nil) return; Or implicitlyif (!person) return; Can use in assignments and as arguments if expected Sending a message to nil?
  25. 25. BOOL typedefWhen ObjC was developed, C had no booleantype (C99 introduced one)ObjC uses a typedef to define BOOL as a typeMacros included for initialization and comparison:YES and NO
  26. 26. Selectors identify methods by name A selector has type SELSEL action = [button action]; ![buttonsetAction:@selector(start:)]; Conceptually similar to function pointer Selectors include the name and all colons, for example:-(void)setName:(NSString *)name age:(int)age;would have a selector:SEL sel = @selector(setName:age:);
  27. 27. DemoUsing selector and respondsToSelector: method
  28. 28. Class IntrospectionYou can ask an object about its classTesting for general class membership(subclasses included):Testing for specific class membership(subclasses excluded):
  29. 29. Object Identity versus EqualityIdentity—testing equality of the pointer valuesEquality—testing object attributes
  30. 30. DemoClass IntrospectionInstance equality
  31. 31. Foundation Classes
  32. 32. NSObjectRoot classImplements many basics Memory management Introspection Object equality
  33. 33. Other TypesNSStringString Constants In C constant strings are “simple” In ObjC, constant strings are @“just as simple” Constant strings are NSString instances NSString *aString = @”Hello World!”;
  34. 34. Format StringsNSMutableStringCollectionsNSArrayNSMutableArrayNSDictionaryNSMutableDictionaryNSSetNSMutableSet
  35. 35. Enumeration Consistent way of enumerating over objects in collections Use with NSArray, NSDictionary, NSSet, etc.for (i = 0; i < count; i++) {for (Person *person in array) {
  36. 36. DemoEnumerationNSString
  37. 37. Object Lifecycle
  38. 38. Object Creation Two step process allocate memory to store the object initialize object state + allocClass method that knows how much memory is needed - initInstance method to set initial values, perform other setup Create = Allocate + Initialize
  39. 39. Implementing your own - init methodDemoMultiple init methods
  40. 40. Memory Management Allocation DestructionC malloc freeObjective-C alloc deallocCalls must be balanced Otherwise your program may leak or crash
  41. 41. Reference CountingEvery object has a retain count Defined on NSObject As long as retain count is > 0, object is alive and valid+alloc and -copy create objects with retain count == 1-retain increments retain count-release decrements retain countWhen retain count reaches 0, object is destroyed -dealloc method invoked automatically One-way street, once you’re in -dealloc there’s no turning back
  42. 42. DemoBalanced Call
  43. 43. Reference counting in actionPerson *person = [[Person alloc] init]; Retain count begins at 1 with +alloc[person retain]; Retain count increases to 2 with -retain[person release]; Retain count decreases to 1 with - release[person release]; Retain count decreases to 0, -dealloc automatically called
  44. 44. Messaging deallocated objectsDemo
  45. 45. Implementing a -dealloc methodDemo
  46. 46. Object Lifecycle RecapObjects begin with a retain count of 1Increase and decrease with -retain and -releaseWhen retain count reaches 0, object deallocatedautomaticallyYou never call dealloc explicitly in your code Exception is calling -[super dealloc] You only deal with alloc, copy, retain, release
  47. 47. AutoreleaseCalling -autorelease flags an object to be sentrelease at some point in the futureLet’s you fulfill your retain/release obligationswhile allowing an object some additional time toliveMakes it much more convenient to managememoryVery useful in methods which return a newlycreated object
  48. 48. DemoWhen to use auto release (return methods)
  49. 49. Method Names & AutoreleaseMethods whose names includes alloc, copy, ornew return a retained object that the callerneeds to releaseAll other methods return autoreleased objectsThis is a convention- follow it in methods youdefine!
  50. 50. How does -autorelease work?Object is added to current autorelease poolAutorelease pools track objects scheduled to bereleased When the pool itself is released, it sends -release to all its objectsUIKit automatically wraps a pool around everyevent dispatch
  51. 51. Hanging Onto an Autoreleased ObjectMany methods return autoreleased objectsIf you need to hold onto those objects you needto retain them
  52. 52. DemoReturn auto released object and retain it.
  53. 53. Objective-C Properties
  54. 54. PropertiesProvide access to object attributesShortcut to implementing getter/setter methodsAlso allow you to specify: read-only versus read-write access memory management policy
  55. 55. DemoPropertiesProperty Attributes