Introduction to iOS and Objective-C

1,560 views

Published on

A light introduction to iOS and Objective-C is given:
- The iOS architecture
- Objective-C history
- Syntax of an Objective-C program

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,560
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
90
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction to iOS and Objective-C

  1. 1. TECNOLOGIAS EMERGENTES EM JOGOS Engenharia em Desenvolvimento de Jogos Digitais - 2013/2014 February 18, 2014 Daniela da Cruz
  2. 2. AGENDA 1. The iOS architecture 2
  3. 3. AGENDA 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 2
  4. 4. THE IOS ARCHITECTURE
  5. 5. THE IOS ARCHITECTURE → iOS is the operating system that runs on iPad, iPhone, and iPod touch devices. → At the highest level, iOS acts as an intermediary between the underlying hardware and the apps we create. → Apps do not talk to the underlying hardware directly. Instead, they communicate with the hardware through a set of well-dened system interfaces. → These interfaces make it easy to write apps that work consistently on devices having dierent hardware capabilities. 4
  6. 6. THE IOS ARCHITECTURE The implementation of iOS technologies can be viewed as a set of layers. 5
  7. 7. THE IOS ARCHITECTURE → Lower layers contain fundamental services and technologies. → Higher-level layers build upon the lower layers and provide more sophisticated services and technologies. 6
  8. 8. THE IOS ARCHITECTURE CORE OS The Core OS layer contains the low-level features that most other technologies are built upon. 7
  9. 9. THE IOS ARCHITECTURE CORE OS The layer provides a variety of services including: → low level networking; → access to external accessories; → and the usual fundamental operating system services such as memory management, le system handling and threads. 8
  10. 10. THE IOS ARCHITECTURE CORE SERVICES The iOS Core Services layer provides much of the foundation on which the other layers are built. 9
  11. 11. THE IOS ARCHITECTURE CORE SERVICES Key among these services are the Core Foundation and Foundation frameworks, which dene the basic types that all apps use. This layer also contains individual technologies to support features such as location, iCloud, social media, and networking. 10
  12. 12. THE IOS ARCHITECTURE MEDIA The Media layer contains the graphics, audio, and video technologies you use to implement multimedia experiences in our apps. The technologies in this layer make easier to build apps that look and sound great. 11
  13. 13. THE IOS ARCHITECTURE COCOA The Cocoa Touch layer contains key frameworks for building iOS apps. These frameworks dene the appearance of your app. They also provide the basic app infrastructure and support for key technologies such as multitasking, touch-based input, push notications, and many high-level system services. 12
  14. 14. OBJECTIVE-C
  15. 15. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 14
  16. 16. HISTORICAL CONTEXT → 1972 → 1980 → 1983 → 1986 C programming language SmallTalk (rst OO language) C++ (C extension with OO support) Objective-C (C extension with concepts from SmallTalk) 15
  17. 17. WHY OBJECTIVE-C? → Was chosen as the main language in NeXT company founded by Steve Jobs to develop the operating System NeXTStep → In 1996, Apple buys NeXT and starting the development of Mac OS X (2001) using Objective-C Objective-C is the native programming language for Apple's iOS and OS X operating systems. 16
  18. 18. OBJECTIVE-C It's a compiled, general-purpose language capable of building everything from command line utilities to animated GUIs to domain-specic libraries. 17
  19. 19. OBJECTIVE-C Objective-C is a strict superset of C, which means that it's possible to seamlessly combine both languages in the same source le. In fact, Objective-C relies on C for most of its core language t's important to have at least a basic foundation in C before tackling the higher-level aspects of the constructs, so i language. 18
  20. 20. OBJECTIVE-C Like C++, Objective-C was designed to add object-oriented features to C, but the two languages accomplished this using fundamentally distinct philosophies. → Objective-C is more dynamic, deferring most of its decisions to run-time rather than compile-time. → Objective-C is also known for its verbose naming conventions. 19
  21. 21. OBJECTIVE-C C++ 1 john-drive(Corvette, Mary's House) Objective-C 1 [john driveCar:@Corvette toDestination:@Mary's House] Objective-C method calls read more like a human language than a computer one. 20
  22. 22. OBJECTIVE-C As with plain old C programs, the main() function serves as the root of an Objective-C application. An autoreleasepool implements simple garbage collection for reference-counted objects. It temporarily takes ownwership of reference-counted objects that nobody else wants to take ownership of and releases them at a later, appropriate point in time. 21
  23. 23. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 22
  24. 24. ID TYPE The id type is the generic type for all Objective-C objects. You can think of it as the object-oriented version of C's void pointer (void*). And, like a void pointer, it can store a reference to any type of object. id mysteryObject = @An NSString object; 23
  25. 25. CLASS TYPE Objective-C classes are represented as objects themselves, using a special data type called Class. This lets you, for example, dynamically check an object's type at runtime (introspection for later discussion). Class targetClass = [NSString class]; id mysteryObject = @An NSString object; if ([mysteryObject isKindOfClass:targetClass]) { NSLog(@Yup! That's an instance of the target class); } 24
  26. 26. SEL TYPE The SEL data type is used to store selectors, which are Objective-C's internal representation of a method name. For example, the following snippet stores a method called sayHello in the someMethod variable. This variable could be used to dynamically call a method at runtime. SEL someMethod = @selector(sayHello); 25
  27. 27. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 26
  28. 28. FOUNDATION FRAMEWORK DATA TYPES (PART I) NSObject is the root class of most Objective-C class hierarchies. Is the base class for pretty much every object in the iOS SDK. 27
  29. 29. FOUNDATION FRAMEWORK DATA TYPES BOOL used for representing Boolean (YES or NO are BOOL types allowed). BOOL myBOOLyes = YES; BOOL myBOOLno = NO; 28
  30. 30. FOUNDATION FRAMEWORK DATA TYPES NSString used for representing a string instead of C language's char* → → type. An NSString instance immutable. can not be modied! They are Tons of utility functions available (case conversion, URLs, substrings, type conversions, etc.). NSString* hi = @Hi; 29
  31. 31. FOUNDATION FRAMEWORK DATA TYPES NSMutableString Mutable version of NSString. (Somewhat rarely used.) NSMutableString * str1 = [NSMutableString stringWithString:@Hello World]; 30
  32. 32. FOUNDATION FRAMEWORK DATA TYPES NSNumber Object wrapper around primitive types like int, float, double, BOOL, etc. NSNumber *num = [NSNumber numberWithInt:36]; float f = [num floatValue]; // would return 36 as a float // (i.e. will convert types) 31
  33. 33. FOUNDATION FRAMEWORK DATA TYPES NSValue Generic object wrapper for other non-object data types. // CGPoint is a C struct CGPoint point = CGPointMake(25.0, 15.0); NSValue *pointObject = [NSValue valueWithCGPoint:point]; 32
  34. 34. FOUNDATION FRAMEWORK DATA TYPES NSDate Used to nd out the time right now or to store past or future times/dates. An NSDate object represents a specic point in time, independent of any particular calendrical system, time zone, or locale. Internally, it just records the number of seconds from an arbitrary reference point (January 1st, 2001 GMT). NSDate *now = [NSDate date]; 33
  35. 35. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 34
  36. 36. POINTERS All Objective-C objects are referenced as pointers. An NSString object must be stored as a pointer, not a normal variable: NSString *model = @Honda; 35
  37. 37. POINTERS There is a slight dierence between C and Objective-C. Whereas C uses NULL, Objective-C denes its own macro, nil, as its null object. NSString *anObject; anObject = NULL; anObject = nil; // An Objective-C object // This will work // But this is preferred int *aPointer; aPointer = nil; aPointer = NULL; // A plain old C pointer // Don't do this // Do this instead 36
  38. 38. NIL VERSUS NULL Objective-C's nill keyword is similar to NULL in other languages like C and Java. The dierence is that it is perfectly safe to call methods on If any method that returns an object is called on receive a nil nill, nil. we will back. 37
  39. 39. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 38
  40. 40. CLASSES When we dene a class, we dene a blueprint for a data type. This doesn't actually dene any data, but it does dene what the class name means, that is: → what an object of the class will consist of; and → what operations can be performed on such an object. 39
  41. 41. CLASSES INTERFACE An interface declares the public properties and methods of a class. @interface Photo : NSObject { // Protected instance variables (not recommended) } // Properties and Methods @end Protected variables can be dened inside of the curly braces, but most developers treat instance variables as implementation details and prefer to store them in the .m le instead of the interface. 40
  42. 42. CLASSES INTERFACE When declaring a method, ask yourself: Does the method apply to only one instance or all instances of the class? If it applies to all instances, it's usually better as a class method. Class methods are preceded by a minus (-) symbol. Instance methods are preceded by a plus (+) symbol. → 41
  43. 43. CLASSES INTERFACE Example: @interface Photo : NSObject - (NSString*) caption; - (NSString*) photographer; - (void) setCaption: (NSString*)input; - (void) setPhotographer: (NSString*)input; @end 42
  44. 44. CLASSES IMPLEMENTATION The @implementation directive is similar to @interface, except you don't need to include the super class. @implementation Photo { // Private instance variables } // Methods @end The instance variables are private and are only accessible inside the class implementation. 43
  45. 45. CLASSES IMPLEMENTATION Continuation of previous example: @implementation Photo { NSString* caption; NSString* photographer; } - (NSString*) caption { return caption; } - (NSString*) photographer { return photographer; } @end 44
  46. 46. CLASSES IMPLEMENTATION The getters themselves are very simple. They just return the instance variable. A setter's job is to swap the old value with the new one. Objective-C objects are dierent than an int or float. assign an object to a variable, it does not get the reference to the object. When you value; it gets a 45
  47. 47. CLASSES IMPLEMENTATION - (void) setCaption: (NSString*) input { [caption autorelease]; [input retain]; caption = input; } 46
  48. 48. CLASSES IMPLEMENTATION 1. [caption autorelease] We no longer need the old NSString object that caption refers so, so we call autorelease on it. 2. [input retain] We call retain on the input object because we are going to assign it to the instance variable and need it to stay around. 3. caption = input Finally we set the caption instance variable to be equal to input. 47
  49. 49. DO I ALWAYS NEED TO DO THIS? NO but only if you choose Automatic Reference Counting (ARC) to be used in your project!!! 48
  50. 50. CLASSES IMPLEMENTATION Classes usually have an init method to set initial values for its instance variables or other setup tasks. - (id) init { [self setCaption:@Default Caption]; [self setPhotographer:@Daniela da Cruz]; } return self; 49
  51. 51. CLASSES IMPLEMENTATION 1. Instance variables are set to nil automatically, so inside the brackets, we set default values for 2. photographer. Any init method caption we create has to return and self at the end. Our implementation calls to the superclass version of init and captures the result, so we need to actually return the new object to whoever requested it. 50
  52. 52. CLASSES INIT Straight from Apple's ocial documentation: There are several critical rules to follow when implementing an init method that serves as a class's sole initializer or, if there are multiple initializers, its designated initializer: 1. Always invoke the superclass (super) initializer rst. 2. Check the object returned by the superclass. If it is initialization cannot proceed; return nil nil, then to the receiver. 3. After setting instance variables to valid initial values, return self. 51
  53. 53. CLASSES INIT The basic format of a method to override the initializer looks as follows: -(id)init { self = [super init]; if (self != nil) { // Initialization code here } return self; } 52
  54. 54. CLASSES DESIGNATED INITIALIZER When working with a class that has more than one initialization method, it's important that one of the initializers drives the whole process. Put another way, only one of the initializer methods does the actual work of calling the super class initializer and initializing the instance variables of the object. A general rule of thumb (although not always the case) is that the designated initializer is the initializer with the most parameters. 53
  55. 55. CLASSES DESIGNATED INITIALIZER @implementation Photo // = Designated initializer = -(id)initWithCaptionAndPhotographer: (NSString *)inCaption photographer:(NSString *)inPhotographer { if (self = [super init]) { [self setCaption:inCaption]; [self setPhotographer:inPhotographer]; } return self; } 54
  56. 56. CLASSES DESIGNATED INITIALIZER -(id)initWithCaption: (NSString *)inCaption { return [self initWithCaptionAndPhotographer:inCaption photographer:nil]; } -(id)init { return [self initWithCaption:nil]; } @end 55
  57. 57. CLASSES Some notes: → All classes are derived from the base class called → NSObject → NSObject. it is the superclass of all Objective-C classes. It provides basic methods like memory allocation and initialization. → Getters usually don't include the get keyword in method name. → Init always return self at the end. 56
  58. 58. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 57
  59. 59. NAMESPACES Objective-C does not support namespaces, which is why the Cocoa functions require prexes like NS, CA, AV, etc to avoid naming collisions. The recommended convention is to use a three-letter prex for your application-specic classes (e.g., XYZCar). 58
  60. 60. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 59
  61. 61. UTILITIES NSLOG NSLog is used for printing a statement. It will be printed in device logs and debug console in release and debug modes respectively. Format Speciers: → %i, %d → %u → %f → %@ signed integer unsigned integer oat object 60

×