Date post: | 15-May-2015 |
Category: |
Technology |
Upload: | jean-luc-david |
View: | 44,542 times |
Download: | 2 times |
iOS Coding Best PracticesJean-Luc David
CTO, Digiflare Inc
Agenda• Project Structure• Design Patterns & Architecture• Storing Data• Coding Conventions
What main design pattern does Apple recommend for structuringyour iOS projects?
Model-View-Controller
Controller
Model View
Model Data
View Display
Controller Coordination Changes
Update
Update
Changes
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• Don’t try to reinvent UIKit, use it
• Controller• Coordination/Delegation/Odd Jobs• Transitions, Startup/Shutdown
Is this a good pattern & why?
Controller
ViewModel
Update
Don’t cut out the controller!Avoid bi-directional messaging
Controller
ViewModel
Update
RejectDelay
Validate
• Network Access
• Multiple Choices
• Commit
“Home” Controller
“Social” Controller
“Settings” Controller
“Home” View “Social” View “Settings” View
“Home” Model “Social” Model“Settings”
Model
Is this a good pattern & why?
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
What are three of the design patterns used to communicate between the model, view & controller?
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:
How do you create a custom controller to split the screen in two parts?
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
UIViewController
UIScrollView
Is this a good pattern & why?
UITableView
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if ([scrollView isKindOfClass:[UITableView class]}){ // do something } else { // use UIScrollView logic}
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
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)
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
What’s the optimal architecture for a Universal Application?
Non-UI Framework (Networking & Models)
UI Framework (Views & Controllers)
iPhone App iPad App
Optimal Universal App Architecture
Photo Sharing Application
Data from the model is in both the inspector and in the toolbar
MVC Structure
Controller
Controller
Model
Inspector
Toolbar
ChangeUpdate
Change
Update
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?
Don’t 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 appsSupports 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/trunk/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
Model
Controller Controller
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