Swift中的use块给出了错误“变量在其自身初始值内使用”。 [英] Use block in Swift giving error "Variable used within its own initial value"

查看:137
本文介绍了Swift中的use块给出了错误“变量在其自身初始值内使用”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在obj-c中的代码:

This is my code in obj-c:

__block NSString *requestReference = [self operation:method url:url parameters:parameters headers:headers success:^(NSURLSessionDataTask *task, id responseObject) {
NSError *error = [NSError errorWithSessionTask:task responseObject:responseObject];
if (error) {
    NSLog(@"error - %@", error);
    [delegate requestWithReference:requestReference didFinishWithBusinessError:error];
} else {
    id responseModel;
    if (modelClass && responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            // if response is a dictionary, create model out of it
            responseModel = [modelClass objectFromDictionary:responseObject error:&error];
        } else if ([responseObject isKindOfClass:[NSArray class]]) {
        }
    }
} } failure:^(NSURLSessionDataTask *task, NSError *error) {
[delegate requestWithReference:requestReference didFailWithError:error]; }];

这是在Swift中转换后的代码:

and this is the code after conversion in Swift:

var requestReference = self.operation(method, url: url, parameters: parameters, headers: headers, success: {(_ task: URLSessionDataTask, _ responseObject: Any) -> Void in
    var error = Error(sessionTask: task, responseObject: responseObject)
    if error {
        print("error - \(error)")
        delegate.request(with: requestReference, didFinishWithBusinessError: error)
    }
    else {
        var responseModel: Any!
        if modelClass && responseObject {
            if (responseObject is [AnyHashable: Any]) {
                // if response is a dictionary, create model out of it
                do {
                    responseModel = try modelClass.object(fromDictionary: responseObject)
                }
                catch {
                }
            }
            else if (responseObject is [Any]) {

            }
        }
    }
}, failure: {(_ task: URLSessionDataTask, _ error: Error) -> Void in
    delegate.request(with: requestReference, didFailWithError: error)
})

我已经转换并提出了这段代码。
一切都在此代码中运行
但我遇到了错误:

I have converted and come up with this code. everything is working in this code But I'm getting error:


变量在其自身的首字母内使用value

"Variable used within its own initial value"


推荐答案

Swift编译器非常严格,并检查每个变量是否具有$ b $在使用之前定义的值。编译器
不知道在您的情况下,闭包将仅在定义 requestReference 变量之后的
之后执行。 。

The Swift compiler is very strict and checks that every variable has a defined value before it is used. The compiler does not know that in your case, the closures will be executed only after the requestReference variable is defined.

在这种情况下,您可以使用隐式解包可选:

In such cases you can use an implicitly unwrapped optional:

var requestReference: String! 
requestReference = self.operation(..., success: {(_ task: URLSessionDataTask, _ responseObject: Any) -> Void in
    // ... 
    delegate.request(with: requestReference, didFinishWithBusinessError: error)
}, failure: {(_ task: URLSessionDataTask, _ error: Error) -> Void in
    delegate.request(with: requestReference, didFailWithError: error)
})

一个隐式展开的可选内容是对编译器的承诺:
变量没有值现在,但使用时将具有值。

An implicitly unwrapped optional is a promise to the compiler: The variable has no value now, but it will have a value when it is used.

这篇关于Swift中的use块给出了错误“变量在其自身初始值内使用”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