Torus tech co



A guide for a swift developer beginner - Part 1

Written by Mathivathani Murugesan and Prasad NRV - 24 Sep 2017 3 minutes read

Type less. Read less. Produce more.

Swift is a general-purpose, multi-paradigm and compiled programming language developed by Apple Inc. for iOS, macOS, watchOS, tvOS, and Linux. It is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of extant Objective-C (ObjC) code written for Apple products.

Swift is built with the open source LLVM compiler framework and has been included in Xcode since version 6. On platforms other than Linux, it uses the Objective-C runtime library which allows C, Objective-C, C++ and Swift code to run within one program.

Some key features of Swift includes:

1. Swift makes use of safe programming patterns.
2. Swift provides modern programming features.
3. Swift provides Objective-C like syntax.
4. Swift is a fantastic way to write iOS and OS X apps.
5. Swift provides seamless access to existing Cocoa frameworks.
6. Swift unifies the procedural and object-oriented portions of the language.
7. Swift does not need a separate library import to support functionalities like input/output or string handling.

If you have just begun your Swift journey, here are some tips useful that will help improve your basic standards in coding:

1. Extension:

Ex) Square a number

// Okay Version 
func square(x: Int) -> Int { return x * x } 
var squaredOFFive = square(x: 5) 
square(x:squaredOFFive) // 625

The useless variable was created to double square 5— we need not enjoy typing.

// Better Version
extension Int {
var squared: Int { return self * self }
5.squared // 25
5.squared.squared // 625

2. Generics:

Ex) Print all elements in an array:

// Bad Code
var stringArray = ["Twitter", "Facebook", "Instagram"]
var intArray = [1, 3, 4, 5, 6]
var doubleArray = [1.0, 2.0, 3.0]
func printStringArray(a: [String]) { for s in a { print(s) } }
func printIntArray(a: [Int]) { for i in a { print(i) } }
func printDoubleArray(a: [Double]) {for d in a { print(d) } }

Too many useless functions. Let’s create just one.

// Awesome Code!
func printElementFromArray(a: [T]) {
for element in a { print(element) }

3. For Loop vs While Loop:

Ex) Print “Count” 5 times

// Okay Code
var i = 0
while 5 > i {
i += 1 }

You made the variable “i” to make sure your computer doesn’t break by printing limited numbers. Remember, more variables = more memorization = more headache = more bugs = more life problems.

// Better Code
for _ in 1...5 { print("Count") }

4. Optional Unwrapping:

Ex) Guard let vs if let
Let’s make a program for welcoming a new user.

// Okay code
var myUsername: Double?
var myPassword: Double?

// Hideous Code
func userLogIn() {
if let username = myUsername {
if let password = myPassword {
print("Welcome, \(username)"!) } } }

// Pretty Code
func userLogIn()
guard let username = myUsername, let password = myPassword
{ return }
print("Welcome, \(username)!") }

The difference is tremendous. If username or password has a nil value, the pretty code will early-exit the function by calling “return”. If not, it will print the welcoming message. No Pyramid.

5. Computed Property vs Function:

Ex) finding a diameter of a circle

// Okay Code
func getDiameter(radius: Double) -> Double { return radius * 2}
func getRadius(diameter: Double) -> Double { return diameter / 2}
getDiameter(radius: 5) // return 10
getRadius(diameter: 500) // return 250
getRadius(diameter: 1000) // return 500

You created two mutually exclusive functions. Outrageous! Let’s connect the dot between radius and diameter.

// Good Code
var radius: Double = 10
var diameter: Double {
get { return radius * 2}
set { radius = newValue / 2} }
radius // 10
diameter // 20
diameter = 1000
radius // 500

Now, the radius and diameter variables are interdependent of each other. More connections = less extra typing = fewer typos = fewer bugs = fewer life problems.

These are some basic tips to become a better Swift developer. We hope this makes coding more easier and swift, with fewer bugs and more productivity.

Do continue to read more tips for Swift development in Part 2 of this post!

Digital Marketing | Cloud Services | Mobile  App Development  | Web development


For any of your Digital Marketing, Cloud Services, Mobile & Web development requirements, please get in touch with us on or contact us on US (+1 414-491-6804) India (+91 9600577018).


More blog posts

GDRP, Torus Tech, TTC

GDPR 101 for Businesses

Written by Sundaresan Sekar - 25 May 2018 4 minutes read

GDPR By now, you must be receiving a flurry of emails from your favorite apps and tools about the updates in their privacy policy and terms and condit...

Mobile, Application, Torus, Torustech,

10 ways to add Marketing into your Mobile App

Written by Sundaresan Sekar - 13 Apr 2018 7 minutes read

Back in the non-digital era, the marketing teams would need to wait for the Android/iOS application development team to complete the products and hand...