0

所以我要重新开始编程,但我遇到了一个问题。当我在其中存储值时,我的函数没有返回值。你们能看看并指出我为什么会这样吗?

func getLocation() -> NSString {
    manager = OneShotLocationManager()
    var tempLocation: NSString = "" // created an empty string for the var

    manager!.fetchWithCompletion {location, error in
        if let locatie = location {
            tempLocation = String(locatie.coordinate.latitude) + "," + String(locatie.coordinate.longitude)
            print(tempLocation) // It stores a value here but will not show it on the return
        } else if let err = error {
            tempLocation = err.localizedDescription
        }
        self.manager = nil
    }
    return tempLocation // It's not returning anything here.. 
}
4

4 回答 4

2

您的函数没有返回值,因为fetchWithCompletion它是在 return 语句之后执行的,因为它是异步的。您可以通过使用完成处理程序来修改您的函数,以便tempLocation在设置后访问:

func getLocation(completion: (location: String) -> ())  {
    manager = OneShotLocationManager()
    var tempLocation: NSString = "" // created an empty string for the var

    manager!.fetchWithCompletion {location, error in
        if let locatie = location {
            tempLocation = String(locatie.coordinate.latitude) + "," + String(locatie.coordinate.longitude)
            print(tempLocation) // It stores a value here but will not show it on the return
        } else if let err = error {
            tempLocation = err.localizedDescription
        }
        self.manager = nil
        completion(location: tempLocation)
    }
}

您可以通过以下方式实现此功能:

getLocation { (location) -> () in
    print(location)
}
于 2015-10-08T07:04:57.890 回答
2

退出函数后完成开始,所以这就是我猜的问题。您返回“”,然后在完成代码中执行这些操作

于 2015-10-07T21:03:01.850 回答
0

您需要调用 getLocation 并在 fetchWithCompletion 闭包中执行您的操作

func getLocation() {
    manager = OneShotLocationManager()

    manager!.fetchWithCompletion {location, error in
        if let locatie = location {
            tempLocation = String(locatie.coordinate.latitude) + "," + String(locatie.coordinate.longitude)

            //DO THE THINGS YOU NEED TO DO WITH THE LOCATION HERE

        } else if let err = error {
            tempLocation = err.localizedDescription
        }
        self.manager = nil
    }
}
于 2015-10-07T21:56:19.730 回答
0

提供给的闭包fetchWithCompletion()被异步调用 - 在管理器的位置获取完成后。您的函数启动管理器获取,然后在管理器获取完成之前返回;因此最初的赋值tempLocation是返回为""

由于获取是异步的,也许您getLocation()也应该是异步的?

func getLocationAsynch (handler: (String) -> Void) {
  OneShotLocationManager()!.fetchWithCompletion { location, error in
    if let locatie = location {
      handler (String(locatie.coordinate.latitude) + "," + String(locatie.coordinate.longitude))
    } else if let err = error {
      handler (err.localizedDescription)
    } else {
      handler ("")
    }
  }
}

以上可以防止您的代码阻塞;也许当该位置可用时,您只需将其显示给用户?

但是,如果您想阻止。使用 adispatch_semaphore作为:

// assuming dispatch_semaphore works in Swift
func getLocation () -> String {
  let sem = dispatch_semaphore_create (0);
  var result = ""

  OneShotLocationManager()!.fetchWithCompletion { location, error in
    if let locatie = location {
      result = String(locatie.coordinate.latitude) + "," + String(locatie.coordinate.longitude)
    } else if let err = error {
      result = err.localizedDescription
    } 
    dispatch_semaphore_signal(sem)
  }

  dispatch_semaphore_wait(sem)
  return result
}
于 2015-10-07T22:40:23.860 回答