Swift Language Type casting in Swift Language


Example


Type Casting

Type casting is a way to check the type of an instance, or to treat that instance as a different superclass or subclass from somewhere else in its own class hierarchy.

Type casting in Swift is implemented with the is and as operators. These two operators provide a simple and expressive way to check the type of a value or cast a value to a different type.


Downcasting

A constant or variable of a certain class type may actually refer to an instance of a subclass behind the scenes. Where you believe this is the case, you can try to downcast to the subclass type with a type cast operator (as? or as!).

Because downcasting can fail, the type cast operator comes in two different forms. The conditional form, as?, returns an optional value of the type you are trying to downcast to. The forced form, as!, attempts the downcast and force-unwraps the result as a single compound action.

Use the conditional form of the type cast operator (as?) when you are not sure if the downcast will succeed. This form of the operator will always return an optional value, and the value will be nil if the downcast was not possible. This enables you to check for a successful downcast.

Use the forced form of the type cast operator (as!) only when you are sure that the downcast will always succeed. This form of the operator will trigger a runtime error if you try to downcast to an incorrect class type. Know more.


String to Int & Float conversion : -

     let numbers = "888.00"
     let intValue = NSString(string: numbers).integerValue
     print(intValue) // Output - 888

     
     let numbers = "888.00"
     let floatValue = NSString(string: numbers).floatValue
     print(floatValue) // Output : 888.0

Float to String Conversion

    let numbers = 888.00
    let floatValue = String(numbers) 
    print(floatValue) // Output : 888.0

    // Get Float value at particular decimal point 
    let numbers = 888.00
    let floatValue = String(format: "%.2f", numbers) // Here %.2f will give 2 numbers after decimal points we can use as per our need
    print(floatValue) // Output : "888.00"

Integer to String value

    let numbers = 888
    let intValue = String(numbers)
    print(intValue) // Output : "888"

Float to String value

    let numbers = 888.00
    let floatValue = String(numbers)
    print(floatValue)

Optional Float value to String

    let numbers: Any = 888.00
    let floatValue = String(describing: numbers)
    print(floatValue) // Output : 888.0

Optional String to Int value

    let hitCount = "100"
    let data :AnyObject = hitCount
    let score = Int(data as? String ?? "") ?? 0
    print(score)

Downcasting values from JSON

    let json = ["name" : "john", "subjects": ["Maths", "Science", "English", "C Language"]] as [String : Any]
    let name = json["name"] as? String ?? ""
    print(name) // Output : john
    let subjects = json["subjects"] as? [String] ?? []
    print(subjects) // Output : ["Maths", "Science", "English", "C Language"]

Downcasting values from Optional JSON

    let response: Any = ["name" : "john", "subjects": ["Maths", "Science", "English", "C Language"]]
    let json = response as? [String: Any] ?? [:]
    let name = json["name"] as? String ?? ""
    print(name) // Output : john
    let subjects = json["subjects"] as? [String] ?? []
    print(subjects) // Output : ["Maths", "Science", "English", "C Language"]

Manage JSON Response with conditions

    let response: Any = ["name" : "john", "subjects": ["Maths", "Science", "English", "C Language"]] //Optional Response 
    
    guard let json = response as? [String: Any] else {
        // Handle here nil value
        print("Empty Dictionary")
        // Do something here
        return
    }
    let name = json["name"] as? String ?? ""
    print(name) // Output : john
    let subjects = json["subjects"] as? [String] ?? []
    print(subjects) // Output : ["Maths", "Science", "English", "C Language"]

Manage Nil Response with condition

    let response: Any? = nil
    guard let json = response as? [String: Any] else {
        // Handle here nil value
        print("Empty Dictionary")
        // Do something here
        return
    }
    let name = json["name"] as? String ?? ""
    print(name) 
    let subjects = json["subjects"] as? [String] ?? []
    print(subjects) 

Output : Empty Dictionary