Basic type conversion in swift

Source: Internet
Author: User

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:

  1. When the first character of a string is not a number and converted to a double (INT), it is 0,
  2. 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:
  1. String (INT) conversion is not allowed.
  2. Int can directly use double (INT), float (INT)
  3. Most of the basic types are converted using the bridgetoobjectivec () function.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.