Stuart Breckenridge

Downloading the WWDC 2017 Sample Code

Downloading all the sample code from WWDC is a fairly tedious task. However, Johannes Fahrenkrug has created a handy RubyGem to download all the sample code for you. The code is available on GitHub and the gem is really easy to use.

It should be noted that all the sample code comes to 393.5 MB.


— Supported by —


The Correct Way to Initialise a UITableViewCell

Below I present four options for initialising a UITableViewCell. Of these options, I tend to use the second option (or a close variant) in production code, and for brevity, option 4 in sample code.

What do you prefer? Are there other, safer options available?

Option 1:

var cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil {
    cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
}
return cell

Option 2:

guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) else {
    let newCell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
    return newCell
}
return cell

Option 3:

if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) {
    return cell
} else {
    let newCell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
    return newCell
}

Option 4:

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
return cell

The Case for Keeping Touch ID

As a result of Ming-Chi Kuo’s research note, there’s been quite a bit of interesting discussion about the iPhone 8 dropping Touch ID in favour of using facial recognition technology.

Benjamin Mayo, at 9to5Mac:

KGI’s Ming-Chi Kuo has sent out a new report with his top predictions for the 2017 iPhone lineup: two iterative updates to the iPhone 7 and iPhone 7 Plus, as well as the all-new OLED iPhone 8. The analyst believes that the iPhone 8 will feature the highest screen-to-body ratio of any phone on the market, thanks to the addition of the ‘notch’ at the top of the screen and a virtual home button.

However, KGI dampens spirits by stating that the virtual home button will not support fingerprint recognition, and that the OLED iPhone will not include a Touch ID sensor of any kind …

For several practical reasons, I don’t think Apple will remove Touch ID from any of the 2017 iPhones:

  1. How would you unlock your iPhone when you are not looking at it?
  2. In low-light scenarios, e.g. at the cinema or a bar, would your only option be to use a passcode?
  3. If you wear any headwear, e.g. a niqab, burka, balaclava, or perhaps even sunglasses, how would Face-to-Unlock work?

It doesn’t make sense to remove Touch ID and replace it with something that is sub-optimal by comparison. My guess — well, hope — is that if Face-to-Unlock is implemented, it will be done so in an additive fashion and not in a way where Touch ID is consigned to the scrap heap.


Simple JSON Parsing with Codable

As I rewrite Amazing Flag Quiz for iOS 11, one of the greatest things I’ve come across is the new Codable protocol in Swift 4. Before I get into why it’s great, here’s a bit of background: Amazing Flag Quiz v1 to v2.1.2 (current) contains an XML file of ids, country names (short and long, I don’t know why!), continent names, flag image names, and population (I don’t know why!). That file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<countries>
  <country>
    <ID>0</ID>
    <shortCountryName>Afghanistan</shortCountryName>
    <longCountryName>Afghanistan</longCountryName>
    <countryFlag>Afghanistan.png</countryFlag>
    <countryPopulation>25500100</countryPopulation>
    <countryContinent>Asia</countryContinent>
  </country>
  <country>
    <ID>1</ID>
    <shortCountryName>Aland</shortCountryName>
    <longCountryName>'c5land Islands</longCountryName>
    <countryFlag>Aland.png</countryFlag>
    <countryPopulation>28355</countryPopulation>
    <countryContinent>Don't Use</countryContinent>
  </country>
  ...
</countries>

To use this file meant creating an NSXMLParser and all the code that goes along with it. For version 3 of Amazing Flag Quiz, I’ve decided to convert the XML to JSON and use a Country struct that conforms to Decodable.

The tidied up JSON file looks like this:

[
  {
  "name": "Afghanistan",
  "game": "Asia",
  "excludeFromWorldChallenge": false,
  "flag": "AF.png"
  },
  {
  "name": "Albania",
  "game": "Europe",
  "excludeFromWorldChallenge": false,
  "flag": "AL.png"
  }, 
  ...
]

The Country struct mirrors the data set:

/// Representation of data as held in the countryFlags.json file.
struct Country: Decodable {
    var name: String
    var flag: String
    var game: String
    var excludeFromWorldChallenge: Bool
}

I then have a single function to extract a Country array for the game type the player picks:

func flags(for gameType: GameType?) -> [Country] {
    
    let decoder = JSONDecoder()
    let data = try? Data(contentsOf: file!)
    let decodedData = try? decoder.decode([Country].self, from: data!)
    
    guard let countries = decodedData else {
        return []
    }
    
    if gameType == nil {
        return countries
    }
    
    if gameType == GameType.world {
        return GKRandomSource.sharedRandom().arrayByShufflingObjects(in: countries.filter({ $0.excludeFromWorldChallenge == false })) as! [Country]
    }
    
    return  GKRandomSource.sharedRandom().arrayByShufflingObjects(in: countries.filter({ $0.game == gameType!.rawValue })) as! [Country]

}

If a player selected the Asia game type:

let gameArray = flags(for: .asia) // returns a shuffled array of Asian countries.

Codable has made JSON parsing so simple. I’d recommend the Ulimate Guide to JSON Parsing With Swift 4 by Ben Scheirman as further reading.


Happy Birthday, iPhone

There’s only been one iPhone generation that I wasn’t part of: the first. In the UK the iPhone was only available on o2 and I was with T-Mobile. That fact, coupled with my contract renewal time-frame, meant that I upgraded to a different phone: LG Viewty.

While the rest of the world wishes the iPhone a happy birthday, I thought I’d jot down some thoughts about the LG Viewty.

The front of the phone was OK, while the back of the phone was hideous: the camera lens was huge but, to be fair, it did take really good pictures. It also had 120fps video and direct upload to YouTube (in 2007). The camera, in retrospect, was way ahead of its time.

The back panel of the phone was removable. The reason I know this was that mine was faulty and kept falling off. The whole phone had to be sent for repair for three weeks and during this time I was given a loaner feature phone with capacity for 100 text messages.

Anyway, while the Viewty’s operating system was LG’s, the user interface was an Adobe Flash variant…so it was unresponsive and buggy. The Viewty also came with a stylus and I used it maybe three times. Tapping buttons provided haptic feedback, but because it was a resistance based touchscreen you had to put in effort, and have patience, before said feedback was felt and your tap was registered.

After about three months I gave up with the Viewty and sold it on eBay. For the remainder of my contract I used a Sony Ericsson W580 that I bought in a rage from Argos.

I’m glad the iPhone 3G came along when it did.