ios beginners lesson 1

68
INTRODUCTION to iOS Programming

Upload: calvin-cheng

Post on 27-May-2015

233 views

Category:

Software


3 download

DESCRIPTION

Fundamentals for programmers new to iOS development with Objective-C

TRANSCRIPT

Page 1: iOS Beginners Lesson 1

INTRODUCTIONto iOS Programming

Page 2: iOS Beginners Lesson 1

ME• Travel & outdoor sports

• Chemical engineering

• Freezer engineering

• Software consultant, technology businesses

• Optometry & Real Estate

Page 3: iOS Beginners Lesson 1

ME

• http://calvinx.com

• Twitter @calvinchengx

• FB http://facebook.com/calvin.cheng.lc

• Github http://github.com/calvinchengx

Page 4: iOS Beginners Lesson 1

YOU?

Page 5: iOS Beginners Lesson 1

WHY?

• Why do we learn programming?

• Why do you want to learn programming?

• What problems do you want to solve?

• Who can we help?

Page 6: iOS Beginners Lesson 1

OVERVIEW

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface

Page 7: iOS Beginners Lesson 1

LESSON 1: INTRODUCTIONS

• Hour 1 - Demo and Set-up

• Hour 2 - Objective-C Primer

• Hour 3 - MVC Architecture

Page 8: iOS Beginners Lesson 1

DEMO

Page 9: iOS Beginners Lesson 1

APPLE DEVELOPER

Page 10: iOS Beginners Lesson 1

PREREQUISITES

• Do you know an existing programming language or scripting language?

• Control logic, data and interfaces - what do you want the user to see and/or do?

• Objective-Oriented Programming?

Page 11: iOS Beginners Lesson 1

COMPARISONS

Page 12: iOS Beginners Lesson 1

OBJECT-ORIENTED PROGRAMMING

• Class

• Objects

• Functions

• Methods

Page 13: iOS Beginners Lesson 1

KNOW A PROGRAMMINGLANGUAGE?

• C

• Java?

• Javascript?

• Python?

• Ruby?

Page 14: iOS Beginners Lesson 1

C-STYLE LANGUAGES• C

• C++

• Objective-C as a superset of C

• PHP

• C#

• Java

• Perl

Page 15: iOS Beginners Lesson 1

OBJECTIVE-C SYNTAX

• @ is used to identify Objective-C

• Special keywords, e.g. protocol, property, interface, implementation, NSObject, NSInteger, NSNumber etc

• Tokens, e.g. @, (, ; etc

Page 16: iOS Beginners Lesson 1

XCODE: CREATE PROJECT

Page 17: iOS Beginners Lesson 1

XCODE: CREATE PROJECT

Page 18: iOS Beginners Lesson 1

OBJECTIVE-C PRIMER

Page 19: iOS Beginners Lesson 1

• Basic Syntax

• Variables and Data Types

• Working with Objects

• Classes and Objects

• Collections

• Files

• Language Features

• Errors and Debugging

Page 20: iOS Beginners Lesson 1

BASIC SYNTAX

• Objective-C is a superset of C programming language

• i.e. it understands all C syntax

• AND it introduces new syntax of its own on top of C

Page 21: iOS Beginners Lesson 1

BASIC SYNTAX

Page 22: iOS Beginners Lesson 1

BASIC SYNTAX

Page 23: iOS Beginners Lesson 1

BASIC SYNTAX

Code is grouped into “pairs” of:

• Header file

• Implementation file

Page 24: iOS Beginners Lesson 1

BASIC SYNTAX

Page 25: iOS Beginners Lesson 1

BASIC SYNTAX

Page 26: iOS Beginners Lesson 1

BASIC SYNTAX

Page 27: iOS Beginners Lesson 1

VARIABLES & DATA TYPE

[email protected]

#ffffff

37

2008-08-08

Page 28: iOS Beginners Lesson 1

VARIABLES & DATA TYPE

[email protected]

#ffffff

37

2008-08-08

email:

color:

age:

date of event:

Page 29: iOS Beginners Lesson 1

VARIABLES & DATA TYPE

Javascript

var a = “wonderful”;

a = 123;

a = 1.289;

a = false;

Page 30: iOS Beginners Lesson 1

VARIABLES & DATA TYPE

Objective-C

type variable value

int homeworkScore = 87;

Page 31: iOS Beginners Lesson 1

VARIABLE & DATA TYPE

Naming convention: camel case

firstName

score

playerKarma

my8Number NOT RECOMMENDED

int_score NOT RECOMMENDED

Page 32: iOS Beginners Lesson 1

VARIABLES & DATA TYPE

Primitive Data Types

int

float

double

char

BOOL Objective-C

Page 33: iOS Beginners Lesson 1

VARIABLES & DATA TYPEComposite Data Types

int day = 28;

int month = 11;

int year = 2011;

int secondsSince1970 = 1294909373;

char c1 = ‘h’;

char c2 = ‘e’;

char c3 = ‘l’;

char c4 = ‘l’;

char c5 = ‘o’;

Page 34: iOS Beginners Lesson 1

VARIABLES & DATA TYPEComplex Types: iOS SDK (CocoaTouch Framework)

NSString *myString = @“Hello”;

NSDate *today = [NSDate date];

Page 35: iOS Beginners Lesson 1

WORKING WITH OBJECTS

Procedural Program (Example C program)

Page 36: iOS Beginners Lesson 1

WORKING WITH OBJECTSObject Oriented Programs (Objective-C)

datalogic

datalogic

datalogic

Page 37: iOS Beginners Lesson 1

WORKING WITH OBJECTS

class object

Page 38: iOS Beginners Lesson 1

CLASS = OBJECT BLUEPRINT

Page 39: iOS Beginners Lesson 1

CLASS = OBJECT BLUEPRINT

NSDate *today = [NSDate date];

ref - https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

Page 40: iOS Beginners Lesson 1

CLASS = OBJECT BLUEPRINT

NSDate *today = [NSDate date];

ref - https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

What’s this?

Page 41: iOS Beginners Lesson 1

CLASS = OBJECT BLUEPRINT

• Pointers!

• All objects are accessed using Pointers

• * represents a pointer and a primitive value

Page 42: iOS Beginners Lesson 1

USING OBJECTS

Page 43: iOS Beginners Lesson 1

CLASS = OBJECT BLUEPRINT

NSDate *today = [NSDate date];

ref - https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

What’s this?

Page 44: iOS Beginners Lesson 1

METHODS

NSDate *today = [NSDate date];

ref - https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

MethodClassType Pointer

denotes that this variable is a Pointer

Page 45: iOS Beginners Lesson 1

METHODS

• Class Methods

• Instance Methods (also known as Object Methods)

Page 46: iOS Beginners Lesson 1

METHODS

NSDate *today = [NSDate date]

NSTimeInterval timeInterval = 123;

NSDate *dateFromInterval = [NSDate dateWithTimeInterval:timeInterval sinceDate:today]

Class Method

Page 47: iOS Beginners Lesson 1

METHODS

NSComparsionResult result = [today compare:dateFromInterval]

Instance Methoda.k.a. Object Method

NSDate object from previous slide

NSDate object from previous slide

Page 48: iOS Beginners Lesson 1

COLLECTIONS

• A group of data or objects

• Various languages refer to them as “arrays” or “lists” or “dictionaries” or “linked lists” (each with slightly different underlying meaning)

Page 49: iOS Beginners Lesson 1

COLLECTIONS

Page 50: iOS Beginners Lesson 1

FILES

Page 51: iOS Beginners Lesson 1

FILES• All programming languages provide a

way to write to your computer’s/operating system’s filesystem

• If we cannot put things into a file, the data that we are working with will not be available when our computer is switched off since it’s only in the memory

Page 52: iOS Beginners Lesson 1

FILES

Page 53: iOS Beginners Lesson 1

LANGUAGE FEATURES

• Inheritance (e.g. NSObject > NSResponder > NSView > NSControl > NSButton)

• Categories: extending a class with new methods without sub-classing

Page 54: iOS Beginners Lesson 1

LANGUAGE FEATURES

• Class Extensions: extending a class with new properties without sub-classing (only “.h” created, no implementation file :-))

