I have a question and unfortunately not found anywhere responses.
I'll describe the problem in a simple example. I have an array of objects. Each object contains the id, name, count. This array is the result of parsing the data from the server. It is taken as the first.
I also have a second array taken from another server. This is the same array id, name, count.
My question is as follows. The first array has 20 elements, then each of this element I want to compare with an array of the other. The second array is the parse. You may need to use a loop and check whether every element in the first array is in the second or not?
//
// ViewController.swift
// ParseSearching
//
// Created by Mateusz Fraczek on 21.07.2015.
// Copyright (c) 2015 universeldev. All rights reserved.
//
import UIKit
import Parse
struct Model : Equatable {
var ide: String!
var name: String!
var count: String!
}
func ==(a: Model, b: Model) -> Bool {
return a.ide == b.ide && a.name == b.name && a.count == b.count
}
extension Array {
func indexesOfSubset<T : Equatable>(objects : [T]) -> [Int] {
// Create storage for filtered objects and results
var unusedObjects = objects
var result : [Int] = []
// Enumerate through all objects in array
for (index, obj) in enumerate(self) {
// Enumerate again through all objects that has not been found
for x in unusedObjects {
// If we hit match, append result, remove it from usused objects
if obj as! T == x {
result.append(index)
unusedObjects = unusedObjects.filter( { $0 != x } )
break
}
}
}
// Get results
return result
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var firstArray = [Model]()
var resultsArray = [Model]()
firstArray.append(Model(ide: "xyz1", name: "name1", count: "0"))
firstArray.append(Model(ide: "xyz2", name: "name2", count: "0"))
firstArray.append(Model(ide: "xyz3", name: "name3", count: "0"))
firstArray.append(Model(ide: "xyz4", name: "name4", count: "0"))
// let testObject = PFObject(className: "TestObject")
// testObject["ide"] = "xyz1"
// testObject["name"] = "name1"
// testObject["count"] = "0"
// testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
// println("Object has been saved.")
// }
var query = PFQuery(className: "TestObject")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let object = objects as? [PFObject] {
for obj in object {
var id = obj.objectForKey("ide") as! String
var name = obj.objectForKey("name") as! String
var count = obj.objectForKey("count") as! String
var model = Model(ide: id, name: name, count: count)
resultsArray.append(model)
println(resultsArray)
let indexes = firstArray.indexesOfSubset(resultsArray)
println("Indexes \(indexes) ")
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}