Learn Swift – 5 Advanced Swift 3.0 Tips

0
Shares
Learn Swift – 5 Advanced Swift 3.0 Tips
4.7 (93.33%) 3 votes

Since Swift 1.0 release in September 2014, our iOS app development team has been experimenting with Swift programming language.

Swift is a multi-paradigm, compiled programming language that created for iOS, OS X, watchOS, tvOS, and Linux development by Apple Inc. Swift language is designed with Apple’s Cocoa and Cocoa Touch frameworks.

Swift adds language features that enable developers to code faster and safer. This language is more flexible than Objective-C and thus, we have had moved towards Swift.

Below are some of the advanced Apple Swift app development tips that we have learned throughout our experience. These tips also help our developers to learn Swift.

Old Tradition Addition way accepted in Swift 3

var i = 0
i += 1

Old Tradition Subtraction way accepted in Swift 3

var j = 0
j -= 1

Various types of for-loops in Swift 3

1 – Simple for-in loop

for i in 1...10 {
print(i)
}

2 – Simple for-each loop

(1...10).forEach {
print($0)
}

var temp = [5, 6, 7, 8, 9, 10]
for val in temp{
print(val)
}

3 – For-in reverse loops

for index in stride(from: 5, to: 1, by: -1) {
print(index)
}
//prints 5, 4, 3, 2

for index in stride(from: 5, through: 1, by: -1) {
print(index)
}
//prints 5, 4, 3, 2, 1

for (index, value) in temp.enumerated(){
print("Index \(index) & value \(value)")
}

for index in (0..<temp.count).reversed(){
print(temp[index])
}

Various Ways to define Functions in Swift 3.0

1 – Simplest and most common way

func demo(_ a: Int, b: Int) -> Int{
return a + b
}

2 – Function with mutating variable

func gcdDemo (a: Int) -> Int{
let b = a
return b + 5
}

3 – New Way to access Constants in Swift 3.0

//PI = 3.14
Double.pi
Float.pi
CGFloat.pi

4 – Getting File Path from the Project Folder

let file = Bundle.main.path(forResource: "tutorials", ofType: "json")
let url = URL(fileURLWithPath: file!)
let data = try! Data(contentsOf: url)
let json = try! JSONSerialization.jsonObject(with: data)
print(json)

Types of Queue’s

1 – Define Serial Queue

//Define Serial Queue
let queue = DispatchQueue(label: "Swift 3")
queue.async {
print("Swift 3 queue")
}

2 – Define ConcurrentQueue

let concurrentQueue = DispatchQueue(label: "queuename", attributes: .concurrent)
concurrentQueue.sync {
}

3 – Define Main Thread queue async

DispatchQueue.main.async {

}

4 – Define Global Queue

DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {

}

DispatchQueue.global().async {
// qos' default value is ´DispatchQoS.QoSClass.default`
}

Array properties

1 – Array Ascending Sort

var numbers = [5, 6, 7, 8, 9, 10]
numbers.sort()

2 – Array Ascending Sort with Return

let arrNew = temp.sorted()

3 – Array Conditional Sort

temp.sort { (int1, int2) -> Bool in
//UserCreationTime
if int1 > int2{
return true
}

//User Updation Time
else if int1 < int2{
return false
}
return false
}

Hope, you have learned Swift tips through this article, and will implement in your project for quick development. We are excited to use Swift language more and share our personal experience through the swift tutorial.

If you’ve any questions or want to hire Swift developer from us on hourly and project basis, do ask us. We will answer any questions you have related to Swift development.

 
0
Shares
 

Have an App Idea?

Get your free consultation now