在快速的代表数组中查找代表

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

问题描述

我想在删除之前检查我的 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)
    }
}

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)