IOS Development in Swift #4

Against my better judgement, I went ahead and watched the next lecture to see if I could handle it. It was a bit much. It was straight Swift syntax and programming concepts for an hour and 15 minutes. I would hold off on watching this one; I’ll switch over to a different resource for now, to get some more real experience like what we did with CodeWithChris. I’ll still review a little bit of what Mr. Hegarty covered in the lecture now.

Lecture 4: More Swift and Foundation Frameworks

Mr. Hegarty reveals an interesting aspect of the nature of optionals and unwrapping. The optional is a generic enum (it takes any type T as an input) that returns nil or some object of T. Unwrapping is a switch that either assigns an unwrapped variable to a new variable, or raises an exception if it can’t be unwrapped.

An important part of this lecture which I found relevant to my current purposes was the idea of data structures. The three basic building blocks are classes, structs, and enums (structs being things we know as arrays and dictionaries). The similarities between the three are their declaration syntax; use of properties and functions (no stored values in enum); and initializers (not enum). Their differences include inheritance (only class); introspection and casting (only class, to have an enum or struct that modifies itself, include the keyword mutating); value vs. reference (struct, enum vs. class). This idea was touched upon before but to stress it: when passing by value, values are copied when set as a parameter or assigned to a different variable; passing by reference stores data in the heap and accessed upon each calling.

Hegarty talked extensively about the rules of initializers. Most of it was for advanced coding so was not too relevant, except now I know if I get an error message about initializing, that I should check back to this video.

Useful Methods

Array methods:

+=[T]

first -> T?

last -> T?

append(T)

insert(T, atIndex: Int)

splice(Array<T>, atIndex: Int)

removeAtIndex(Int)

removeRange(Range)

replaceRange(Range, [T])

sort(isOrderedBefore: (T, T) -> Bool) // sorts elements e.g. a.sort { $0 < $1 }

filter(includeElement: (T) -> Bool) -> [T] // filters out undesirable elements e.g. a.filter { $0 < 3 }

map(transform: (T) -> U) -> [U] // transforms elements to different type e.g. a.map { “\($0)” }

reduce(initial: U, combine: (U, T) -> U) -> [U] // reduce elements to one value e.g. a.reduce(0) { $0 + $1 }

String methods (NOTE: Strings are indexed by String.index because a given glyph might be represented by multiple unicode characters so Strings can’t be indexed by Int):

advance(String.Index, Int) // e.g.

// var s = “hello”

// let index = advance(s.startIndex, 2)

// s.splice(“abc”, index)

// let startIndex = advance(s.startIndex, 1)

// let endIndex = advance(s.startIndex, 6)

// let substring = s[index..<endIndex]

The lecture slides on iTunesU have all of these methods and more. Here’s an article that also talks more about some of these higher order functions. 


At the end of my free CodeWithChris trial, I was a bit unsatisfied with the War game we created; it wasn’t how the game actually worked. The actual game doesn’t work off of points, what happens is that the winner of a round gets both cards, and whoever ends up with all the cards wins. So I decided to expand the functionality of the War game and also practice what we learned from lecture 3 to create a WarBrain.

I’ve attached my rendition of how I accomplished this below–it might not be the most economic code but it definitely got the job done. Try this out for yourself before looking at what I did. The finished result took me a couple hours, but it was great practice. The goal is that you want to:

  1. Create a model with a data structure to hold all the cards
  2. Create a method to randomly distribute the cards to each player
  3. Evaluate each round by adding the losing card to the winner’s stack
  4. Declare a winner when one player’s stack becomes empty

Screen Shot 2015-09-21 at 21.51.32Click image for fullscreen.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s