iOS Beginners Lesson 1

Post on 27-May-2015

235 views 3 download

Tags:

description

Fundamentals for programmers new to iOS development with Objective-C

Transcript of iOS Beginners Lesson 1

INTRODUCTIONto iOS Programming

ME• Travel & outdoor sports

• Chemical engineering

• Freezer engineering

• Software consultant, technology businesses

• Optometry & Real Estate

ME

• http://calvinx.com

• Twitter @calvinchengx

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

• Github http://github.com/calvinchengx

YOU?

WHY?

• Why do we learn programming?

• Why do you want to learn programming?

• What problems do you want to solve?

• Who can we help?

OVERVIEW

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface

LESSON 1: INTRODUCTIONS

• Hour 1 - Demo and Set-up

• Hour 2 - Objective-C Primer

• Hour 3 - MVC Architecture

DEMO

APPLE DEVELOPER

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?

COMPARISONS

OBJECT-ORIENTED PROGRAMMING

• Class

• Objects

• Functions

• Methods

KNOW A PROGRAMMINGLANGUAGE?

• C

• Java?

• Javascript?

• Python?

• Ruby?

C-STYLE LANGUAGES• C

• C++

• Objective-C as a superset of C

• PHP

• C#

• Java

• Perl

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

XCODE: CREATE PROJECT

XCODE: CREATE PROJECT

OBJECTIVE-C PRIMER

• Basic Syntax

• Variables and Data Types

• Working with Objects

• Classes and Objects

• Collections

• Files

• Language Features

• Errors and Debugging

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

BASIC SYNTAX

BASIC SYNTAX

BASIC SYNTAX

Code is grouped into “pairs” of:

• Header file

• Implementation file

BASIC SYNTAX

BASIC SYNTAX

BASIC SYNTAX

VARIABLES & DATA TYPE

myemail@domain.com

#ffffff

37

2008-08-08

VARIABLES & DATA TYPE

myemail@domain.com

#ffffff

37

2008-08-08

email:

color:

age:

date of event:

VARIABLES & DATA TYPE

Javascript

var a = “wonderful”;

a = 123;

a = 1.289;

a = false;

VARIABLES & DATA TYPE

Objective-C

type variable value

int homeworkScore = 87;

VARIABLE & DATA TYPE

Naming convention: camel case

firstName

score

playerKarma

my8Number NOT RECOMMENDED

int_score NOT RECOMMENDED

VARIABLES & DATA TYPE

Primitive Data Types

int

float

double

char

BOOL Objective-C

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’;

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

NSString *myString = @“Hello”;

NSDate *today = [NSDate date];

WORKING WITH OBJECTS

Procedural Program (Example C program)

WORKING WITH OBJECTSObject Oriented Programs (Objective-C)

datalogic

datalogic

datalogic

WORKING WITH OBJECTS

class object

CLASS = OBJECT BLUEPRINT

CLASS = OBJECT BLUEPRINT

NSDate *today = [NSDate date];

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

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?

CLASS = OBJECT BLUEPRINT

• Pointers!

• All objects are accessed using Pointers

• * represents a pointer and a primitive value

USING OBJECTS

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?

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

METHODS

• Class Methods

• Instance Methods (also known as Object Methods)

METHODS

NSDate *today = [NSDate date]

NSTimeInterval timeInterval = 123;

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

Class Method

METHODS

NSComparsionResult result = [today compare:dateFromInterval]

Instance Methoda.k.a. Object Method

NSDate object from previous slide

NSDate object from previous slide

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)

COLLECTIONS

FILES

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

FILES

LANGUAGE FEATURES

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

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

LANGUAGE FEATURES

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

LANGUAGE FEATURES

• Categories versus Class Extensions

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

LANGUAGE FEATURES

• Dynamic Typing with id

• id is a generic object pointer

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

MVC ARCHITECTURE

• What is MVC?

• Using the iOS Simulator in Xcode

• Exercise Files

• Features of our Note Taking App

WHAT IS MVC?

• Model

• View

• Controller

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

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”)

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

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

IOS SIMULATOR

EXERCISE FILES

• Github url

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

WHAT’S NEXT?

• Lesson 1: Introductions

• Lesson 2: iOS specifics

• Lesson 3: Data Model

• Lesson 4: Logic (Controller) & Interface