Sample Code Summer 2020

To showcase my current programming style I’ve started a sample iOS project. It is an Books-alike that will be able to access a collection of books from a JSON file and then be able to download and read them.

These are the features it will have once it is feature-complete:

  •  Create Project without Storyboard
  •  Create JSON file format & Codeable Model
  •  Make Network Request
  •  Layout Header with info from JSON file
  •  Show temp data in Collection View
  •  Create Constants for any Design values
  •  Make network request for cover images concurrently
  •  Create cache for cover images
  •  Write Unit tests for ViewModel
  •  Write UI tests
  •  Download PDFs from network to local storage
  •  Open PDF in PDFView from Collection View
  •  Manage Cached Books on disk

Searching an NSArray of NSDictionaries

NSPredicate is the simplest way to do it.

Given Data like this:

NSArray *theArray = @[@{@"id":1,"name":@"one"},@{@"id":2,"name":@"two"},@{@"id":3,"name":@"three"}];
NSPredicate *p = [NSPredicate predicateWithFormat:@"id = 291"]; //fiber
NSArray *matchedDicts = [theArray filteredArrayUsingPredicate:p];

There is much more to NSPredicate. I used exact matching, but matching with BEGINSWITH, CONTAINS, ENDSWITH, and LIKE. In some examples you’ll also see [c] or [cd] next to these keywords. The ‘c’ means it searches case insensitively and the ‘d’ means that an ‘o’ with an umlaut is still just an ‘o’.

Asterix Options

NSString *asterixOnVar;
NSString * asterixInAir;
NSString* asterixOnTypdef;

They all declare that it is a pointer to an NSString. There is no difference except readability. I prefer the first version because it shows the type and it is clear that the variable is a pointer.

Sorting Segues Sanely

There are three kinds of segues, push, modal and custom. You’ll probably most often use the ‘push’ method. It is incredibly useful in making storyboards do stuff.

Primarily there are two ways to invoke a segue. One is to rig up a button in Interface Builder that triggers a segue. The other is to invoke the segue on your own. I think that a lot of times I need to validate what’s on the screen before I can release it to the next controller. Therefore I need to do it myself.

Also you need to pass information back and forth, which will be taken care of by setting variables in the method:

-(void) prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender {

Continue reading