<![CDATA[Bruno Delgado]]>https://brunodelgado.github.iohttps://brunodelgado.github.iofavicon.pngBruno Delgadohttps://brunodelgado.github.ioGhost 3.14Fri, 08 May 2020 03:31:28 GMT60<![CDATA[Small Swift Tips #02: Using reserved keywords]]>The SmallSwiftTips on this post will show you how you can use some system reserved keywords on your code, if you ever need.

A very common example can be founded when declaring enum values. Let's imagine we have an enum and we want to have a case named "default". This

]]>
https://brunodelgado.github.iosmallswifttips-02-using-reserved-keywords/5eb4cdfe2268b00e2c238b34Fri, 08 May 2020 03:18:47 GMTThe SmallSwiftTips on this post will show you how you can use some system reserved keywords on your code, if you ever need.

A very common example can be founded when declaring enum values. Let's imagine we have an enum and we want to have a case named "default". This is a reserved keyword so if you just try to use it, you'’l notice you can't.


Using the name between `` you'll be able to use the reserved keyword.

enum LayoutMode {
    case `default`
    case expanded
    case detailed
}

There are a few reserved keywords that you might want to use on your code like associativity, convenience, dynamic, final, lazy, left, mutating, none, optional, override, postfix, precedence, prefix, required, right, weak.

With this tip you can use them without problems and keep the consistency on your code when naming variables, enums and so on. 😁🚀


]]>
<![CDATA[Small Swift Tips #01: Grouping elements using group:by:]]>For the very first SmallSwiftTips I will start with a very handy new Dictionary init that is init(grouping:by:)

This new init was introduced on Swift 4 and it helps creating a new grouped Dictionary. It takes a Sequence and a closure as input.

let frameworks = ["UIKit", "CoreMedia", "Security"
]]>
https://brunodelgado.github.iosmall-swift-tips-01/5eb473782268b00e2c238b06Fri, 08 May 2020 03:18:39 GMTFor the very first SmallSwiftTips I will start with a very handy new Dictionary init that is init(grouping:by:)

This new init was introduced on Swift 4 and it helps creating a new grouped Dictionary. It takes a Sequence and a closure as input.

let frameworks = ["UIKit", "CoreMedia", "Security", "MapKit", "StoreKit"]
let frameworksFirstLetters = Dictionary(grouping: frameworks, by: { $0.first! })
//["U": ["UIKit"], "M": ["MapKit"], "C": ["CoreMedia"], "S": ["Security", "StoreKit"]]

let frameworksWithCoreOnName = Dictionary(grouping: frameworks, by: { $0.contains("Core") })
//[false: ["UIKit", "Security", "MapKit", "StoreKit"], true: ["CoreMedia"]]

Note that the key for this new Dictionary will be the result from the closure. In the first example, the closure's result is a Character, so we'll have a new [Character: [String]] Dictionary. On the second example we have a Bool value in the closure's result, giving us a new [Bool: [String]].

You can use a sequence of custom objects as well.

enum AssetType {
    case image
    case video
}

struct Asset {
    let type: AssetType
    let name: String
}

let assets = [
    Asset(type: .image, name: "icon"),
    Asset(type: .video, name: "apple-watch-intro-video"),
    Asset(type: .image, name: "new-footer")
]

let group = Dictionary(grouping: assets, by: { $0.type })
//[AssetType.image: [Asset, Asset], AssetType.video: [Asset]]

This code will return a new [AssetType: [Asset]] which is really handy. This can be very helpful and I hope you liked and use it. 😁🚀


]]>
<![CDATA[Introducing Small Swift Tips]]>One of my new year’s resolution for 2019 2020 was to create the habit of blogging. Ok, I’m a little late as we are nearly half year, but hey, it’s never too late.

To start creating this habit I thought it’d be easier to start with

]]>
https://brunodelgado.github.iointroducing-small-swift-tips/5eb470822268b00e2c238ad6Fri, 08 May 2020 03:18:31 GMT

One of my new year’s resolution for 2019 2020 was to create the habit of blogging. Ok, I’m a little late as we are nearly half year, but hey, it’s never too late.

To start creating this habit I thought it’d be easier to start with small posts and today I’m starting this idea with a series I called  which is pretty explanatory, tiny swift tips to help new developers. 🚀


]]>