Swift 3.0 Resultaat van oproep is ongebruikt

Ik schrijf in swift 3.0

Ik heb deze code die me de waarschuwing geeft dat de oproep ongebruikt is

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

Ik heb geprobeerd deze methode in een andere klas te gebruiken, maar ik krijg nog steeds de foutmelding dat de code voor annotatie invoegen hierboven staat


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
}

Ik heb veel methoden geprobeerd, maar het werkt gewoon niet, maar in swift 2.2 werkt het prima. Heb je enig idee waarom dit gebeurt?


Antwoord 1, autoriteit 100%

Je krijgt dit probleem omdat de functie die je aanroept een waarde retourneert, maar je negeert het resultaat.

Er zijn twee manieren om dit probleem op te lossen:

  1. Negeer het resultaat door _ =toe te voegen voor de functie-aanroep

  2. Voeg @discardableResulttoe aan de declaratie van de functie om de compiler het zwijgen op te leggen

Other episodes