0

I am trying to query the distance of my users. I can do so with this code

    PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGeoPoint?, error: NSError?) -> Void in

    let query = PFUser.query()

    query?.whereKey("location", nearGeoPoint: geoPoint!, withinKilometers: 1.0)

What I am trying to do is that when the user sets a slider value that this value is used instead of 1.0 as seen above. I was attempting to do this with this code: This is the slider code:

var distanceSearch: String?

 @IBAction func kmSliderValueChanged(sender: UISlider) {

    let kmCurrentValue = Int(sender.value)

    kmLabelUpdate.text = "\(kmCurrentValue)km"

    distanceSearch = "\(kmCurrentValue)"
}

And then this is the query line:

query?.whereKey("location", nearGeoPoint: geoPoint!, withinKilometers: distanceSearch!)

It is returning an error on the query line:

cannot convert value of type String to expected argument type Double

So I need to convert the slider kmCurrentValue to a double but I don't understand how to do this from reading the other SO questions. I am still new enough to coding in general. Can someone tell me what it is I need to do?

SO convert string to double

6
  • Have you tried Double(distanceSearch!)?
    – luk2302
    Nov 24, 2015 at 17:14
  • Yes that worked!!! Thanks, will you add it as an answer and I'll mark it!
    – Grace
    Nov 24, 2015 at 17:26
  • 1
    Why are you converting from Int to String just to then convert to Double ?
    – Wain
    Nov 24, 2015 at 17:26
  • @Wain please see the comments under Robs answer.
    – Grace
    Nov 24, 2015 at 17:39
  • Yeah, so you shouldn't do all the converting.
    – Wain
    Nov 24, 2015 at 17:44

4 Answers 4

0

Simply convert the distanceSearch to a Double using the initializer:

query?.whereKey("location", nearGeoPoint: geoPoint!, withinKilometers: Double(distanceSearch!)!)
3
  • 1
    Why are you not recommending to not store the value as a string?
    – Wain
    Nov 24, 2015 at 17:27
  • @Wain because he asked how to convert it to a Double. I did not look over his code to verify that is actually to proper thing for him to do in the first place. As it seems though you are right that he is doing unnecessary work.
    – luk2302
    Nov 24, 2015 at 17:30
  • This was the fix I requested and its worked, maybe my code is a mess but this fixed my immediate issue as requested above. Thanks.
    – Grace
    Nov 24, 2015 at 23:58
0

Swift's Double class has a constructor that takes a string argument, so you could do Double(distanceSearch!) as @luk2302 suggested in the comments.

This result returns a Swift optional, however, so you should do Double(distanceSearch!)! in order to unwrap the optional returned by the Double constructor. Take a look at the Apple documentation on that Double constructor.

1
  • hey vigneshv ya the prompter suggested those edits to luk2302s answer thanks.
    – Grace
    Nov 25, 2015 at 0:00
0

There are many ways to convert String to Double. You can use the default initializer:

Double("23.0")

Or convert to NSString and use the doubleValue method.

("23.0" as NSString).doubleValue

There is an additional thing to do when you are dealing with an optional.

let str: String? = ...

Then you can try to forcefully unwrap it:

Double(str!)

But this will crash when your string in nil. The safer way is:

Double(str ?? "0")

This one will default to zero.

2
  • ... will default to an optional double. Nov 25, 2015 at 5:17
  • It will default to an optional double with a value of 0 if the string is nil.
    – Adam
    Nov 25, 2015 at 9:43
0

There's no reason for all this indirection. The slider itself is giving you a double. You don't need to convert it to a string and then back to a double. Just use the value from the slider itself.

4
  • Sorry maybe I should have stated in my question but the slider is on a different View Controller so to store its value I was storing it as such. Would you still suggest storing it as an Int instead of a string?
    – Grace
    Nov 24, 2015 at 17:33
  • @grace If you want a double, why wouldn't you store it as a double? (Also, one view controller should almost never grab properties from another view controller. And a model class should never, ever read a view controller property. This suggests you're violating MVC.) But yes, storing a number as a string is probably never the right answer.
    – Rob Napier
    Nov 24, 2015 at 17:33
  • I set it up this way as view controller 1 is a settings page so the user sets the distance they want to search users by. Then on VC2 it only displays users in the distance setting that was set on VC1. Maybe I have gone about it all wrong I am very new to app development....I did try to store it as a Double on VC1 but it returned more errors.
    – Grace
    Nov 24, 2015 at 17:37
  • Either VC1 should pass its values to VC2 (if VC1 is the parent of VC2), or the values should be stored in the model and both VCs should query it. VC2 should barely know anything about VC1. (In your particular case, it sounds like VC1 should probably be passing data to VC2.) I don't understand "returned more errors."
    – Rob Napier
    Nov 24, 2015 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.