• <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. 在快速的代表数组中查找代表

        Find delegate in a swift Array of delegates(在快速的代表数组中查找代表)
          <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)
                      }
                  }
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS AutoLayout - get frame size width(iOS AutoLayout - 获取帧大小宽度)
                  Auto layout constraints issue on iOS7 in UITableViewCell(UITableViewCell中iOS7上的自动布局约束问题)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5(自动布局中的弹簧:在 Xcode 5 中使用约束均匀分布视图)
                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员

                  <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>
                              <tbody id='lRvCe'></tbody>