OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS...

73

Transcript of OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS...

Page 1: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers
Page 2: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

OVERVIEW

Why learn iOS programming?Share first-hand experience.Identify platform differences.Identify similarities with .NET

Page 3: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

•Microsoft MVP for 4 years

•C#, WinForms, WPF, Silverlight

• Joined Cynergy about 2 years ago

•Working in native iOS ever since

•iJoshSmith.com (my iOS blog)

Page 4: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

MY BOOK

iOS Programming for .NET DevelopersiosForDotNetDevs.comPaperback, iBooks, Kindle, Nook

Page 5: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

WHY MOBILE?

Huge demand for mobile devs

Large budgets for mobile projects

Lots of greenfield dev work! :-)

Page 6: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

WHY IOS?

Not the only choice, but a good one

Many mobile projects focus on iOS

Perceived difficulty == Mo’ Money

Page 7: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

IOS APP PLATFORMS

Xamarin/MonoTouch - C#

PhoneGap - HTML/JavaScript

Native - Objective-C

Page 8: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

MY FIRST REAL IOS PROJECT

Page 9: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

TRANE MAP

For HVAC salespeopleSelect systems to sell

Present systems to homeownerCreate and sign proposal on iPad

Generate PDF proposal document

Page 11: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

LESSONS LEARNED

Mobile apps are a different breedMobile form factor matters

Apps can (easily) run out of memoryNaming conventions are important

Core Data is terrificBe careful re: data model versioning

Page 12: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

NOT IN KANSAS ANYMOREWindows OS X

Visual Studio Xcode

C# or VB.NET Objective-C

Garbage Collection Reference Counting

.NET Runtime Objective-C Runtime

Base Class Library Foundation

WPF or SL or WinForms UIKit

Page 13: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

TOUR DE XCODELet’s take a guided tour of Apple’s IDE...

Page 14: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

OBJECTIVE-C != C#

Page 15: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

OBJECTIVE-C

Superset of C

Object orientation a la Smalltalk

Obj-C Runtime supports OO

Page 16: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

POINTERS 101

Pointer is 8 bytes of memory

Stores a memory address

Not guaranteed to be valid

Page 17: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

POINTERS EXAMPLE

int *pointerToInt = NULL;int someInt = 42;// Assign the pointer the address of someInt.

pointerToInt = &someInt;// Dereference the pointer and assign it a value.

*pointerToInt = 101;assert(someInt == 101);

Page 18: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

NIL / NULL

nil - for pointer to Obj-C objectNULL - for pointer to C valueNSNull - similar to .NET DBNullNil - for pointer to a Class object

Page 19: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

NIL HANDLING

nil is false and non-nil is trueif (aPointer != nil)...is equivalent to...if (aPointer)

Page 20: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

CALLING A METHOD

Obj-C “message passing”[anObject someMethod];C#anObject.SomeMethod();

Page 21: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

MESSAGE TO NIL

Messages sent to nil are ignored

Evaluates to default of return type (0)

Edge cases if method returns C struct

Page 22: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

PASSING AN ARGUMENT

Obj-C[dev learnSkill:@”iOS”];C#dev.LearnSkill(”iOS”);

Page 23: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

PASSING ARGUMENTS

Obj-C[obj setOffset:42 animated:YES];C#obj.SetOffset(42, true);

Page 24: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

CREATING AN OBJECT

Obj-CJASFoo *f = [[JASFoo alloc] init];JASFoo *f = [JASFoo new];

C#Foo f = new Foo();

Page 25: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

.H & .M FILES

Classes are split between two files

Header (.h) - declarations

Implementation (.m) - definitions

Page 26: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

CLASS DECLARATION

Obj-C (in .h file)@interface JASFoo : NSObject- (BOOL)hello;@end

C# does not have an equivalent

Page 27: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

CLASS DEFINITIONObj-C (in .m file)@implementation JASFoo { NSString *_anIvar;}- (BOOL)hello { return NO; } @endC# (in .cs file)public class Foo { private string _aField; public bool Hello() { return false; }}

Page 28: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

METHODS

Declared in .h file (optional)Defined in .m file

Class methods prefixed with +Instance methods prefixed with -

Page 29: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

CLASS METHODS

Similar to static methods in .NETCan be overridden by subclasses!!!Template:+ (return_type)methodName:(arg_type)argName

Example:+ (JASFoo *)fooWithURL:(NSURL *)url;

Page 30: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

INSTANCE METHODS

Template:- (return_type)methodName:(arg_type)argName

Example:- (int)indexOfObject:(id)anObject;

Page 31: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

PROPERTIES

Declared in .h@property (nonatomic, copy) NSString *name;

Synthesized by compilerCustom accessor methods in .mExample usage:obj.name = @”Arthur Dent”;

Page 32: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

THE SELF POINTER

Similar to ‘this’ in C#

