I'm an iOS engineer and a ninja as well.

0%

The release of Swift 5.1 brought some useful polish to the language with minimal source breaking changes. Some of the bigger improvements like property wrappers and ordered collection diffing take some time to get your head around. Luckily there are also some quick hits that you can start using today.

Read more »

One of Swift’s greatest features, and one of my favorites, are enums with associated values. The language itself uses them for its fundamentals, like Optional<T>, which either has a .some(T) or is .none. Another example is the new since Swift 5 Result<T, E>, which either contains a .success(T) or a .failure(E) case. In this post, we will go over cases (no pun intended) where an enum is more suitable than a struct or class, and also learn how one can make enums with associated values conform to Codable, achieving a better and safer usage of these data representations when they need to be encoded and decoded. You can scroll to the end of the post to get the final playground.

Enums with associated values make sense when a type may hold only one value, instead of two or more optional values. A classic example for Result is a network operation, which might return either an error or an object. They never should be nil or not be nil simultaneously: when one is nil, the other should exist.

1
2
3
4
5
//in this case, the caller must check for nil for both values
func issueRequest<T>(_ request: URLRequest, completion: @escaping (T?, Error?) -> Void)

//here, however, it will be either .success or .failure
func issueRequest<T>(_ request: URLRequest, completion: @escaping (Result<T, Error>) -> Void)
Read more »

Enums are one of the most powerful features of Swift language. It forms Value-Oriented Programming in conjunction with Structs. Enum is the best way to describe the exclusive state in Swift, but what about the case when you need an inclusive state. Today we will talk about OptionSet protocol and how we can achieve inclusive states with it.

Exclusive Enums

Assume that we have some HistoryFetcher class, which can fetch data from a cache or make a network request or both of them. Let’s start with describing very simple source enum.

1
2
3
4
5
6
7
enum FetchSource {
case memory
case disk
case remote
case cache
case all
}
Read more »

How do you tell if a string is empty in Swift? That depends on what you mean by “empty”. You might mean a string with zero length, or maybe also an optional string that is nil. What about a “blank” string that only contains whitespace. Let’s see how to test for each of those conditions with Swift.

Read more »

We usually use the class keyword to define a class only protocol in the normal way.

1
2
3
protocol DetailViewControllerDelegate: class {
func didFinishTask(sender: DetailViewController)
}
Read more »

You are using third-party tools like CocoaPods, Cathage, SPM or just having some useful script files for your project. It’s fine!

Read more »

For the longest time I just used “po” in lldb to examine objects but “e” just adds a whole new level to debugging !

Read more »

⌃⌥⌘U shortcut does different things depending on the current cursor location:

  • inside a test method: runs this single test method
  • inside an XCTestCase subclass but outside of any method: runs all tests in the class
  • Run last test case before: ⌃⌥⌘G
    Read more »

Passing data from one view controller to another view controller using Notification Center is an easy way when compared to delegate protocols.

Here we need add observer or listeners for getting new data to load. First we need to send data using post notification method.

Read more »