函数不返回字符串

时间:2015-10-07 20:51:22

标签: swift closures completionhandler

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

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 个答案:

答案 0 :(得分:3)

退出函数后,完成开始,这就是我猜的问题。你回来""然后在完成代码中执行这些操作

答案 1 :(得分:2)

您的函数未返回值,因为fetchWithCompletion正在返回语句之后执行,因为它是异步的。您可以使用完成处理程序修改您的函数,以便在设置后访问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)
}

答案 2 :(得分: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
    }
}

答案 3 :(得分: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 ("")
    }
  }
}

以上内容可防止您的代码被阻止;也许当位置变得可用时,您只需为用户显示它?

但是,如果您要阻止。使用dispatch_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
}