11 tips to become a better Swift (iOS) Developer

Vineet Choudhary
developerinsider
Published in
4 min readDec 24, 2017

This article was first published on DeveloperInsider. You can take a look at it here.

Swift is friendly to new programmers. It’s an industrial-quality programming language that’s as expressive and enjoyable as a scripting language. Here the some tips to become a better Swift Developer. You can copy and paste the code snippets into Playground, to make it easier for you to understand -

1. For loop with Half-open range

The half-open range operator (a ..< b) defines a range that runs from a to b, but doesn’t include b. The half-open range operator also has a one-sided form that’s written with only its final value.

1.1 With Half-open range operator (or 😎 Code)

let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names[..<2] {
print(name)
}
/* prints -
Anna
Alex */
for name in names[1..<3] {
print(name)
}
/* prints -
Alex
Brian
*/

1.2 Without Half-open range operator (or 💩 Code)

let names = ["Anna", "Alex", "Brian", "Jack"]
for (index, name) in names.enumerated(){
if index < 2 {
print(name)
}
}
/* prints -
Anna
Alex */

or with while loop (still 💩 Code)

let names = ["Anna", "Alex", "Brian", "Jack"]
var index = 0
while index < 2 {
print(names[index])
index += 1
}
/* prints -
Anna
Alex */

2. For loop with Closed range

The closed range operator (a…b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b. The closed range operator has an alternative form for ranges that continue as far as possible in one direction.

2.1 With Closed range operator (or ❤️ Code)

let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names[...2] {
print(name)
}
/* prints -
Anna
Alex
Brian
*/
for name in names[1...2] {
print(name)
}
/* prints -
Alex
Brian
*/

2.2 Without Closed range operator (or 💩 Code)

let names = ["Anna", "Alex", "Brian", "Jack"]
for (index, name) in names.enumerated(){
if index >= 1 && index <= 2 {
print(name)
}
}
/* prints -
Alex
Brian
*/

3. Subscripts

Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the member elements of a collection, list, or sequence. You use subscripts to set and retrieve values by index without needing separate methods for setting and retrieval. For example, you access elements in an Arrayinstance as someArray[index].

The following example defines a Matrix structure, which represents a two-dimensional matrix of Doublevalues with and without subscripts :

3.1 Without Subscripts (or Okay Version)

struct Matrix {
let rows: Int, columns: Int
var grid: [Double]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
self.grid = Array(repeatElement(0.0, count: rows * columns))
}

func getValue(row: Int, column: Int) -> Double{
return grid[(row * columns) + column]
}

mutating func setValue(row: Int, column: Int, value: Double){
grid[(row * columns) + column] = value
}
}
var matrix = Matrix(rows: 2, columns: 2)
matrix.setValue(row: 0, column: 0, value: 1.0)
matrix.setValue(row: 0, column: 1, value: 2.0)
matrix.setValue(row: 1, column: 0, value: 3.0)
matrix.setValue(row: 1, column: 1, value: 4.0)
print(matrix.getValue(row: 0, column: 0)) //prints "1.0"
print(matrix.getValue(row: 0, column: 1)) //prints "2.0"
print(matrix.getValue(row: 1, column: 0)) //prints "3.0"
print(matrix.getValue(row: 1, column: 1)) //prints "4.0"

3.2 With Subscripts (or Better Version)

struct Matrix {
let rows: Int, columns: Int
var grid: [Double]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
self.grid = Array(repeatElement(0.0, count: rows * columns))
}
subscript(row: Int, column: Int) -> Double {
get {
return grid[(row * columns) + column]
}
set {
grid[(row * columns) + column] = newValue
}
}
}
var matrix = Matrix(rows: 2, columns: 2)matrix[0,0] = 1.0
matrix[0,1] = 2.0
matrix[1,0] = 3.0
matrix[1,1] = 4.0
print(matrix[0,0]) //prints "1.0"
print(matrix[0,1]) //prints "2.0"
print(matrix[1,0]) //prints "3.0"
print(matrix[1,1]) //prints "4.0"

4. Function vs Computed Property

4.1 Function (or 💩 Code)

As you know, functions are self-contained chunks of code that perform a specific task. Here’s an example of a function implementation, this example find the diameter of a circle from circle radius or visa-versa :

func getDiameter(radius: Double) -> Double {
return radius * 2
}
func getRadius(diameter: Double) -> Double {
return diameter / 2
}
print(getDiameter(radius: 100)) //prints "200"
print(getRadius(diameter: 100)) //prints "50"

4.2 Computed Property (or ❤️ Code)

Computed property provide a getter and an optional setter to retrieve and set other properties and values indirectly. Computed properties do not actually store a value. Here’s the same example of with computed property, this example find the diameter of a circle from circle radius or visa-versa :

var radius: Double = 100
var diameter: Double {
get {
return radius * 2
}
set {
radius = newValue / 2
}
}
print(diameter) //prints "200.0"diameter = 100
print(radius) //prints "50.0"

This article was first published on DeveloperInsider. If you wish to finish reading this article about 11 tips to become better swift developer, feel free to visit here.

Check out AppBox. AppBox is tool which build and deploy Development, Ad-Hoc and In-house (Enterprise) iOS applications directly to the devices from your Dropbox account. AppBox is a opensource software available on github https://github.com/vineetchoudhary/AppBox-iOSAppsWirelessInstallation .

--

--

Vineet Choudhary
developerinsider

iOS, macOS and Xamarin Developer | Creator of @DevsInsider amd @AppBoxHQ