How to change which ViewController loads at startup in Storyboard

1 Comment

You can drag around the little arrow from one VC to the other. Or your can select the little checkbox in the menu titled ‘Is Initial View Controller’ near the top on the right hand side.

Screen Shot 2018-07-16 at 6.36.46 AM.png

Advertisements

When to use Optionals in Swift

Leave a comment

Being new to Optionals, sometimes I struggle for good examples on where and when to use them.

Here’s an example I link that show how one could use an Optional to model an AccountBalance.

struct AccountBalance {
    init(_ ledger: Money, overdraftLimit: Money? = nil) {
        self.ledger = ledger
        self.overdraftLimit = overdraftLimit
    }

    var available: Money {
        if let overdraftLimit = overdraftLimit {
            return ledger + overdraftLimit
        }
        return ledger
    }

    let ledger: Money
    let overdraftLimit: Money?
}

Both the ledger and overdraftLimit are let’s (meaning once assigned they can’t be changed). But the overdraftLimit is also an Optional because the overdraftLimit may or may not exist for this account.

I like this example because it shows how the overdraftLimit exists for some but not others. It truly is Optional. And for those who don’t have it we need some kind of check.

Building on that, here are the methods to increase/decrease the amounts of funds.

func increased(by amount: Money) -> AccountBalance {
    return AccountBalance(ledger + amount, overdraftLimit: overdraftLimit)
}

func decreased(by amount: Money) -> AccountBalance? {
    guard amount <= available else { return nil }
    return AccountBalance(ledger - amount, overdraftLimit: overdraftLimit)
}

Notice here how the AccountBalance in decreasedBy is also an Optional? This is because this operation could fail if the account does not have sufficient funds. This is a really good example of how to use a guard clause to see if something bad might happen, and then instead of throwing an error (i.e. InSufficientFunds) instead simply returning a nil when the client can guard and look for (which we will see in the next step).

We can bring it all together like this.

class Account {
init(identifier: AccountIdentifier, balance: AccountBalance) {
self.identifier = identifier
self.balance = balance
}

func credit(_ amount: Money) {
balance = balance.increased(by: amount)
}

func debit(_ amount: Money) throws {
guard let newBalance = balance.decreased(by: amount) else {
throw AccountError.insufficientBalance
}
balance = newBalance
}

let identifier: AccountIdentifier
private(set) var balance: AccountBalance
}

enum AccountError: Error {
case insufficientBalance
}

Here we can see how:

  • The AccountIdentifer can be a let, but a let that dynamically builds itself based on internal properties
  • The AccountBalance is a private(set) meaning no one can change its value save the AccountBalance itself
  • The AccountBalance continuously creates new instances of itself, always representing itself as a Value object (safer than a reference)
  • The debit method looks for that Optional if anything goes bad (much nicer than having to try catch and error). Here is simply looks for the nil (or Optional) being returned by protects itself with a guard clause at which point it decides to throw an Error

Really nice example showing a lot of the different constructs and how to use them in Swift.

Thanks to Khawer Khaliq for this great example.

How to draw icons illustrator

Leave a comment

Say you want to try an icon that looks something like this

Screen Shot 2018-06-30 at 6.48.17 AM.png

They first thing to realize is that this is just a series of geometric shapes – circles and lines.

So we can start by simply drawing a circle

Screen Shot 2018-06-30 at 7.19.15 AM.png

Cut it in half with scissors by clicking on the dots

Screen Shot 2018-06-30 at 7.20.24 AM.png

Round the caps (Window > Stroke)

Screen Shot 2018-06-30 at 7.20.56 AM.png
Screen Shot 2018-06-30 at 7.21.51 AM.png

With this you now have the brush in a good state. You can repeat this process with more circle and line tools to draw the rest of your icon.

Happy drawing!

How to add a custom UITableViewCell Swift

Leave a comment

Create a new UITableViewCell in Swift (with nib)

Screen Shot 2018-06-24 at 7.56.16 AM.png

Give it a unique identifier

Screen Shot 2018-06-24 at 7.57.14 AM.png

Load it up in your ViewController

Screen Shot 2018-06-24 at 7.58.01 AM.png

Then access it in your UITableView delegate methods like this

Screen Shot 2018-06-24 at 8.02.41 AM.png

Screen Shot 2018-06-24 at 8.02.47 AM.png

Give your cell a default height in the ViewController else you will get a warning

Screen Shot 2018-06-24 at 8.15.48 AM.png

How to hookup UITableView Swift

1 Comment

Screen Shot 2018-06-24 at 7.37.39 AM.png

Implement the protocols, get a reference to the tableView in the VC, and set yourself as the x2 delegates.

Then implement these x2 methodsScreen Shot 2018-06-24 at 7.38.50 AM.png

Give your prototype cell a reuse identifier

Screen Shot 2018-06-24 at 8.10.14 AM.png

What is a tuple

Leave a comment

A tuple is a immutable heterogenous data structure while Lists are homogenous sequences. Tuples have structure. Lists have order.

For example say we wanted to save notes in a book on a specific page an line number. We could define that data structure as a tuple.

my_location = (42, 11)   # page number, line number

And then use this as a key in a Dictionary to store notes on locations. A List on the other hand could be used to store multiple locations. Naturally one might want to add and remove from the list. That’s why Lists are mutable and tuples are immutable.

https://stackoverflow.com/questions/626759/whats-the-difference-between-lists-and-tuples

How to soft ambient hue adobe illustrator

1 Comment

Screen Shot 2018-03-06 at 8.52.08 AM.png

I got this nice ambient transparent hue on an image by doing the following:

White rounded corner square

Screen Shot 2018-03-06 at 8.54.13 AM.png

Transparency 80% (Window / Transparency)

Slide and apply over image. They select the ‘Hue’ option from the Transparent dialog.

Older Entries Newer Entries

%d bloggers like this: