Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MapStack - lesson 8

leveton
March 11, 2016
40

MapStack - lesson 8

leveton

March 11, 2016
Tweet

Transcript

  1. Using predicates to filter collections Our settings controller allows us

    to pare locations down to a selected range. For this, we’ll use NSPredicates and sort descriptors - one of the more powerful Foundation API’s. But first we need to make a new model - an MSRange model - that will allow us to easily move ranges around the app.
  2. NSCopying Toggling our ranges back and forth, we need a

    reference to our original locations datasource. Being reference types you can’t just assign copiedDataSource = dataSource. Anything that happens to copiedDataSource will affect dataSource and vice versa because they share the same pointer. In order to copy objects entirely, with a new chunk of memory and a new pointer, we must use NSCopying. The object being copied must conform to the NSCopying protocol. Under the hood, copyWIthZone: is called on the object being copied.
  3. NSCopying You can make custom objects copyable by having them

    conform to NSCopying. Let’s see that with MSLocation: Now it can both act as an annotation on our map and be copied on the fly.
  4. The power of UITableView We’ve seen how much Apple bakes

    into UITableView with reusable cells, animated index paths, and delegates for all standard actions. Let’s use the moveRowAtIndexPath: and canMoveRowAtIndexPath: delegate methods to allow the user to reorder the set of location types. A little-used but powerful feature.
  5. The power of UITableView moveRowAtIndexPath and canMoveRowAtIndexPath are just delegate

    methods like cellForRowAtIndexPath or didSelectRowAtIndexPath Our settings view controller acts as a delegate for the cells being reordered. Certain events (like the reordering of cells) trigger these methods to be called. The view controller implements them
  6. The power of UITableView All this comes built into UITableView

    and UITableViewCell. No importing of third-party libraries and no having to write hundreds of lines of code if you were writing this app in the C you wrote in CS50.
  7. The MSLocation object is everywhere! Let’s review the advantages of

    objects: 1) We keep a mental model of the object across maps and tables. 2) We’re passing a 64 bit pointer around instead of a potentially huge chunk of memory. 3) The object is easily extendable.. What are other things we can do with our objects?
  8. .pch - prefix header Because they are everywhere, about 10

    different files import MSSingleton and MSLocation. Too much boilerplate! Create a ‘pch’ file that will be linked to every file in the project. You must do the following additional compiler setup..
  9. Instruments Just like Xcode is a huge leap from the

    CS50 IDE, Instruments takes Valgrind to another level There are the standard tools for profiling memory leaks and threading bugs but also tools for: • graphics usage • speed tests for Core Data fetches • a busy network layer • zombie objects • UI automated interactions (finger tapping, swiping, long-press) Much more
  10. Profiling hotspots with Instruments Let’s look at one of my

    favorite Instruments features - Color Blended Layers Color blended layers only works with a device. 1. Hold down the play button and choose ‘profile’ 2. Instruments will start up, choose Core Animation 3. On the right-hand side panel, choose ‘Display Settings’ 4. Check ‘color blended layers’ The phone should now look like this:
  11. Profiling hotspots with Instruments Views in green are opaque views

    that are not blended. Views in red are blended views where the graphics processor blends the view’s CALayer. Here, we see that our main and sublabels are opaque while the label property of our detail and delete buttons are blended.
  12. Profiling hotspots with Instruments In MSTableViewCell, find the initializer methods

    for our two buttons and walk the property path setting the button label’s opaque property to YES. The button’s color will now be black unless you set it so do that too. Profile the App’s Core Graphics processing one more time:
  13. Profiling hotspots with Instruments With opaque button labels, the GPU

    is having to do much less work. If this table were to have enough rows where scrolling reached 60 frames per second, changing the opacity of the buttons could really benefit the performance of our app.
  14. Instruments Graphics profiling is just one of hundreds of ways

    you can use Instruments to improve your app.
  15. NSLocalizedString and basic localization Notice throughout our app that all

    strings have been wrapped in an NSLocalizedString() function. The operating system will match the argument passed in with a key that you must provide. The keys are located in a .strings file that you must create. There is a .strings file for each language supported by the operating system.
  16. NSLocalizedString and basic localization Apple recommends that all apps, even

    those from indie devs, support English, Spanish, German, French, Portuguese, and Italian. These languages cover the majority of iOS users using the Latin alphabet. Localizing Asian languages is much more difficult from both a translation and user interface standpoint.
  17. Breakpoints, debug and terminal window XCode’s debugger offers the typical

    ‘step in’ and ‘step over’ functionality seen in the CS50 IDE. Besides the standard features, you can: • Setup a breakpoint to make a sound when it’s triggered. • Setup a breakpoint to send an email when it’s triggered. • Setup a breakpoint to be ignored unless a variable is true or method returns true.
  18. categories Notice we implemented verticallyCenteredFrameForChildFrame: in more than one class?

    We could put the method in a global constants file but that’s overkill for large projects that are only going to use the method once albeit disparagingly. Objective-C categories provide a powerful way to extend the functionality of Foundation and UIKit classes.
  19. categories Here are extend good old UIView so that any

    class that imports UIView+MSAdditions.h can call verticallyCenteredFrameForChildFrame: on any UIView or UIView subclass.
  20. Testing with XCTest iOS developers traditionally don’t test as often

    as web or server-side devs mainly because the front end compiler does static analysis for you. But there were a few other reasons that are no longer true: One screen size - now we have several. One language - now we have two. No legacy - 2007 is getting smaller in the rear-view mirror. Over time gigantic, aging codebases have come into being.
  21. Testing with XCTest Looking in MapStackTests.m we have to setUp

    and tearDown methods. Think of this as init and dealloc in a typical class implementation - allowing you to the same property in multiple tests.
  22. Testing with XCTest Our first two tests simply check for

    correct input and correct data type. Our third test contains one of XCTest’s best features measureBlock:
  23. Objective-C vs Swift Remember the MSRange model we had to

    create in order to conveniently store two floats? Have you ever wanted something that’s easy to copy and not referenced like a struct but smart and powerful like an object? Do you get tired of header and implementation files and feel a bit like you’re coding during the Reagan administration?
  24. Objective-C vs Swift Swift would’ve allowed us to return a

    tuple instead. Swift has much more powerful structs and enums which would’ve simplified your CS50 assignments. Swift accelerates the extinction of the header files in our industry. Apps built in modern languages have far fewer files with far fewer lines of code. Despite code snippets and thorough knowledge XCode shortcuts, my wrist hurts ;(
  25. Static and dynamic libraries .dylib (dynamic library) - linked at

    runtime and therefore may change and be recompiled. .a (static library) - linked at compile time and therefore doesn’t change during the runtime because the object code that makes up the binary is set.
  26. Frameworks .framework - combo of static and dynamic libraries along

    with header files and documentation. Useful when you have a suite of apps under one banner. Gmail, Google Docs, Google Maps, etc. all probably share a framework.
  27. Apple’s Human Interface Guidelines The HIG is the culmination of

    years of Apple elevating user interaction and user experience to the highest priority. Notice that MapStack uses Apple defaults whenever possible i.e. no third parties and few custom objects. You should dig a little to discover the amazing controls available to you at project creation. We didn’t make our map, rows, or buttons too small. In fact, many views have a minimum size that cannot be overridden without some serious hacks.
  28. Charles - Intercepts http traffic between your device and your

    webservice. Great for debugging web traffic. Paw - Build your http requests with different environments, auth tokens. Sketch - Adobe Illustrator replacement that’s geared toward Apple, costs about an order of magnitude less and weighs even less than that. Paintcode - Generates code for the shapes you draw on the GUI. Bezier path heaven. 3rd party tools