【问题标题】:Swift 3.0 Result of call is unused [duplicate]Swift 3.0 调用结果未使用 [重复]
【发布时间】:2016-11-06 15:50:38
【问题描述】:

我正在使用 swift 3.0 编写

我有这段代码,它给了我调用的警告结果是未使用的

public override init(){
    super.init()
}
            
public init(annotations: [MKAnnotation]){
    super.init()
    addAnnotations(annotations:  annotations)        
}
            
public func setAnnotations(annotations:[MKAnnotation]){
    tree = nil
    addAnnotations(annotations: annotations)
}
            
public func addAnnotations(annotations:[MKAnnotation]){
    if tree == nil {
        tree = AKQuadTree()
    }
                
    lock.lock()
    for annotation in annotations {
// The warning occurs at this line
        tree!.insertAnnotation(annotation: annotation)
    }
    lock.unlock()
}

我尝试在另一个类中使用此方法,但它仍然给我错误插入注释的代码在上面


func insertAnnotation(annotation:MKAnnotation) -> Bool {
    return insertAnnotation(annotation: annotation, toNode:rootNode!)
}
        
func insertAnnotation(annotation:MKAnnotation, toNode node:AKQuadTreeNode) -> Bool {
            
    if !AKQuadTreeNode.AKBoundingBoxContainsCoordinate(box: node.boundingBox!, coordinate: annotation.coordinate) {
        return false
    }
            
    if node.count < nodeCapacity {
        node.annotations.append(annotation)
        node.count += 1
        return true
    }
            
    if node.isLeaf() {
        node.subdivide()
    }
            
    if insertAnnotation(annotation: annotation, toNode:node.northEast!) {
        return true
    }
            
    if insertAnnotation(annotation: annotation, toNode:node.northWest!) {
        return true
    }
            
    if insertAnnotation(annotation: annotation, toNode:node.southEast!) {
        return true
    }
            
    if insertAnnotation(annotation: annotation, toNode:node.southWest!) {
        return true
    }
    
    return false
}

我尝试了很多方法,但都不起作用,但在 swift 2.2 中它运行良好,任何想法为什么会发生这种情况?

【问题讨论】:

标签: ios swift swift3


【解决方案1】:

您遇到此问题是因为您正在调用的函数返回一个值,但您忽略了结果。

有两种方法可以解决这个问题:

  1. 通过在函数调用前添加_ =来忽略结果

  2. @discardableResult添加到函数声明中以使编译器静音

【讨论】:

  • 一方面你可以把@discardableResult 一次并修复它们,但另一方面你不能用你的豆荚做到这一点,所以你必须使用第一种解决方案。
  • 那个奥西尔是什么意思?你不能将它与 CocoaPods 一起使用?
  • @gran_profaci 您可能正在使用其他人的代码(通过 Pod 或其他地方)并且无法更改代码接口定义,在这种情况下,“_ =”是唯一的解决方案。
  • @discardableResult 是完美的 ;) 目前正在通过 swift3 迁移!
  • 谁决定了 Swift 的这种行为!?我想用一些编译器选项来抑制这个警告,这很烦人而且没用。
猜你喜欢
  • 2017-03-02
  • 2018-02-10
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
相关资源
最近更新 更多