I have recently learned the swift language. I feel very comfortable with Js. Let's record my learning process here.
I don't have much access to objective-C, so I may still have a lot of things I don't know about in OC.
Recently I have written some small example apps using SWIFT, and the conversion of basic types is used in many places, but I found that he does not have integer. something like parse (), so I tried again.
PS: the version I used is xcode6 bate4 (other versions may be implemented differently)
OK, talk is chaep, show you the code!
Extended double, used to retain a few decimal places, such as double. Format (". 2") to retain two digits. Others are similar to this
Extension double {// convert func format (F: string)-> string {return nsstring (Format: "% \ (f) F", self )}}
INT-> double, float, string
var i:Int = 1Double(i)// 1.0Float(i)// 1.0i.bridgeToObjectiveC().stringValue// "1"
String-> int, Doubel, float
var s:String = "1.9d2d"s.toInt()//nils.bridgeToObjectiveC().integerValue// 1s.bridgeToObjectiveC().doubleValue// 1.9s.bridgeToObjectiveC().floatValue// 1.89999999...
The conversion rules here are similar to those in JS, such:
- When the first character of a string is not a number and converted to a double (INT), it is 0,
- When the first digit of a string is a number, it is directly converted to a number until a non-numeric character is stopped.
Double, float, Int-> string
VaR D: Double = 1.09d.bridgetoobjectivec (). stringvalue // 1.09 "D. format (". 1 ") // according to the extension double extension method (rounding)->" 1.1 "Var F: Float = 1.09f.bridgetoobjectivec (). stringvalue // "1.09"Basic rules:
- String (INT) conversion is not allowed.
- Int can directly use double (INT), float (INT)
- Most of the basic types are converted using the bridgetoobjectivec () function.