比较两个图像列表并过滤掉重复。 (SWIFT)(IOS)

时间:2015-03-13 01:10:00

标签: ios swift filter control-flow

我有两个ID(字符串)列表,代表两组图像。 viewSpaceArray是用户已查看的图像的ID。 HomeViewData包含整个图像集。 currentSpaceId是当前图像的ID。 Number是HomeViewData上项目的索引 我想要做的是过滤掉已查看的图像,直到我得到一个未经审查的"一。

我设法将单个currentSpaceId与列表viewSpaceArray进行比较,但我不知道如何创建一个循环来遍历一系列currentSpaceId,直到找到未查看的项目。这是代码: 感谢

func filterViewedSpaces() {
    viewedSpaceArray.removeAllObjects()
    var findViewQuery:PFQuery = PFQuery(className: "views")
    findViewQuery.findObjectsInBackgroundWithBlock{
        (objects: [AnyObject]!, error:NSError!) -> Void in
        if error == nil {
            for object in objects {
                let upload:PFObject = object as PFObject
                let spaceId = upload["spaceId"] as String                        
                self.viewedSpaceArray.addObject(spaceId)
            }
        }
    }

    var currentSpace:PFObject = self.homeViewData[self.number] as PFObject
    var currentSpaceId = currentSpace.objectId as String!

    for object in viewedSpaceArray {
        let vSpaceId = object as String
        if currentSpaceId == vSpaceId {
            number++
        } else {
            loadData()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

以下是使用集差分算法found here

的一种方法
/// Computes the relative complement of a with respect to b
func relative_complement<T: Comparable>(a a0: Array<T>, b b0: Array<T>) -> Array<T> {
    let a = a0.sorted { $0 < $1 }
    let b = b0.sorted { $0 < $1 }

    var result = [T]()

    var x = 0, y = 0
    while x < a.count {
        if y == b.count {
            return Array<T>(a[x..<a.count])
        }

        if a[x] < b[y] {
            result.append(a[x])
            ++x
        }
        else {
            if !(b[y] < a[x]) {
                ++x
            }
            ++y
        }

    }

    return result
}

你可以像这样使用它:

let collection = ["a","b","c","d","e","f"]
let viewed = ["a","c","d","f"]

let relcomp = relative_complement(a: collection, b: viewed)
// relcomp contains ["b","e"]