ios coding best practices

46
iOS Coding Best Practices Jean-Luc David CTO, Digiflare Inc

Upload: jean-luc-david

Post on 15-May-2015

44.542 views

Category:

Technology


2 download

DESCRIPTION

iOS Coding Best Practices

TRANSCRIPT

Page 1: iOS Coding Best Practices

iOS Coding Best PracticesJean-Luc David

CTO, Digiflare Inc

Page 2: iOS Coding Best Practices

Agenda• Project Structure• Design Patterns & Architecture• Storing Data• Coding Conventions

Page 3: iOS Coding Best Practices

What main design pattern does Apple recommend for structuringyour iOS projects?

Page 4: iOS Coding Best Practices

Model-View-Controller

Controller

Model View

Model Data

View Display

Controller Coordination Changes

Update

Update

Changes

Page 5: iOS Coding Best Practices

Default Project

• Notice Classes is a catch-all default ‘’bucket’’ for code

Page 6: iOS Coding Best Practices

MVC Formatted Project• Remove references• Create class folders

in Finder• AppDelegate• Controllers• Helpers• Models

• Drag into Xcode

Page 7: iOS Coding Best Practices

According to Apple, should the model, view or controller be used to capture events?

Page 8: iOS Coding Best Practices

Roles & Responsibilities• Model• Data/Algorithms/Networking• Most of your custom code lives here

• View• Display/Event Capture/Visual Appeal• Don’t try to reinvent UIKit, use it

• Controller• Coordination/Delegation/Odd Jobs• Transitions, Startup/Shutdown

Page 9: iOS Coding Best Practices

Is this a good pattern & why?

Controller

ViewModel

Update

Page 10: iOS Coding Best Practices

Don’t cut out the controller!Avoid bi-directional messaging

Controller

ViewModel

Update

RejectDelay

Validate

• Network Access

• Multiple Choices

• Commit

Page 11: iOS Coding Best Practices

“Home” Controller

“Social” Controller

“Settings” Controller

“Home” View “Social” View “Settings” View

“Home” Model “Social” Model“Settings”

Model

Is this a good pattern & why?

Page 12: iOS Coding Best Practices

Loose Coupling

• Don’t skip MVC layers when messaging• Use controllers to coordinate messages

• Don’t mix MVC roles in one object• Don’t gather too much work in one place

• Don’t declare data in your view classes• Maintenance nightmare• You can display data in a view, just don’t

store it

Page 13: iOS Coding Best Practices

What are three of the design patterns used to communicate between the model, view & controller?

Page 14: iOS Coding Best Practices

Communication Between Objects

Target-Action Notification Delegation

UIButton UIKeyboard

Target

Reuse controls without subclassing

« When tapped, call this method »

-setTarget:(id)target action:(SEL)action…

Observer

Observer« Software keyboard

about to appear! »NSNotificationCenter

Broadcast changes

UITextField

Delegate

“Yes”

“End Editing?”

Control reuse

UIKit (UIScrollView, ect)Will/Did/Should

-(void)scrollViewDidZoom:

Page 15: iOS Coding Best Practices

How do you create a custom controller to split the screen in two parts?

Page 16: iOS Coding Best Practices

You Don’t!Use the UISplitViewController

• Don’t try to reinvent the wheel• UIKit has a lot of the base controls you

need• Really question whether you need a custom

control (and if it exists already)• Don’t misuse framework classes• ie Removing views from UIViewControllers

• Don’t try to reinvent the way models, views & controllers talk to each other• Use delegates & notifications

Page 17: iOS Coding Best Practices

UIViewController

UIScrollView

Is this a good pattern & why?

