2016-06-09 62 views
0

如果是可选或强制更新,我需要创建某种闭包来返回。我已经创造了一些伪代码:如何创建一个闭包来返回哪一个参数为真?

func verifyAppVersionWithServer(isForceUpdate: bool -> true, isOptionalUpdate: bool -> true) { 
    //Some check will be performed here then: 
    if isForceUpdate { 
     return isForceUpdate -> true 
    } else { 
     return isOptionalUpdate -> true 
    }  
} 

我不知道如何创建一个斯威夫特关闭然后将返回的参数是真实的。

回答

1

这可能是更好的返回一个enum表示更新所需要的类型。

你会再有这样的事情:

enum UpdateType { 
    case None 
    case Optional 
    case Required 
} 

func verifyAppVersionWithServer(completion:(UpdateType) -> Void) { 

    let anyUpdate = true 
    let forcedUpdate = false 

    if anyUpdate { 
     if forcedUpdate { 
      completion(.Required) 
     } else { 
      completion(.Optional) 
     } 
    } else { 
     completion(.None) 
    } 
} 

你会称其为:

verifyAppVersionWithServer { (updateType) in 
    print("Update type is \(updateType)") 
} 

显然,值将通过您的服务器响应来决定,而不是固定的值,因为我已经证明。

+0

感谢您的回答。我实现了这个功能,但在试图调用它时声明编译器错误,指出由于分段错误11导致'Command失败。你知道这是为什么吗? – kevinabraham

+0

我不确定没有关于您如何呼叫以及哪条线路导致异常的附加信息。我的代码在一个操场上工作。 – Paulw11

+0

你的代码工作。我的错误在于结束。尝试使用'func'调用'class func',这会导致它失败。非常感谢! – kevinabraham

0

您可以使用类似下面

func verifyAppVersionWithServer(parm1: String, withParma2: Bool, completionHandeler: (isSucess: Bool, error : NSError) -> Void) { 

    //Write your logic 

    //call complition handeler 
    completionHandeler(isSucess: true, error: error) 
} 

希望这将有助于

相关问题