ios course day 1

Post on 05-Apr-2017

206 Views

Category:

Engineering

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to iOS

Components of Computer System

RAM(Random Access Memory)

Disk(Hard Disk Drive)

CPU(Central Processing Unit)

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

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

iPhone and iPad

iOS Stack

Core OS

Core Services

Media

Coca TouchHigh Level

(Objective C)

Low Level(C)

Tools

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

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

Main

Other ProgramsInstruments, Memory Allocation & Leaks

Exercise 1Hello World

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!

Part 2Programming Basics

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;

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;

}

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.

C Programming in 1 SlideInteger

Loops

Array

If Then

Switch

Struct

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.

Objective-C

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.

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

Objective-C: Sending Messages With Parameters

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

[myClass doSomething: @“aString”];

Objective-C: Sending Messages With Multiple Parameters

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

[myClass doSomethingWith This:@“FirstString”

andThis:@“SecondString”];

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.

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.

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.

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.

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

Objective-C: Getters / Setters

Interface (.h)

Objective-C: Getters / Setters

Implementation (.m)

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.

Objective-C: PropertiesHow do we call a property?

We use something called Dot Notation

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

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

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

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

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.

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.

Inheritance and Polymorphism

Vehicle

Car BattleShip

Vehicle.h-(void)move

-(void)turnLeft-(void)turnRight

BattleShip.h-(void)shoot

Car.h-(void)Park

Inheritance and Polymorphism

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

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

Coca-Touch: Model View Controller

Model View

Controller

UI Storyboard

ViewController.h

Concert.h

Model data source could be:

File SystemDatabase

Web ServiceCore Data

Demo

• String with format

• App Coda

• Ray Weinerlich

top related