Intro to iOS: Object Oriented Programming and Objective-C

15
Developing for iOS

description

What is iOS programming like? What is Object Oriented programming and it's core features? A basic intro to Objective-C

Transcript of Intro to iOS: Object Oriented Programming and Objective-C

Page 1: Intro to iOS: Object Oriented Programming and Objective-C

Developing for iOS

Page 2: Intro to iOS: Object Oriented Programming and Objective-C

What is developing for iOS?

• The language – Objective-C but most recently Swift

• The tools – Xcode mostly

• The concepts - MVC

• The frameworks (libraries) - Cocoa touch (for iOS) is the API: Foundation and UIKit, and many others

Page 3: Intro to iOS: Object Oriented Programming and Objective-C

Intro to Object Oriented ProgrammingWhat is it and why use it?

A programming paradigm. Wraps up state and behavior into one package. Make “blueprints” for creating objects. Objects are abstract data types.

State = instance variables. The ‘nouns’ of an object. What exactly is the object and what does it represent?

Behavior = methods. The ‘verbs’ of a object. What does the object actually do?

Page 4: Intro to iOS: Object Oriented Programming and Objective-C

Why OO programming?

• Modularity: Program can be broken into pieces.• Abstraction: Hide the details.• Maintenance. Don’t have to modify existing

objects as long as you interface with them properly.

• Reuse. Many objects do the same functionality. So why write more code? Just instantiate more instances of an object.

Page 5: Intro to iOS: Object Oriented Programming and Objective-C

What is a class?

• Class is a “template” or “blueprint” that is used to create objects.

• Describes the behavior and properties common to any particular type of object.

• You use a class to instantiate an instance of an object.

Page 6: Intro to iOS: Object Oriented Programming and Objective-C

Object Oriented Concepts

• Abstraction: Hide the details of the implementation and just deal with “what” instead of “how”.

• Encapsulation : Enforces modularity. Wrapping up data members and method together into a single unit. Protect the object from other objects.

• Inheritance

• Polymorphism

Page 7: Intro to iOS: Object Oriented Programming and Objective-C

OO Concepts: Inheritance

Objects can acquire the properties of another class. Promotes reusability of code by using existing code from parent class. Child class adopts parent class features and has its own features.

Note the inheritance:All animals eat and sleep but the child classes have theirown unique behaviors as well.

Page 8: Intro to iOS: Object Oriented Programming and Objective-C

OO Concepts: Polymorphism

• Polymorphism: The ability of different objects to respond, each in its own way, to identical messages is called polymorphism. Note: All these shapes will draw but of course they’re implemented in different ways.

Page 9: Intro to iOS: Object Oriented Programming and Objective-C

• Objective-C is a super set of C.• Objective-C is also known for its verbose

naming conventions.• Designed to be reader friendly for humans.• Developed by NextStep. Company created by

Steve Jobs after he was kicked from Apple.• Objects everywhere. • Objective-C is decidedly more dynamic,

deferring most of its decisions to run-time rather than compile-time.

Intro to Objective-C

Page 10: Intro to iOS: Object Oriented Programming and Objective-C

Some Objective-C Data Types

• Some common data types:

• NSString *myString = nil;

• NSString *aString = @"Hello”;

• NSArray *myArray = @[object1, object2, object3];

• NSNumber *num = [[NSNumber alloc] initWithFloat:3.6];

• id = Hold a reference to any object.

Notice the pointers (*) everywhere. Object instantiation galore. This has to do with Obj-C’s runtime. Obj-C is a very dynamic language. So it throws everything on the heap and just points to the objects.

Page 11: Intro to iOS: Object Oriented Programming and Objective-C

Method Examples

Page 12: Intro to iOS: Object Oriented Programming and Objective-C

“Calling a method” vs “Sending a message”

• Theoretically, they're different but in practice they’re not. Difference comes from the Objective-C runtime.

• Most compiled languages refer to methods and functions internally as memory offsets where the method resides. This has the advantage of being very fast. The starting point is known at compile-time.

• Objective-C runtime maintains a list of all methods and functions. This allows for dynamic behavior by allowing us to modify what section of code a method executes, not responding to a message, or introspection.

• 99% of the time sending a message to an object will call the method intended. So in practice there’s not much difference.

Page 13: Intro to iOS: Object Oriented Programming and Objective-C

Basic Objective-C Header file

Notice the import of Foundation. Foundation gives basic functionality for things like NSString, NSArray, NSObject, etc. It will be automatically imported to your classes when you create a new class file in Xcode.

All objects will inherit from NSObject. Just do it.

Page 14: Intro to iOS: Object Oriented Programming and Objective-C

Book Implementation File

Page 15: Intro to iOS: Object Oriented Programming and Objective-C

Model View Controller: An architectural pattern for organizing your code. Commonly used when implementing UI.

Watch this video: http://bit.ly/Zjsic2 (Stanford University Developing iOS 7 Apps: Lecture 1)