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

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

问题描述

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

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

这是我到目前为止所做的:

Here's what I've got so far:

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
        }
    }
}

然而,这给了我以下错误'如果包含'line:

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

不能使用类型'(@lvalue Array< LocationManagerDelegate>!,LocationManagerDelegate)'

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

推荐答案

Swift 3的更新:

假设代表们实际上是实例一个,您可以通过继承类来在协议中要求:

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

protocol LocationManagerDelegate: class {
    // ...
}

and然后使用索引(其中:) 方法,使用indentitiy运算符
===

and then use the index(where:) method, using the "indentitiy operator ===:

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

    func removeDelegate(delegate:LocationManagerDelegate) {
        if let index = _delegates.index(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 ==.

假设委托实际上是的实例,你可以通过继承class来在协议中要求

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

protocol LocationManagerDelegate : class {
    // ...
}

然后使用第二个基于谓词的版本包含()
身份运算符 ===

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
    }
}

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

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
}

然后使用

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

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

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