在快速的代表数组中查找代表 [英] Find delegate in a swift Array of delegates

查看:19
本文介绍了在快速的代表数组中查找代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在删除之前检查我的 removeDelegate 方法中是否已经有一个委托.我怎么做?

I want to check if I already have a delegate in my removeDelegate method before removing. How do I do that?

这是我目前所得到的:

protocol LocationManagerDelegate {
    func locationManagerDidUpdateLocation(
        oldLocation: CLLocationCoordinate2D,
        currentLocation: CLLocationCoordinate2D
    )
}

class LocationManager: NSObject {
    private var _delegates = [LocationManagerDelegate]()

    func removeDelegate(delegate:LocationManagerDelegate) {
        if contains(_delegates, delegate) {
            // Remove delegate
        }
    }
}

但是,这在if contains"行上给了我以下错误:

However, this gives me the following error on the 'if contains' line:

无法使用类型为(@lvalue Array!, LocationManagerDelegate)"的参数列表调用contains"

cannot invoke 'contains' with an argument list of type '(@lvalue Array< LocationManagerDelegate >!, LocationManagerDelegate)'

推荐答案

Swift 4.2 更新:

假设委托实际上是一个的实例,你可以在协议中通过继承"来要求它.来自类":

Assuming that the delegates are actually instances of a class, you could require that in the protocol by "inheriting" from "class":

protocol LocationManagerDelegate: class {
    // ...
}

然后使用 firstIndex(where:) 方法,使用恒等运算符"===:

and then use the firstIndex(where:) method, using the "identity operator ===:

class LocationManager: NSObject {
    private var _delegates = [LocationManagerDelegate]()
    
    func removeDelegate(delegate:LocationManagerDelegate) {
        if let index = _delegates.firstIndex(where: { $0 === delegate }) {
            _delegates.remove(at: index)
        }
    }
}


旧答案(Swift 1):

有两个略有不同的contains()函数:

There are two slightly different contains() functions:

func contains<S : SequenceType where S.Generator.Element : Equatable>(seq: S, x: S.Generator.Element) -> Bool

func contains<S : SequenceType, L : BooleanType>(seq: S, predicate: (S.Generator.Element) -> L) -> Bool

您使用的是第一个,它要求序列元素符合Equatable 协议,即它们可以与 == 进行比较.

You are using the first one, which requires that the sequence elements conform to the Equatable protocol, i.e. they can be compared with ==.

假设委托实际上是一个的实例,你可以要求在协议中通过继承"来自类":

Assuming that the delegates are actually instances of a class, you could require that in the protocol by "inheriting" from "class":

protocol LocationManagerDelegate : class {
    // ...
}

然后将 contains() 的第二个基于谓词的版本与身份运算符 ===:

and then use the second, predicate-based version of contains() with the identity operator ===:

func removeDelegate(delegate:LocationManagerDelegate) {
    if contains(_delegates, { $0 === delegate }) {
        // Remove delegate
    }
}

要从数组中删除对象,您必须获取它的索引,因此您可以使用https://stackoverflow.com/a/25543084/1187415 中的 findIdenticalObject() 函数:

To remove the object from the array you'll have to get its index, so you might use the findIdenticalObject() function from https://stackoverflow.com/a/25543084/1187415:

func findIdenticalObject<T : AnyObject>(array: [T], value: T) -> Int? {
    for (index, elem) in enumerate(array) {
        if elem === value {
            return index
        }
    }
    return nil
}

然后从数组中查找并删除

and then find and remove from the array with

func removeDelegate(delegate:LocationManagerDelegate) {
    if let index = findIdenticalObject(_delegates, delegate) {
        _delegates.removeAtIndex(index)
    }
}

这篇关于在快速的代表数组中查找代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