0

I want to convert a String in a NSString, for obtaining a double value. But I have an error:

Argument labels '(_:)' do not match any available overloads.

How can I fix this?

NSString as! String

1
  • The init for a String is NSString(string:) you need to have the label. Commented Dec 24, 2017 at 15:16

4 Answers 4

5

Don't do that.

Basically you can bridge cast String to NSString

let nsString = "12.34" as NSString
print(nsString.doubleValue)

rather than using an initializer (the error message says there is no appropriate initializer without a parameter label), but casting to NSString to get the doubleValue is the wrong way in Swift.

Get the String, use the Double initializer and unwrap the optionals safely

if let lat = array[indexPath.row]["lat"] as? String, 
   let latAsDouble = Double(lat) {
       // do something with latAsDouble
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thx vadian. I use your code for return a CLLocation and calculate the distance between two points in kilometers, and it works.
1

I guess you have to use this syntax to convert

let myString = "556"
let myFloat = (myString as NSString).doubleValue

Comments

0

Swift 3 & 4

String to Double

CartAmount = (response.object(forKey:"amount")as! NSString).doubleValue!

Double to String

let string = String(CartAmount)

Comments

-1

swift 4 - String to Double || NSString to Double

try this

 let nsstring:NSString = "111.11"
        let str:String = "111.11"

        let tempString = (str as NSString).doubleValue
        print("String:-",tempString)

        let temp = nsstring.doubleValue
        print("NSString",temp)

Output

String:- 111.11
NSString 111.11

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.