ios course day 1

41
Introduction to iOS

Upload: rich-allen

Post on 05-Apr-2017

206 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: iOS course day 1

Introduction to iOS

Page 2: iOS course day 1

Components of Computer System

RAM(Random Access Memory)

Disk(Hard Disk Drive)

CPU(Central Processing Unit)

Page 3: iOS course day 1

How a computer reads our code

Objective-CThis is what our source code is written in.

Compiler

Object Code(otherwise known as Binary)

eg: 0110001010110101

Page 4: iOS course day 1

RAM: The place where applications run. Computers are restrained by memory and 80% of program crashes relate to memory.

Disk: This is where applications that are not running reside along with user data.

CPU: The processor that runs and executes programs

Page 5: iOS course day 1

iPhone and iPad

Page 6: iOS course day 1

iOS Stack

Core OS

Core Services

Media

Coca TouchHigh Level

(Objective C)

Low Level(C)

Page 7: iOS course day 1

Tools

Integrated Development Environment (IDE): Xcode(Code Editor)

Programming Languages: C, C++, Objective-C and Swift

Main

Other ProgramsInstruments, Memory Allocation & Leaks

Page 8: iOS course day 1

Exercise 1Hello World

Page 9: iOS course day 1

Step 1: Start a new project in Xcode

Step 2: Navigate to the App Delegate.m file

Step 3: Add the following code:

Step 4: Build and Run!

Page 10: iOS course day 1

Part 2Programming Basics

Page 11: iOS course day 1

Variables: Variables are a container that holds some kind of value.for example: int myVar = 1;

We now have a variable called myVar and its value is equal to 1;

Page 12: iOS course day 1

Functions: Functions are the way a system performs some kind of action. These can either return a value or not.

Example 1:int myVar = 1;

[self myFunction];

(void)myFunction{myVar = 2;

}

Example 2:int myVar = 1;

myVar = [self myFunction];

(int)myFunction{return 2;

}

Page 13: iOS course day 1

Parameters: Parameters are variables passed into a function for it to do some computation with.Example 1:int myVar = 1;int otherNumber = 20;

int total = [self addNumbers: myVar and: otherNumber];

(int)addNumbers:(int)firstNumber and:(int)secondNumber{int result = firstNumber + secondNumber;return variable;

}

total would equal 21.

Page 14: iOS course day 1

C Programming in 1 SlideInteger

Loops

Array

If Then

Switch

Struct

Page 15: iOS course day 1

PreprocessorProcesses the source code before it gets compiled.

Preprocessor performs a search &replace to all occurrences of the name with its value.

Loads all the content of a file into the current file. - Watch out for circular dependancies!

Objective-C: Loads all the content of a file into the current file. Automatically handles circular dependancies.

Compiler Specific instructions. Often used as a way for you to easily navigate the code file.

Page 16: iOS course day 1

Objective-C

Page 17: iOS course day 1

ClassesObjective C is object orientated

This means the source code is split up into files called classes.

An Objective C Class has Two Files:A interface file (.h)

(otherwise known as a header) A Implementation file (.m)

@implementation ViewController

@end

@interface ViewController : UIViewController

@end

A Header is the public file. It is where we declare methods and variables for use by other classes

The Implementation file is where the logic for the declared methods goes.

Page 18: iOS course day 1

Objective-C: Sending Messages

We want to be able to send messages to a class when trying to set a variable or call a function.

The way we do that is like:

[myClass doSomething];

Page 19: iOS course day 1

Objective-C: Sending Messages With Parameters

If we want to call a function with Parameters we call:

[myClass doSomething: @“aString”];

Page 20: iOS course day 1

Objective-C: Sending Messages With Multiple Parameters

If we want to call a function with Parameters we call:

[myClass doSomethingWith This:@“FirstString”

andThis:@“SecondString”];

Page 21: iOS course day 1

Objective-C: Instantiating ObjectsWhen we want to use a class we need to create a instance of that class in memory. We do that by calling the classes init method, which stands for instantiate.

MyClass *object = [[MyClass alloc]init];

NOTE: we called two methods on this alloc and init. ‘alloc’ is the function that allocates the memory required for the object and then init sets it up.

Page 22: iOS course day 1

Objective-C: More Classes

When we alloc and init a class the system sets us up an instance of our class and then returns a pointer to its location in memory.

