What New Is Added To Swift 4

Posted By Gunjan Gumber | 24-Sep-2017

Here below i have discussed some of the new things of swift 4 added to swift 3 existing features

Swift 4 is a part of XCode 9

** In swift 3 the swap(_:_:) method take two elements and swap them but the major drawback was
 the swapped elements are passed to the function as inout parameters but swift 4 has improved it

In swift 4:

var array = [a,b,c,d,e,f]
array.swapAt(1,2)
the resut would be: [a,c,b,d,e,f]

 

**Swift 4 has improved Dictionaries

In swift 4 we can create dictionary from an array
example:-

let array = [("Mon", 1),  ("Tue", 2),  ("Wed", 3),  ("Thu", 4),  ("Fri", 5),  ("Sat", 6),  ("Sun", 7)]
let dictionary = Dictionary(uniqueKeysWithValues: array)

If we have different arrays for the keys and values to begin with

let keys = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
let values = [1, 2, 3, 4, 5, 6, 7]
let newDictionary = Dictionary(uniqueKeysWithValues: zip(keys, values))

Swift 4 has special feature of checking the duplicate values

let array = [("Monday", 1),  ("Tuesday", 2),  ("Wednesday", 3), ("Monday", 4)]
let dictionary = Dictionary(array, uniquingKeysWith: min)

Like in the above example we have two values for same key(i.e Monday) and we want to choose the smallest one then we can use
 init(_: uniquingKeysWith:) initializer.

 

**Private vs Fileprivate

Swift 3 uses the fileprivate access control modifier

In swift 3:

class Person {
  fileprivate let name: String
  fileprivate let age: Int
  
 }

 

Swift 4 uses the private access control modifier

In Swift 4:

class Person {
  fileprivate let name: String
  fileprivate let age: Int
}

 

 

**NSNumber Improvements
NSNumber to an UInt8 typecast was not proper in Swift 3

let x = NSNumber(value: 1000)
let y = x as? UInt8

 

in this case, the value of y should be nil as  Uint8 type begins with 0 and goes up to 255 only.
and swift 3 calculates the value of y as -- 1000 % 256 = 232.
In Swift 4,value for y is nil

 

Thanks.

Request for Proposal

Recaptcha is required.

Sending message..