Page 55: iOS Beginners Lesson 1

LANGUAGE FEATURES

• Categories versus Class Extensions

Page 56: iOS Beginners Lesson 1

LANGUAGE FEATURES

• Protocols: a list of methods you want a object to perform

• required methods

• optional methods

• ANY object can perform those methods

• Think of it as a set/group of methods (“functions”) that don’t belong to any class

Page 57: iOS Beginners Lesson 1

LANGUAGE FEATURES

• Dynamic Typing with id

• id is a generic object pointer

Page 58: iOS Beginners Lesson 1

ERRORS & DEBUGGING• Issue navigator

• Pick the first error (don’t start at the bottom)

• Common errors:

• Missing pointer symbol

• Missing semi-colons or braces (parsing error)

• Undeclared identifier (didn’t import a header?)

• Learning to use breakpoints

Page 59: iOS Beginners Lesson 1

MVC ARCHITECTURE

• What is MVC?

• Using the iOS Simulator in Xcode

• Exercise Files

• Features of our Note Taking App

Page 60: iOS Beginners Lesson 1

WHAT IS MVC?

• Model

• View

• Controller

Page 61: iOS Beginners Lesson 1

WHAT IS MVC?

Model

• Representation of your class (“blueprint”, .h, .m)

• Set properties and behaviour of your class

• Set properties and behaviour of your class’ objects

• Handles data storage in memory

• Handles data storage on disk

• Handles data storage to the “cloud”/backend

Page 62: iOS Beginners Lesson 1

WHAT IS MVC?

View

• The interface for the user of your app - “Scene”, “Storyboard”

• Listens to user interactions - tap, swipe, other events (shaking your phone)

• Shows the user responses and ask for user responses

• Aesthetic purpose as well as User function purpose

• Usually tied to the logic (i.e. “controller”)

Page 63: iOS Beginners Lesson 1

WHAT IS MVC?

Controller

• “ViewController” (.h, .m)

• Your app should be organised with multiple ViewControllers with different purposes

• Calls out classes, class methods, objects, object methods from your models, changes data according to user interactions and accepts or pushes data to the view

• Using linked to scenes and UI (User Interface) object(s) on a scene

Page 64: iOS Beginners Lesson 1

WHY MVC?

• Organised, modular (cleanly separated code for easy debugging)

• A convention for teamwork so others can easily get the “big picture” of the components that make your app work

Page 65: iOS Beginners Lesson 1

IOS SIMULATOR

Page 66: iOS Beginners Lesson 1

EXERCISE FILES

• Github url

Page 67: iOS Beginners Lesson 1

FEATURES OF OUR NOTE TAKING APP

• Show list of notes

• Create a note and be able to persist the note (“save”)

• Edit a note

• Delete a note

Page 68: iOS Beginners Lesson 1

WHAT’S NEXT?

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface