最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[无法解决]swift代码出错:Cannot convert value of type Result JSON NSError to expected argument type AnyObject

iOS crifan 3552浏览 0评论

代码:

        getUrlRespDataJson_async(

            .POST,

            url: ServerApi.registerUrl(),

            parameters: [

                "dealer"    : dealer,

                "phone"     : phone,

                "password"  : password,

                "code"      : code,

            ],

            respJsonHandler: { respDataJson in

                gLog.info("respDataJson=\(respDataJson)")

                NSNotificationCenter.defaultCenter().postNotificationName(kRegisterComplete, object: respDataJson)

        })

func getUrlRespDataJson_async(httpMethod:Alamofire.Method, url:String, parameters: [String : AnyObject]? = nil, headers: [String : String]? = nil, respJsonHandler: (Alamofire.Result<JSON, NSError>) -> Void) {

}

public enum Result<Value, Error : ErrorType> {

    case Success(Value)

    case Failure(Error)

    /// Returns `true` if the result is a success, `false` otherwise.

    public var isSuccess: Bool { get }

    /// Returns `true` if the result is a failure, `false` otherwise.

    public var isFailure: Bool { get }

    /// Returns the associated value if the result is a success, `nil` otherwise.

    public var value: Value? { get }

    /// Returns the associated error value if the result is a failure, `nil` otherwise.

    public var error: Error? { get }

}

出错:

Cannot convert value of type ‘Result<JSON, NSError>’ to expected argument type ‘AnyObject?’

即,想要把:

Result<JSON, NSError>

当做AnyObject的类型的参数传递,结果不支持。

换成:

                NSNotificationCenter.defaultCenter().postNotificationName(kRegisterComplete, object: nil, userInfo: ["respDataJson" : respDataJson])

也不行,同样错误。

Cannot convert value of type ‘Result<JSON, NSError>’ to expected argument type ‘AnyObject?’

ios – Cannot convert value of type ‘JSON’ to expected argument type ‘JSON’ – Stack Overflow

xcode – cannot convert value of type, swift 2.0, error handling – Stack Overflow

ios – Swift 1.2 to swift 2: Cannot convert value of type to expected argument type – Stack Overflow

swift – Cannot convert value JSON to AnyObject in Alamofire and Sync – Stack Overflow

ios – Cannot subsript a value of type anyobject in swift 2 – Stack Overflow

swift – cannot convert value of type NSMutableDictionary to expected argument type ‘[NSObject:AnyObject]?’ – Stack Overflow

还是找不到合适的写法。

ios – Swift 2 Cannot convert value of type ‘[AnyObject]’ to expected argument type ‘[UIViewController]?’ – Stack Overflow

Cannot convert value of type ‘String?!’ to expected argument type ‘Notifications’ – yuluer.com

Cannot convert value of type Result<JSON, NSError> AnyObject

Cannot convert value of type JSON, NSError to AnyObject

Swift AFNetworking POST error (Swift) – Codedump.io

Cannot convert  type tuple to AnyObject

How to convert Tuple to AnyObject in Swift – Stack Overflow

答案是:

没法把tuple类型变量转换为AnyObject

-》除非用workaround,借用新建一个类,成员变量是原先的tuple

-》就搞得太复杂了。。。

-》不这样弄了。

ios – Pass Tuples as AnyObject in swift – Stack Overflow

-》只能自己想办法:

把tuple,转换为dict去传递。。。

[总结]

swift和oc中,都不支持把:

Result<JSON, NSError>

类型的变量,即tuple类型的变量,作为AnyObject去传递值。

-》所以此处只能放弃此种用法,该放到新建类,或者改为dictionary的方式去传递值了。

转载请注明:在路上 » [无法解决]swift代码出错:Cannot convert value of type Result JSON NSError to expected argument type AnyObject

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.158 seconds, using 22.08MB memory