【发布时间】: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 中它运行良好,任何想法为什么会发生这种情况?
【问题讨论】:
-
它告诉你真相。您的函数返回
Bool,而您在调用时忽略了这一事实。如果这就是您的意思,并且您想关闭警告,请将调用更改为_ = tree!.insertAnnotation(annotation: annotation)。 -
@discardableResult stackoverflow.com/a/27261991/2303865