Rohan Vasishth
Rohan Vasishth

Reputation: 417

String Interpolation Segment Error in Swift 3

I recently upgraded my project to Swift 3 and have been having some trouble with a string interpolation error.

My code:

let coordString = "\(locationCoordinate.latitude) \(locationCoordinate.longitude)".stringByReplacingOccurrencesOfString(".", withString: ",")

The error says:

Static member 'init(stringInterpolationSegment:)' cannot be used on instance of type 'String'

How can I solve the error?

Upvotes: 1

Views: 801

Answers (2)

vadian
vadian

Reputation: 285132

Replacing the decimal separator using replacingOccurrences(of... is not good programming habit.

You should use always NumberFormatter to be able to consider the current locale of the user.

This is an example. The decimal separator is displayed depending on the current locale. If you really want explicit a comma uncomment the locale line and set the locale identifier to your preferred one.

let latitude = 52.5
let longitude = 13.0
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
// formatter.locale = Locale(identifier: "DE_de")

let coordString = formatter.string(from: NSNumber(value:latitude))! + " " + formatter.string(from: NSNumber(value:longitude))!

Upvotes: 1

shallowThought
shallowThought

Reputation: 19602

The method has been renamed in Swift 3:

let coordString = "\(locationCoordinate.latitude) \(locationCoordinate.longitude)".replacingOccurrences(of: ".", with: ",")    

Upvotes: 0

Related Questions