Swift 3.0 Result of call is unused

IosSwiftSwift3

Ios Problem Overview


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
}

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

Ios Solutions


Solution 1 - Ios

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. Ignore the result by adding _ = in front of the function call

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAryan KashyapView Question on Stackoverflow
Solution 1 - IosDavid SkrundzView Answer on Stackoverflow