iOS Coding Best Practices

65,407 views

Published on

iOS Coding Best Practices

Published in: Technology
7 Comments
101 Likes
Statistics
Notes
No Downloads
Views
Total views
65,407
On SlideShare
0
From Embeds
0
Number of Embeds
2,635
Actions
Shares
0
Downloads
1,453
Comments
7
Likes
101
Embeds 0
No embeds

No notes for slide

iOS Coding Best Practices

  1. 1. Agenda• Project Structure• Design Patterns & Architecture• Storing Data• Coding Conventions
  2. 2. What main design pattern doesApple recommend forstructuringyour iOS projects?
  3. 3. Model-View-Controller Controller Update Coordination Changes Changes Update Model Data View Display
  4. 4. Default Project • Notice Classes is a catch-all default ‘’bucket’’ for code
  5. 5. MVC Formatted Project• Remove references• Create class folders in Finder • AppDelegate • Controllers • Helpers • Models• Drag into Xcode
  6. 6. According to Apple, should themodel, view or controller be usedto capture events?
  7. 7. 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
  8. 8. Is this a good pattern & why? Update
  9. 9. Don’t cut out the controller! Avoid bi-directional messaging Reject DelayValidate Update • Network Access • Multiple Choices • Commit
  10. 10. Is this a good pattern & why?
  11. 11. 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
  12. 12. What are three of the designpatterns used to communicatebetween the model, view &controller?
  13. 13. Communication Between Objects Target-Action Notification Delegation Reuse controls without Broadcast changes Control reuse subclassing “Yes” « When tapped, call this method » « Software keyboard “End Editing?”-setTarget:(id)target action:(SEL)action…about to appear! » UIKit (UIScrollView, ect) NSNotificationCenter Will/Did/Should -(void)scrollViewDidZoom:
  14. 14. How do you create a customcontroller to split the screen in twoparts?
  15. 15. 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
  16. 16. Is this a good pattern & why?-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if ([scrollView isKindOfClass:[UITableView class]}){ // do something } else { // use UIScrollView logic}
  17. 17. Class checks in delegate methodsCode unmanagable over time - EverythingControllersCoding horror - iOS version of a GOTOCorrect Approach oniPadParcel out yourcontrollers
  18. 18. MANDATORY: ALWAYS write/model out your iOS app design before coding!At first, I was like… But then, it was like… (in your brain) (in the repo)
  19. 19. 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
  20. 20. What’s the optimal architecture fora Universal Application?
  21. 21. Optimal Universal App Architecture iPhone App iPad App UI Framework (Views & Controllers) Non-UI Framework (Networking & Models)
  22. 22. Photo Sharing Application Data from the model is in both the inspector and in the toolbar
  23. 23. MVC Structure Update Change Change Update
  24. 24. What are the six primary ways ofstoring data on iOS?
  25. 25. Six Model Options• Property Lists• Archives• Custom Files• Server/iCloud/APIs• SQLite• CoreData
  26. 26. According to Apple, what data shouldyou store in your AppDefaults/Preferences?
  27. 27. Don’t store data in settings!• Wrong tool for the job• App may get rejected• Settings Panel test • On/Off Advanced Features
  28. 28. What should you use for quick storageofstrings, numbers, arrays, dictionaries, ect?
  29. 29. Property Lists.
  30. 30. What should you use to store partialgraphs?
  31. 31. CoreData• Modeling Tools• Simple save/restore• Queries• Data Protection• Ordered Relationships• UIManagedDocument• Partial Graphs• Undo• Incremental Stores• ect…
  32. 32. What should you use to include datawith queue-based concurrency in yourapp?
  33. 33. CoreData again.
  34. 34. What should you use if you are dealingwith a lot of legacy code or data, or youneed to create an NSObject-basedgraph?
  35. 35. Custom Files.
  36. 36. When would you want to use a dataarchive?
  37. 37. For easily « serializing »and « deserializing »objects in a data file.
  38. 38. What are the two primary features ofSQLite?
  39. 39. Provides Database functionality for iOS appsSupports Object Relational Mapping
  40. 40. Know Your Data Model Options• Property Lists• Archives• Custom Files• Server/iCloud/APIs• SQLite• CoreData
  41. 41. 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
  42. 42. What is KVO?
  43. 43. Key-Value Observing (KVO) • Requires your code to be compliant • Follow style guide • Instrument your own notifications • Automatic Change Notifications for your objects

×