ios Swift 3.0 调用结果未使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38192751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 3.0 Result of call is unused
提问by Aryan Kashyap
I am writing in swift 3.0
我正在写 swift 3.0
i have this code which gives me the warning result of 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 classes but it still gives me the error the code for insertAnnotation is above
我曾尝试在另一个类中使用此方法,但它仍然给我错误,上面是 insertAnnotation 的代码
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??
我尝试了很多方法,但都不起作用,但在 swift 2.2 中它可以正常工作任何想法为什么会发生这种情况?
回答by David Skrundz
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:
有两种方法可以解决这个问题:
Ignore the result by adding
_ =
in front of the function callAdd
@discardableResult
to the declaration of the function to silence the compiler
通过
_ =
在函数调用前添加忽略结果添加
@discardableResult
到函数的声明中以使编译器静音