Points to object in instance methods

Points to a Class in class methods

Page 33: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

THE SELF POINTER

Obj-Cfloat f = [self instanceMethod];double d = self->_anIvar;

C#float f = this.InstanceMethod();double d = this.aField;

Page 34: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

OBJECT INITIALIZERLike a constructor in .NET- (id)init { self = [super init]; if (self) { // Assign initial values. _anIvar = 42; } return self;}

Page 35: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

DEALLOC

Called before an object is killed- (void)dealloc { [_anIvar cleanUp]; // Only if ARC disabled... [super dealloc];}

Page 36: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

MEMORY MANAGEMENT

Page 37: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

MEMORY MANAGEMENT

No Garbage Collector

Reference/Retain Counting

Automatic Reference Counting (ARC)

Page 38: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

OBJECT OWNERSHIP

Objects live while owned

Owned == Retained

An owner retains an object

Page 39: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

OBJECT OWNERSHIP

Deallocated when retain count is 0

Deallocated immediately by runtime

Pointer to dead object is “dangling”

Page 40: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

NSOBJECT +ALLOC

+alloc

Allocates new object

Initial retain count of 1

Page 41: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

NSOBJECT -RETAIN

-retain method

Increments receiver’s retain count

Page 42: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

NSOBJECT -RELEASE

-release method

Decrements receiver’s retain count

If retain count reaches 0 object dies

Page 43: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

NSOBJECT -AUTORELEASE

-autorelease method

Delayed decrement of retain count

Used when returning a new object

Page 44: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

NSOBJECT -DEALLOC

-dealloc method

Relinquish ownership of objects

Invoked when retain count is 0

Page 45: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

EXAMPLEFoo *f = [[Foo alloc] init];// f’s retain count is 1[f doSomethingAwesome];[f release];// f’s retain count is 0// f’s -dealloc executesf = nil;// Assign nil to avoid dangling pointer

Page 46: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

ARC

Compiler feature

Adds memory mgmt to your code

Introduced in iOS 5

Page 47: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

EXAMPLE WITH ARC

Foo *f = [[Foo alloc] init];[f doSomethingAwesome];

Page 48: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

FOUNDATION VS. BCL

Page 49: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

OBJECT VS. NSOBJECT

Page 50: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

STRING VS. NSSTRING

Page 51: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

STRINGBUILDER VS. NSMUTABLESTRING

Page 52: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

DATETIME VS. NSDATE

Page 53: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

ARRAY VS. NSARRAY

Page 54: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

HASHTABLE VS. NSMUTABLEDICTIONARY

Page 55: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

UIKIT

Page 56: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

UIKIT

High-level user interface API

Provides the “iOS look & feel”

Based on MVC pattern

Page 57: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

DRAWING SYSTEMS

Page 58: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

VIEWS

UIView is the basic UI object

Views can contain subviews

Views are contained by a superview

Page 59: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

UIVIEW SUBCLASSES

UIButtonUILabelUITextFieldUIImageViewUITableView

Page 60: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

VIEW CONTROLLERS

Manage a set of Views

Container controller (manages controllers)

Content controller (displays stuff)

Page 61: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

CONTAINER CONTROLLERS

UINavigationControllerUITabBarControllerUIPageViewControllerUISplitViewController (iPad only)

Page 62: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

CONTENT CONTROLLERS

UITableViewController subclasses

Your UIViewController subclasses

Page 63: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers
Page 64: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

MODAL PRESENTATION

Controllers can be ‘presented’

Presented controllers are ‘modal’

Modal controllers are ‘dismissed’

Page 65: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

PRESENTATION EXAMPLE

Page 66: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

XIB FILE

Contains a View

XML data

Edited via Interface Builder

Page 67: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

STORYBOARD FILE

Contains multiple Views (“scenes”)

Defines navigation paths (“segues”)

Edited via Interface Builder

Page 68: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

OUTLETS

A property or ivar on a Controller

Assigned a View pointer automatically

Hooked up in Interface Builder

Page 69: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

ACTIONS

UI event handling methods

Defined in Controller classes

Hooked up in Interface Builder

Page 70: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

LET’S WRITE AN APP!

Create View with Interface BuilderAdd outlets and action to Controller

Concatenate two NSStringsDisplay calculated value in View

Page 71: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

SUMMARY

Different terms, similar ideasXcode is Apple’s free IDE

Objective-C is (kinda) like C#Foundation contains common classesUIKit and Interface Builder create UIs

There is a lot more to know...

Page 72: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

LEARN MORE

iosForDotNetDevs.com‘iOS Programming for .NET Developers’

Page 73: OVERVIEW - Josh Smith on WPF · OVERVIEW Why learn iOS programming? ... •Working in native iOS ever since •iJoshSmith.com (my iOS blog) MY BOOK iOS Programming for .NET Developers

Q&A