1
\$\begingroup\$

I have a PFQueryTableViewController, and have create a separate TableViewController that allows me to select a filter option. When the user selects an option to filter by, I pass the index value back, and then append that Int value to an array. The variable is declared at the top like:

var indexArray = [0]

In my QueryForTable() function, I've written:

    if indexArray == [0] {

        query.orderByDescending("createdAt")

    } else if indexArray == [1] {

        query.orderByAscending("FilmName")

    } else if indexArray == [2] {

        query.orderByDescending("OurRating")

    } else if indexArray == [3] {

        query.orderByAscending("DirectedBy")

    } else if indexArray == [4] {

        query.orderByDescending("UpVoteCount")

    } else if indexArray == [5] {

        query.orderByDescending("DownVoteCount")

    }

This code works fine, and is filtering the option fine.

I'm wondering if there is a better way to write the above code in my QueryForTable() function? Maybe to reduce the amount of code needed?

Any advice appreciated.

\$\endgroup\$
  • \$\begingroup\$ What are you trying to do? Are you trying to allow them to filter by multiple properties at once? Why are you using an array instead of just a single integer? \$\endgroup\$ – nhgrif Apr 1 '16 at 1:39
  • \$\begingroup\$ @nhgrif yes that is the plan. I guess I can just use a single Int. Would that make it easier? \$\endgroup\$ – Nick89 Apr 1 '16 at 7:04
  • \$\begingroup\$ Well, if your plan is to filter by multiple properties at once, I can't see how this works. Also, there doesn't seem to be a way to, for example, sort by "createdAt" in ascending order. \$\endgroup\$ – nhgrif Apr 1 '16 at 12:04
  • \$\begingroup\$ @nhgrif This currently works fine. It filters fine, and displays the items in my tableview as per the filter option. I just want to know is there a cleaner way to code this? Thanks \$\endgroup\$ – Nick89 Apr 1 '16 at 18:14
  • \$\begingroup\$ Why not use a switch statement instead of multiple else ifs? \$\endgroup\$ – kye Apr 2 '16 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.