I have been using swift for several days. It is found that in addition to several basic types of conversions, some special numeric types need to be converted from "bridging" to "Objective-C ~
The Code is also very simple ~
Var numString = "1.0" var numDouble: DoublenumDouble = String. bridgeToObjectiveC (numString )(). doubleValue // equivalent to objective-c's "numdouble = [numString doubleValue]" // numDouble value: 1.0
What if numString is not a "Number" character but another character?
Var numString = "abc" var numDouble: DoublenumDouble = String. bridgeToObjectiveC (numString) (). doubleValue // numDouble: 0.0
This will not cause compiler exceptions. Continue with the following situations:
Var numString1 = "1.5abc" var numString2 = "2. abc0 "var numString3 =" abc3.0 "var numDouble: DoublenumDouble = String. bridgeToObjectiveC (numString1 )(). doubleValue // numDouble1 value: 1.5 numDouble = String. bridgeToObjectiveC (numString2 )(). doubleValue // numDouble2 value: 2.0 numDouble = String. bridgeToObjectiveC (numString3 )(). doubleValue // numDouble3: 0.0
We can see that the doubleValue index starts from the first character of the string and searches for consecutive numeric characters (including a ". "), to the second". "or non-numeric characters until the end of the index.