UITableView

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if ([scrollView isKindOfClass:[UITableView class]}){ // do something } else { // use UIScrollView logic}

Page 18: iOS Coding Best Practices

Class checks in delegate methodsCode unmanagable over time - EverythingControllersCoding horror - iOS version of a GOTO

Controller Controller

View View

Model Model

Correct Approach on iPadParcel out your controllers

Page 19: iOS Coding Best Practices

Controller

View

Model

MANDATORY: ALWAYS write/model out your iOS app design before coding!

ViewView

View

ViewView

Controller Controll

er

Controller

Controller

Model

Model

ModelModel

At first, I was like…(in your brain)

But then, it was like…(in the repo)

Page 20: iOS Coding Best Practices

Creating an MVC diagram of your project• Means you thought through the architecture• Means you know how the code will be

organized physically & logically• Means you potentially avoided structural bugs• Easier to validate with the team• High quality projects & happy clients

Page 21: iOS Coding Best Practices
Page 22: iOS Coding Best Practices

What’s the optimal architecture for a Universal Application?

Page 23: iOS Coding Best Practices

Non-UI Framework (Networking & Models)

UI Framework (Views & Controllers)

iPhone App iPad App

Optimal Universal App Architecture

Page 24: iOS Coding Best Practices

Photo Sharing Application

Data from the model is in both the inspector and in the toolbar

Page 25: iOS Coding Best Practices

MVC Structure

Controller

Controller

Model

Inspector

Toolbar

ChangeUpdate

Change

Update

Page 26: iOS Coding Best Practices

What are the six primary ways of storing data on iOS?

Page 27: iOS Coding Best Practices

Six Model Options

• Property Lists• Archives• Custom Files• Server/iCloud/APIs• SQLite• CoreData

Page 28: iOS Coding Best Practices

According to Apple, what data should you store in your App Defaults/Preferences?

Page 29: iOS Coding Best Practices

Don’t store data in settings!

• Wrong tool for the job• App may get rejected• Settings Panel test• On/Off Advanced Features

Page 30: iOS Coding Best Practices

What should you use for quick storage of strings, numbers, arrays, dictionaries, ect?

Page 31: iOS Coding Best Practices

Property Lists.

Page 32: iOS Coding Best Practices

What should you use to store partial graphs?

Page 33: iOS Coding Best Practices

CoreData• Modeling Tools• Simple save/restore• Queries• Data Protection• Ordered Relationships• UIManagedDocument• Partial Graphs• Undo• Incremental Stores• ect…

Page 34: iOS Coding Best Practices

What should you use to include data with queue-based concurrency in your app?

Page 35: iOS Coding Best Practices

CoreData again.

Page 36: iOS Coding Best Practices

What should you use if you are dealing with a lot of legacy code or data, or you need to create an NSObject-based graph?

Page 37: iOS Coding Best Practices

Custom Files.

Page 38: iOS Coding Best Practices

When would you want to use a data archive?

Page 39: iOS Coding Best Practices

For easily « serializing » and « deserializing » objects in a data file.

Page 40: iOS Coding Best Practices

What are the two primary features of SQLite?

Page 41: iOS Coding Best Practices

Provides Database functionality for iOS appsSupports Object Relational Mapping

Page 42: iOS Coding Best Practices

Know Your Data Model Options

• Property Lists• Archives• Custom Files• Server/iCloud/APIs• SQLite• CoreData

Page 43: iOS Coding Best Practices

Coding Conventions• Brace style for if-else• Parenthesis style• Leading underscores• Code indenting• CapitalizationStyle (ie capitalization_style)

Check out the Google iOS Style guide &Apple docs: http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml

Page 44: iOS Coding Best Practices

What is KVO?

Page 45: iOS Coding Best Practices

Key-Value Observing (KVO)• Requires your code to be compliant• Follow style guide• Instrument your own notifications• Automatic Change Notifications for your

objects

Model

Controller Controller

Page 46: iOS Coding Best Practices

Conclusions• Always start with MVC• Structure your source code for MVC• Don’t fight the framework, understand

what is available & use it• Model out your apps! ***• Understand your data• Use the style guide• Watch all the WWDC videos