• <i id='f07bT'><tr id='f07bT'><dt id='f07bT'><q id='f07bT'><span id='f07bT'><b id='f07bT'><form id='f07bT'><ins id='f07bT'></ins><ul id='f07bT'></ul><sub id='f07bT'></sub></form><legend id='f07bT'></legend><bdo id='f07bT'><pre id='f07bT'><center id='f07bT'></center></pre></bdo></b><th id='f07bT'></th></span></q></dt></tr></i><div id='f07bT'><tfoot id='f07bT'></tfoot><dl id='f07bT'><fieldset id='f07bT'></fieldset></dl></div>

    <legend id='f07bT'><style id='f07bT'><dir id='f07bT'><q id='f07bT'></q></dir></style></legend>

      <bdo id='f07bT'></bdo><ul id='f07bT'></ul>

    <small id='f07bT'></small><noframes id='f07bT'>

    1. <tfoot id='f07bT'></tfoot>
      1. 在快速的代表数组中查找代表

        时间:2023-09-12
          <tbody id='FNCsq'></tbody>
        <tfoot id='FNCsq'></tfoot>
          <legend id='FNCsq'><style id='FNCsq'><dir id='FNCsq'><q id='FNCsq'></q></dir></style></legend>
          • <bdo id='FNCsq'></bdo><ul id='FNCsq'></ul>

                1. <small id='FNCsq'></small><noframes id='FNCsq'>

                  <i id='FNCsq'><tr id='FNCsq'><dt id='FNCsq'><q id='FNCsq'><span id='FNCsq'><b id='FNCsq'><form id='FNCsq'><ins id='FNCsq'></ins><ul id='FNCsq'></ul><sub id='FNCsq'></sub></form><legend id='FNCsq'></legend><bdo id='FNCsq'><pre id='FNCsq'><center id='FNCsq'></center></pre></bdo></b><th id='FNCsq'></th></span></q></dt></tr></i><div id='FNCsq'><tfoot id='FNCsq'></tfoot><dl id='FNCsq'><fieldset id='FNCsq'></fieldset></dl></div>

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

                  问题描述

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

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

                  上一篇:Xcode 故事板委托数据源场景 下一篇:无法在具有@objc 属性的协议中使用自定义类?

                  相关文章

                  <small id='lRvCe'></small><noframes id='lRvCe'>

                    • <bdo id='lRvCe'></bdo><ul id='lRvCe'></ul>
                    1. <legend id='lRvCe'><style id='lRvCe'><dir id='lRvCe'><q id='lRvCe'></q></dir></style></legend>
                      <i id='lRvCe'><tr id='lRvCe'><dt id='lRvCe'><q id='lRvCe'><span id='lRvCe'><b id='lRvCe'><form id='lRvCe'><ins id='lRvCe'></ins><ul id='lRvCe'></ul><sub id='lRvCe'></sub></form><legend id='lRvCe'></legend><bdo id='lRvCe'><pre id='lRvCe'><center id='lRvCe'></center></pre></bdo></b><th id='lRvCe'></th></span></q></dt></tr></i><div id='lRvCe'><tfoot id='lRvCe'></tfoot><dl id='lRvCe'><fieldset id='lRvCe'></fieldset></dl></div>
                      <tfoot id='lRvCe'></tfoot>