Pickerview (using parse.com)

55 views
Skip to first unread message

Heidi Anselstetter

unread,
Feb 25, 2015, 12:15:31 PM2/25/15
to swift-l...@googlegroups.com
Hi,

I have a table "languages" where I have data like "objectId" and "name". This data I want to show in a PickerView.
Fine so far, I put the "name" into an array and show it.

But I want to save the selection back to another table "settings". And there I need to save only the "objectId" of the selected pickerview row into "language".

As far as I can see, the UIPickerView works with arrays, but I can not store the objectId and the name in an array?
I would need to use a dictionary, right?

But a dictionary has no numeric index, so get error messages on functions like

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return languagePickerValues[row] } 
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){ selectedLanguage.text = languagePickerValues[row] self.view.endEditing(true) }

as these functions expect an numeric index.

So what is the right way?
Do I need 1 array and 1 dictionary?
The array for displaying the "name" and the dictionary to match the selected "name" against the dictionary? Don't thinks so, far too dangerous in terms of twin entries...

Does anyone has an idea for me?
I can't be the first person who want to get this working?
But I am searching for 2 days for an solution and can't find any help...

Thanks!

Heidi Anselstetter

unread,
Feb 27, 2015, 6:05:36 AM2/27/15
to swift-l...@googlegroups.com
This way it works for me, using an array for the keys and an array for the values:

var gradePickerKeys = [String]()



var gradePickerValues = [String]()

var selectedGradeObjectId: String?


query the data from parse, fill the 2 arrays with keys and values.

var query = PFQuery(className:"Grade")



        query
.orderByAscending("name")

        query
.findObjectsInBackgroundWithBlock {

           
(objects: [AnyObject]!, error: NSError!) -> Void in

           
if error == nil {

               

               
for object in objects {

                   

                   
var name = object["name"] as String

                   

                   
self.gradePickerKeys.append(object.objectId)

                   
self.gradePickerValues.append(name)

                   

                   
self.gradePicker?.reloadAllComponents()

                   

                   
self.gradeTextField.text = self.gradePickerValues.first

                   
self.selectedGradeObjectId = self.gradePickerKeys.first

                   

               
}

           
} else {

               
// Log details of the failure

                println
("\(error.userInfo)")

           
}

 

       
}


// functions needed for picker view



    func numberOfComponentsInPickerView
(pickerView: UIPickerView) -> Int{

       
return 1

   
}

    func pickerView
(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{

       
return gradePickerValues.count

   
}


    func pickerView
(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {


       
return gradePickerValues[row]


   
}

    func pickerView
(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){


        gradeTextField
.text = gradePickerValues[row]

        selectedGradeObjectId
=  gradePickerKeys[pickerView.selectedRowInComponent(0)] // array value

       
self.view.endEditing(true)

 

   
}


Saving the pointer

let gradeObjectId = PFObject(withoutDataWithClassName: "Grade", objectId: selectedGradeObjectId)



 

var user = PFUser()

 

user
["grade"] = gradeObjectId

 

user
.signUpInBackgroundWithBlock {....


Maybe one day someone can show me a better solution...

Dennis W

unread,
Mar 26, 2015, 12:57:32 AM3/26/15
to swift-l...@googlegroups.com
Hi Heidi,

You might consider storing the information for the languages as a dictionary as you mentioned, then you can access the all the values or keys using the standard accessors provided. For example, if you have a dictionary called languages, then you could get the keys or values as an array using the following:

    languages = [1:"English", 2:"Latin", 3:"Spanish", 4:"French"]


    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

        var languageNames = Array(languages.values)

        return languageNames[row]


    }


    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        var languageIds = Array(languages.keys)

        println("item id:\(languageIds[row])")

    }


One issue you might have in this case is ordering, since dictionaries are unordered by nature. You can extract the language names using the above method, sort the resulting array and display the sorted contents in the picker. Then you will need to find the key for a given value (Swift does not have this method, but Objective-C does) using a for-in loop or maybe consider using the NSDictionary type instead of Swift's dictionary. If you think that you might expand the definition of language, such as including country of origin or number of characters, etc, then creating an object to represent the language would be the better way to proceed. Hope this helps.

Brent Royal-Gordon

unread,
Mar 26, 2015, 4:21:41 AM3/26/15
to Dennis W, swift-l...@googlegroups.com
Then you will need to find the key for a given value (Swift does not have this method, but Objective-C does)

 While there's no precise equivalent to -[NSDictionary allKeysForObject:], you can chain filter() and map() to do the same thing:

    map(filter(dict) { _, value in value == 1 }) { key, _ in key }


Or do it lazily in one pass (the above actually uses two loops, one in filter() and the other in map()):

    lazy(dict).filter { _, value in value == 1 }.map { key, _ in key }.array

Dennis W

unread,
Mar 26, 2015, 11:27:35 AM3/26/15
to swift-l...@googlegroups.com
Brent, that is great answer, I keep forgetting about Map and Filter in Swift, Thanks!


On Wednesday, February 25, 2015 at 9:15:31 AM UTC-8, Heidi Anselstetter wrote:
Reply all
Reply to author
Forward
0 new messages