이 요청에서 내가 겪고있는 문제는 첫 번째 함수입니다. syncRequest은 (답장, 오류)가 반환되기 전에 함수가 종료되기 때문에 항상 nil을 반환합니다.Swift 기능을 종료하기 전에 콜백이 끝날 때까지 기다리는 방법은 무엇입니까?
내 폐쇄에서 복귀하기 전에 콜백이 돌아 오기를 기다리는 방법이 있습니까?
public typealias KKWatchSyncResponse = Dictionary<String, AnyObject>
func syncRequest() -> KKWatchSyncResponse? {
var syncResponseDict : KKWatchSyncResponse?
createRequest(KKWatchRequest.Sync, parameter: nil) { reply, error in
if reply == nil || error != nil {
return
} else {
syncResponseDict = KKWatchSyncResponse()
}
if let songInfo = NSKeyedUnarchiver.unarchiveObjectWithData(reply!["songInfo"] as NSData) as NSDictionary? {
syncResponseDict!["songInfo"] = songInfo
}
if let albumArtImage = NSKeyedUnarchiver.unarchiveObjectWithData(reply!["albumArt"] as NSData) as? UIImage {
syncResponseDict!["albumArtImage"] = albumArtImage
}
if let isPlaying = NSKeyedUnarchiver.unarchiveObjectWithData(reply!["isPlaying"] as NSData) as? Bool {
syncResponseDict!["isPlaying"] = isPlaying
}
}()
return syncResponseDict
}
func createRequest(request:KKWatchRequest, parameter: KKWatchAPIRequestParameter?, callback:KKWatchAPICallback) -> KKWatchAPIParentRequest {
var requestDict : Dictionary<String, AnyObject> = [KKBOXWatchAppRequestType : request.rawValue]
if parameter != nil {
requestDict += parameter! //Combine 2 dictionaries
}
return { WKInterfaceController.openParentApplication(requestDict){ reply, error in
callback(reply, error)
}
}
}
귀하의 도움에 감사드립니다!
예! 그것은 내가 찾고있는 것입니다. 고마워요. –
굉장합니다. 문제 없습니다 ... 도움이 되니 기쁩니다. – charlierproctor