186

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?

b m gevariya
  • 300
  • 1
  • 4
  • 12
Aryan Kashyap
  • 1,913
  • 2
  • 10
  • 6
  • 5
    It's telling you the truth. Your function returns a `Bool`, and you ignore that fact with your invocation. If that's what you mean to do, and you want to shut up the warning, change the invocation to `_ = tree!.insertAnnotation(annotation: annotation)`. – pjs Jul 04 '16 at 22:11
  • 1
    See this thread: [Result of call to "myFunction" is unused](http://stackoverflow.com/questions/37939573/result-of-call-to-myfunction-is-unused). – OOPer Jul 04 '16 at 22:12
  • 3
    @discardableResult http://stackoverflow.com/a/27261991/2303865 – Leo Dabus Jul 04 '16 at 22:16

1 Answers1

571

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

David Skrundz
  • 13,067
  • 6
  • 42
  • 66
  • 8
    On one hand you can put `@discardableResult` once and fix them all, but on other hand you can't make it with you pods, so you have to use 1st solution. – Ossir Oct 12 '16 at 08:58
  • What do you mean by that Ossir? You cannot use it with CocoaPods? – gran_profaci Oct 17 '16 at 20:57
  • 3
    @gran_profaci You might be using someone else's code (through Pods or somewhere else) and cannot change the code interface definition, in which case "_ =" is the only solution. – Andree Oct 18 '16 at 02:44
  • 11
    Who decided this behaviour for Swift!? I'd like to suppress that warning with some compiler option, it's quite annoying and useless. – Ferran Maylinch Apr 19 '17 at 15:45
  • @FerranMaylinch It's actually pretty fantastic. If you're calling a function that returns a result and you don't use it, that's very likely to be a mistake. The compiler is just warning you to make sure you didn't overlook it. If it's commonly not a mistake for that API, then the API should have been marked `@discardableResult`, or not have a return value in the first place. – Alexander Jul 30 '23 at 23:23