Hi iOS Developers,
I’m guessing we have all experienced the pain of spending a lot of time implementing a function, only to realize later that Swift has a built-in function that does the same thing. In this article, I will talk about a few useful protocols that can save you much time and take your code to the next level.
The CaseIterable protocol allows you to retrieve all the values of the type. Let’s take a look at an example:
Without CaseIterable:
enum City {
case new_york
case bei_jing
case vancouver
....
}let cities: [City] = [.new_york, .bei_jing, .vancouver…
Hi iOS Developers,
In one of my previous articles, I discussed some powerful protocols to take your code to the next level. Today, I’m going to tell you about some handy keywords to streamline your code. Please check out my previous article if you are interested, Long story short, let’s jump right into it.
fallthrough
In general, the switch
statement only executes one case. If we want to continue the execution to the next case, we can use the fallthrough
keyword. Here’s an example:
Let’s say we’re developing a weather app. We need to check the authorization status. …
Hi iOS Developers,
Swift has a lot of built-in data structures that allow the program to store a collection of data, such as Array
and Set
. These two are great, they are very easy to use and have a lot of useful built-in functions and properties. But they are not safe. The elements in Arrays
and Sets
are randomly accessible. Anyone can access any element at any time, which is insecure in some situations. Here’s where Stack
and Queue
come in to play.
Unfortunately, Swift doesn’t provide built-in data structures for Stack
and Queue
, so we need to do that…
Hi developers,
Reactive programming is getting more and more attention since Apple announced its Combine framework. Unfortunately, however, the Combine framework is only available for iOS 13.0+. In the meantime, we can still install the RxSwift framework. But the RxSwift framework is huge. It may increase the size of the app unnecessarily, especially if we are only using it to accomplish some simple tasks. Here’s when Key-Value Observing comes into play.
In this tutorial, we are going to build a reactive application using Key-Value Observing. …
Hi iOS Developers,
I talked about many types of keywords in my previous stories. Keywords, along with protocols and attributes, are powerful features that can streamline our code. But today, I’m going to talk about a particularly interesting group of keywords, those that begin with a number sign (#
), sometimes known as a hashtag. Some of these keywords may not immediately strike you as useful, but it’s worthwhile to know about them in case you ever have a need for them.
I’m going to assume that most of us are already familiar with #available
, #else
, #elseif
, #endif
, #if
and #selector
…
Hi iOS Developers,
In the previous stories, I talked about useful keywords and protocols. Today, I’m going to talk about the attributes in Swift that you must know if you want to advance your iOS development skills.
This attribute allows developers to create custom property wrappers with classes, structs, or enumerations. For example, we can create a property wrapper to always convert String
values to uppercase.
@propertyWrapper
struct Uppercased {
var wrappedValue: String {
didSet { wrappedValue = wrappedValue.uppercased() }
} init(wrappedValue: String) {
self.wrappedValue = wrappedValue.uppercased()
}
}
In order to make Uppercased
a property wrapper, it must have…
Building apps is hard, and it’s even harder to debug. Since Xcode 5.0, Apple introduced the XCTest framework, which allows developers to build test automation for apps. Unit Testing automates the testing process, catches errors before the apps run, and saves developers a lot of time. In this story, let’s talk about all the assertions that are available for writing Unit Tests.
This function generates success when the expression
is true
.
func testAssertion() {
let number = 2 XCTAssert(number > 1) // success
XCTAssert(number == 1) // failure
}
This function is very similar to the XCTAssert(expression: Bool)
, it generates…
Hi iOS Devs,
In general, switch
statements only execute one case. If we want to continue the execution to the next case, we can use the fallthrough
keyword. Today, I’ll talk about the most common used-cases of fallthrough
.
Let’s say we’re developing a weather app. We need to check the authorization status. Based on what we find out about status, here’s what we’ll do:
Hi iOS Developers,
Do you know what a “God Class” is? It’s a class that has tons of different categories of functions and variables in one huge file. Your apps likely have some “God Classes” in them and you didn’t even know it!. You still have no idea about what a “God Class” is? Are you aware that it could destroy your codebase? Continue reading and I’ll teach you how to kill these evil things.
It usually exists in the Controllers, if you are working with the MVC design pattern, and in the ViewModels, if you are working with the…
Hi iOS Developers,
Do you still remember the “Hello World” project you wrote? What was the first topic you learned for iOS development? Most people began their iOS development by declaring their first variable and function, which are the most fundamental and mandatory components of all the applications. Do you really know about variables? Let’s find out!
A variable is a data container that stores and/or retrieves data.
String
, Int
, Double
and Bool
, etc.:
var name = "Steve"
var age: Int = 35
var height: Double = 8.9
var isTall: Bool = {
return height > 7
}()
These are…
iOS Engineer, interested in Flutter. Nothing is better than solving complex problems with clean, elegant code. I have a passion for design and programming.