Swift 3.0 调用结果未使用 [英] Swift 3.0 Result of call is unused

查看:24
本文介绍了Swift 3.0 调用结果未使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 swift 3.0 编写

I am writing in swift 3.0

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

I have this code which gives me the warning result of the call is unused

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

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

I have tried using this method in another class but it still gives me the error the code for insert Annotation is above


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 中它可以正常工作任何想法为什么会发生这种情况?

I have tried many methods but just doesn't work but in swift 2.2 it works fine any ideas why this is happening?

推荐答案

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

You are getting this issue because the function you are calling returns a value but you are ignoring the result.

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

There are two ways to solve this issue:

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

在函数声明中添加@discardableResult,使编译器静音

Add @discardableResult to the declaration of the function to silence the compiler

这篇关于Swift 3.0 调用结果未使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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