Introduction to iOS and Objective-C

50 %
50 %
Information about Introduction to iOS and Objective-C

Published on February 27, 2014

Author: danieladacruz84



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

TECNOLOGIAS EMERGENTES EM JOGOS Engenharia em Desenvolvimento de Jogos Digitais - 2013/2014 February 18, 2014 Daniela da Cruz

AGENDA 1. The iOS architecture 2

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


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

THE IOS ARCHITECTURE The implementation of iOS technologies can be viewed as a set of layers. 5

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

THE IOS ARCHITECTURE  CORE OS The Core OS layer contains the low-level features that most other technologies are built upon. 7

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

THE IOS ARCHITECTURE  CORE SERVICES The iOS Core Services layer provides much of the foundation on which the other layers are built. 9

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

FOUNDATION FRAMEWORK  DATA TYPES BOOL  used for representing Boolean (YES or NO are BOOL types allowed). BOOL myBOOLyes = YES; BOOL myBOOLno = NO; 28

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

FOUNDATION FRAMEWORK  DATA TYPES NSMutableString  Mutable version of NSString. (Somewhat rarely used.) NSMutableString * str1 = [NSMutableString stringWithString:@"Hello World"]; 30

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

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

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

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

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

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

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

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

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

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

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

CLASSES  INTERFACE Example: @interface Photo : NSObject - (NSString*) caption; - (NSString*) photographer; - (void) setCaption: (NSString*)input; - (void) setPhotographer: (NSString*)input; @end 42

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

CLASSES  IMPLEMENTATION Continuation of previous example: @implementation Photo { NSString* caption; NSString* photographer; } - (NSString*) caption { return caption; } - (NSString*) photographer { return photographer; } @end 44

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

CLASSES  IMPLEMENTATION - (void) setCaption: (NSString*) input { [caption autorelease]; [input retain]; caption = input; } 46

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

DO I ALWAYS NEED TO DO THIS? NO but only if you choose Automatic Reference Counting (ARC) to be used in your project!!! 48

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

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

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

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

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

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

CLASSES  DESIGNATED INITIALIZER -(id)initWithCaption: (NSString *)inCaption { return [self initWithCaptionAndPhotographer:inCaption photographer:nil]; } -(id)init { return [self initWithCaption:nil]; } @end 55

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

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

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

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

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

Add a comment

Related presentations

Related pages

Introduction - Ry’s Objective-C Tutorial - RyPress

Introduction. Objective-C is the native programming language for Apple’s iOS and OS X operating systems. It’s a compiled, general-purpose language ...
Read more

About Objective-C - Apple Developer

Objective-C is the primary programming language you use when writing software for OS X and iOS. ... Objective-C apps use reference counting to ...
Read more

Introduction - Apple Developer Tools

Important: This document does not describe the Objective-C language itself. To learn about the language, see Programming with Objective-C.
Read more

An introduction to Objective-C - Treehouse Blog

If you are serious about becoming an iOS developer then it is imperative that you learn Objective-C ... 21 Responses to “An introduction to Objective-C ...
Read more

Objective-C - Wikipedia, the free encyclopedia

Objective-C on iOS 7 running on ARM64 uses 19 bits out of a 64-bit word to store ... The introduction of the two letter prefix also became a simplistic ...
Read more

Short Introduction to iOS for Java Developers: Objective-C ...

The Objective-C is extension of the standard ANSI C language, ... Short Introduction to iOS for Java Developers: SDK. More content about iOS. iOS.
Read more

Programming with Objective C for iOS | Lecturio

Programming with Objective C for iOS ... Introduction iOS Programming 30:02 : Objective C 38:41 : Objective C Properties 35:36 ...
Read more

Introduction to iOS Development: An Overview of Objective-C

How do you develop apps for iOS? It’s a common question, and one we hope to answer with this series of posts. Learning how to develop software can be one ...
Read more

Objective-C and IOS Programming: A Simplified Approach to ...

Arshia Khan - Objective-C and IOS Programming: A Simplified Approach to Developing Apps for the Apple jetzt kaufen. ISBN: 9781285187051, ...
Read more