Download - iOS Coding Best Practices
![Page 1: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/1.jpg)
iOS Coding Best PracticesJean-Luc David
CTO, Digiflare Inc
![Page 2: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/2.jpg)
Agenda• Project Structure• Design Patterns & Architecture• Storing Data• Coding Conventions
![Page 3: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/3.jpg)
What main design pattern does Apple recommend for structuringyour iOS projects?
![Page 4: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/4.jpg)
Model-View-Controller
Controller
Model View
Model Data
View Display
Controller Coordination Changes
Update
Update
Changes
![Page 5: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/5.jpg)
Default Project
• Notice Classes is a catch-all default ‘’bucket’’ for code
![Page 6: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/6.jpg)
MVC Formatted Project• Remove references• Create class folders
in Finder• AppDelegate• Controllers• Helpers• Models
• Drag into Xcode
![Page 7: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/7.jpg)
According to Apple, should the model, view or controller be used to capture events?
![Page 8: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/9.jpg)
Is this a good pattern & why?
Controller
ViewModel
Update
![Page 10: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/11.jpg)
“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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/13.jpg)
What are three of the design patterns used to communicate between the model, view & controller?
![Page 14: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/15.jpg)
How do you create a custom controller to split the screen in two parts?
![Page 16: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/21.jpg)
![Page 22: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/22.jpg)
What’s the optimal architecture for a Universal Application?
![Page 23: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/23.jpg)
Non-UI Framework (Networking & Models)
UI Framework (Views & Controllers)
iPhone App iPad App
Optimal Universal App Architecture
![Page 24: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/24.jpg)
Photo Sharing Application
Data from the model is in both the inspector and in the toolbar
![Page 25: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/25.jpg)
MVC Structure
Controller
Controller
Model
Inspector
Toolbar
ChangeUpdate
Change
Update
![Page 26: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/26.jpg)
What are the six primary ways of storing data on iOS?
![Page 27: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/27.jpg)
Six Model Options
• Property Lists• Archives• Custom Files• Server/iCloud/APIs• SQLite• CoreData
![Page 28: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/28.jpg)
According to Apple, what data should you store in your App Defaults/Preferences?
![Page 29: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/30.jpg)
What should you use for quick storage of strings, numbers, arrays, dictionaries, ect?
![Page 31: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/31.jpg)
Property Lists.
![Page 32: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/32.jpg)
What should you use to store partial graphs?
![Page 33: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/33.jpg)
CoreData• Modeling Tools• Simple save/restore• Queries• Data Protection• Ordered Relationships• UIManagedDocument• Partial Graphs• Undo• Incremental Stores• ect…
![Page 34: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/34.jpg)
What should you use to include data with queue-based concurrency in your app?
![Page 35: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/35.jpg)
CoreData again.
![Page 36: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/36.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/37.jpg)
Custom Files.
![Page 38: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/38.jpg)
When would you want to use a data archive?
![Page 39: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/39.jpg)
For easily « serializing » and « deserializing » objects in a data file.
![Page 40: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/40.jpg)
What are the two primary features of SQLite?
![Page 41: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/41.jpg)
Provides Database functionality for iOS appsSupports Object Relational Mapping
![Page 42: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/42.jpg)
Know Your Data Model Options
• Property Lists• Archives• Custom Files• Server/iCloud/APIs• SQLite• CoreData
![Page 43: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/43.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/44.jpg)
What is KVO?
![Page 45: iOS Coding Best Practices](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/45.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022052307/5555bdc6d8b42afe5d8b5139/html5/thumbnails/46.jpg)
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