Need help with a simple Swyft/Xcode Failable init and Closure. I need to create
ID: 3743657 • Letter: N
Question
Need help with a simple Swyft/Xcode Failable init and Closure. I need to create what is described below in an Xcode Playground. Any help is appreciated.
Create a struct called MathOperation
MathOperation should contain two properties:
units
operation
Units will be of type String and contain the units that the operation returns Operation will be a closure that takes in two Doubles and returns a Double. Create a single initializer that is a Failable Initializer, that checks that the "units" property actually contains text, else the initializer should fail.
Explanation / Answer
import Foundation
struct MathOperation {
var units: String
func Operation( a:Double, b:Double) ->Double {
var ret:Double=a * b
self.units=String(ret)
return ret;
}
init?(units:String) {
if units as? String {
self.units=units
}
else{
return nil
}
}
}