0

I'm completely lost with swift. I am trying to split an array of objects (retrieved using the Parse SDK) based on a common key. Basically I would like to make a COUNT + GROUP BY, but I could not find a way to make a GROUP BY with the Parse SDK, so I am trying to group the objects and count them by the code. But first, what I am writing looks horrible, and secondly that does not even compile and I can't understand why.

This is the code I'm using

var groupedEntries = [String: [String: AnyObject?]]()

...

let query = PFQuery(className: "Entry")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
   if error == nil {

      var groupingKey: String
      for object in objects {

         groupingKey = object[groupingKey] as String
         if self.groupedEntries[groupingKey] != nil {
            self.groupedEntries[groupingKey]["count"] = self.groupedEntries[groupingKey]["count"] + 1
         } else {
            self.groupedEntries[groupingKey]["count"] = 1
         }
      }
      self.tableView.reloadData()
   }
}

And the compiler gives the following error : 'String' is not convertible to 'DictionaryIndex'. Whatever I try I get an error related to optionals or wrapping.

Thanks to anybody that will help me :)

1 Answer 1

1

While using subscript the Dictionary returns optional of given value type so you need to unwrap it.You need to take the extra variable i used somevar.See below code

var groupedEntries = [String: [String: AnyObject?]]()

@IBAction func changeImage(sender: AnyObject){
    var objects:[AnyObject]!
    var groupingKey: String //= "abc" Intialize before using
    //var countryCode: String   //= "abc" not defined in your code

    for object in objects {

        groupingKey = object[groupingKey] as String
        if self.groupedEntries[groupingKey] != nil {
            var somevar = self.groupedEntries[groupingKey]!
            somevar["count"] = (somevar["count"]! as Int) + 1
        } else {
            self.groupedEntries[groupingKey] = ["count":1]
        }
    }
}

I do not know about your logic but this code compiles well.

Sign up to request clarification or add additional context in comments.

7 Comments

Well, this compile, but generates an EXC_BAD_INSTRUCTION exception on the "else" statement (fatal error: unexpectedly found nil while unwrapping an Optional value).
you should check self.groupedEntries[countryCode] for nil also if nil need to intialize it
It is improvement but these kind of stuff really get mess if you do not use good techniques.Study good Json parsing library code
Sorry used countryCode instead of groupingKey.Not saw your edit. edited answer
@codester, in the else clause, self.groupedEntries[groupingKey] is nil as established by the if. I suggest first setting it to a new dictionary.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.