Finding the distance, currency, temperature units etc. for a country (iPhone)

前端 未结 5 868
灰色年华
灰色年华 2020-12-29 04:27

During the localization of my app I went through a lot of documentation, somewhere Im sure I read that there is a way to get a set of units that is linked with the locale.

5条回答
  •  佛祖请我去吃肉
    2020-12-29 05:19

    Swift:

    1.)

    import MapKit
    

    2.) Since creating formatter is expensive, you better set formatter as a lazy property. By doing so, you can initialize the formatter only when it's necessary and reuse the formatter rather than create a new one.

    lazy var distanceFormatter: MKDistanceFormatter = {
        var tempDistanceFormatter =  MKDistanceFormatter()
        tempDistanceFormatter.unitStyle = .Abbreviated // .abbreviated in Swift 3
    
        return tempDistanceFormatter
    }()
    

    3.)

    let distanceString = self.distanceFormatter.stringFromDistance(yourDistance)
    

提交回复
热议问题