You can have multiple instances of a class.

There are some classes called singletons however. This means that there is only one of them in the application. An example of this is the App Delegate.

Page 23: iOS course day 1

Objective-C: Singleton Classes

This is something you will come across more as you go forward however the main one you will encounter is your AppDelegate.

One use of a singleton is if you have a variable that needs to be accused through the app then you would declare it there.

Page 24: iOS course day 1

Objective-C: Memory ManagementSince were are allocating memory all over the place we need to clear this up so that other parts of the program can use it. If we don’t its called a memory leak.

The good news is that these days Apple has created something called Automatic Reference Counting (ARC) which does this for us. We will cover this more later.

Page 25: iOS course day 1

Objective-C: Getters / SettersGetters and Setters are fairly self explanatory. They are the methods you would call on your class to get a value or set a value.

Example:

MyClass *object = [[MyClass alloc]init];[object setRating:9.5];

double theRating = [object rating];

Page 26: iOS course day 1

Objective-C: Getters / Setters

Interface (.h)

Page 27: iOS course day 1

Objective-C: Getters / Setters

Implementation (.m)

Page 28: iOS course day 1

Objective-C: PropertiesMore Good News!!!

Apple has created something called properties.When you declare on of these in the implementation file it will create the getters and setters behind the scene

We’ll look at these in more depth in the next slide but the main thing here is to see how they are declared in the .h and by doing @sythesize in the .m it will create the getters and setters.

Page 29: iOS course day 1

Objective-C: PropertiesHow do we call a property?

We use something called Dot Notation

MyClass *object = [[MyClass alloc]init];object.rating = 9.5;

Page 30: iOS course day 1

Objective-C: Properties / MemoryOk back to Memory as Promised

As promised here is the rest of the memory stuff to get to grips with. With ARC iOS will keep tracks of your memory allocation and when you don’t need an object if will remove it from memory. Once removed if you try and call that object your program will crash.

This makes our life much easier however we need to help it a bit by telling it what needs to stay and what doesn’t. We do this in the property declaration.

An object can have any of the following:

StrongWeakNonatomicAtomic

Page 31: iOS course day 1

Strong: Keeps the object around as long as the class is alive and pointing to it.Weak: Keeps the object around as long as another object is pointing to it strongly.

Objective-C: Properties / Memory II

Atomic/ Nonatomic: -atomic is the default, which provides support for using this property in multiple treads (at no extra cost).

Page 32: iOS course day 1

Objective-C: Foundation Classes

In Objective-C we have the same data types as in other languages except these are objects.

Common Objective C object types:

NSStringNSArrayNSDictionaryNSDateNSNumberNSData

ImmutableNSMutableStringNSMutableArrayNSMutableDictionaryNSDateNSNumberNSData

Mutable

Page 33: iOS course day 1

Objective-C: Foundation Classes II

Each one of the types in objective C have a class associated. This is the same as what we saw in previous slides

In those classes are properties such as ‘length’ for NSString

As well as Getters and Setters.

Page 34: iOS course day 1

Inheritance and Polymorphism

Inheritance is something that you will encounter at all over objective C as well as a lot of modern languages.

Its actually more simple that it looks:

It states that if a class can be a sub class of another class (known as a parent class). This means that the class has all the methods and properties of its parent as well as its own.

Page 35: iOS course day 1

Inheritance and Polymorphism

Vehicle

Car BattleShip

Vehicle.h-(void)move

-(void)turnLeft-(void)turnRight

BattleShip.h-(void)shoot

Car.h-(void)Park

Page 36: iOS course day 1

Inheritance and Polymorphism

Polymorphism is an instance of a class can also be treated as an instance of any of its superclasses.

Page 37: iOS course day 1

Coca-Touch: Model View Controller

In iOS the standard way to design your app is using something called Model View Controller

It breaks down like this:

Model: Data and Business Rules

View: User Interface Elements

Controller: Behaviour and referee between the model and the view

Page 38: iOS course day 1

Coca-Touch: Model View Controller

Model View

Controller

UI Storyboard

ViewController.h

Concert.h

Model data source could be:

File SystemDatabase

Web ServiceCore Data

Page 39: iOS course day 1

Demo

Page 40: iOS course day 1
Page 41: iOS course day 1

• String with format

• App Coda

• Ray Weinerlich