Está en la página 1de 46

iOS Coding Best Practices

Jean-Luc David
CTO, Digiflare Inc

Agenda

Project Structure
Design Patterns & Architecture
Storing Data
Coding Conventions

What main design pattern


does
Apple recommend for
structuring
your iOS projects?

Model-View-Controller
Controller
Coordination
Update

Controller

Model
Model
Data

Changes

Update

Changes

View
View
Display

Default Project

Notice Classes is a
catch-all default
bucket for code

MVC Formatted Project


Remove references
Create class folders
in Finder

AppDelegate
Controllers
Helpers
Models

Drag into Xcode

According to Apple, should the


model, view or controller be
used to capture events?

Roles & Responsibilities


Model
Data/Algorithms/Networking
Most of your custom code lives here

View
Display/Event Capture/Visual Appeal
Dont try to reinvent UIKit, use it

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

Is this a good pattern & why?

Controller

Model

Update

View

Dont cut out the controller!


Avoid bi-directional messaging
Reject
Delay
Validate

Controller

Update
Model

View

Network
Access
Multiple
Choices
Commit

Is this a good pattern & why?


Home View

Social View

Settings View

Home
Controller

Social
Controller

Settings
Controller

Home Model

Social Model

Settings
Model

Loose Coupling
Dont skip MVC layers when messaging
Use controllers to coordinate messages
Dont mix MVC roles in one object
Dont gather too much work in one place
Dont declare data in your view classes
Maintenance nightmare
You can display data in a view, just dont
store it

What are three of the design


patterns used to communicate
between the model, view &
controller?

Communication Between
Objects
Target-Action
Reuse controls
without subclassing

Target

UIButton

When tapped, call


this method

Notification

Broadcast changes

Delegation
Control reuse

Yes
Observe
r
Observe
r

UIKeyboar
d

Software
keyboard
setTarget:(id)target action:(SEL)action
about to appear!
NSNotificationCenter

Delegat
e

UITextFiel
d

End Editing?

UIKit (UIScrollView, ect)


Will/Did/Should
-(void)scrollViewDidZoom:

How do you create a custom


controller to split the screen in
two parts?

You Dont!
Use the UISplitViewController
Dont 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)
Dont misuse framework classes
ie Removing views from UIViewControllers
Dont try to reinvent the way models, views &
controllers talk to each other
Use delegates & notifications

Is this a good pattern & why?


UITableView
UIScrollView
UIViewController

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

Class checks in delegate methods


Code unmanagable over time EverythingControllers
Coding horror
- iOS version
of a GOTO
Correct
Approach
on

iPad
Parcel out your
controllers
View

View

Controller

Controller

Model

Model

MANDATORY: ALWAYS write/model


out your iOS app design before coding!
At first, I was like
(in your brain)

View

Controll
er

But then, it was like


(in the repo)

View

View
View

View

Controll
Controll
er Controll er
er

Model

Model

View

Controll
er

Model
Model

Model

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 bug
Easier to validate with the team
High quality projects & happy clients

Whats the optimal architecture


for
a Universal Application?

Optimal Universal App


Architecture
iPhone App

iPad App

UI Framework (Views & Controllers)

Non-UI Framework (Networking & Models)

Photo Sharing Application

Data from the model


is in both the inspecto
and in the toolbar

MVC Structure
Update

Controller

Change

Inspector

Change
Model

Toolbar
Update
Controller

What are the six primary ways


of storing data on iOS?

Six Model Options

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

According to Apple, what data


should you store in your App
Defaults/Preferences?

Dont store data in settings!


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

What should you use for quick


storage of strings, numbers,
arrays, dictionaries, ect?

Property Lists.

What should you use to store


partial graphs?

CoreData

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

What should you use to include


data with queue-based
concurrency in your app?

CoreData again.

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?

Custom Files.

When would you want to use a


data archive?

For easily
serializing and
deserializing
objects in a data file.

What are the two primary features


of SQLite?

Provides Database functionality for iOS


apps
Supports Object Relational Mapping

Know Your Data Model Options

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

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/tr
unk/
objcguide.xml

What is KVO?

Key-Value Observing (KVO)

Requires your code to be compliant


Follow style guide
Instrument your own notifications
Automatic Change Notifications for your
objects
Controller

Controller

Model

Conclusions

Always start with MVC


Structure your source code for MVC
Dont 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

También podría gustarte