Swift accessing class variable inside closures

隐身守侯 提交于 2020-01-04 06:08:34

问题


Why in Objective-C I can set instance variable inside a block:

@interface CMVServices : UIViewController
@property (nonatomic, strong) NSMutableArray *times;

@implementation CMVServices
@synthesize times=_times;

and set the _times instance variable inside a block:
(some code )
.
.
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[_times addObjectsFromArray:objects];
}

but I can't in Swift?

class ViewController: UIViewController 
var times :AnyObject[]!

query.findObjectsInBackgroundWithBlock { (objects: AnyObject[]!, error: NSError!) -> Void in
 self.times = objects
}

The message is: (AnyObject[]?) times = parent failed to evaluate: variable not available


回答1:


Two things:

  1. Swift syntax for Array declarations has changed from AnyObject[] to [AnyObject]
  2. If you're still in the class scope, simply use times instead of self.times.

This works for me in XCode6-Beta4 in a Playground file:

import UIKit

class CMVServices : UIViewController {
    var times : [AnyObject] = []

    func testFunc() {
        func findObjects(objects : [AnyObject]!, error : NSError!) {
            times = objects
            var test_str = "still evaluating in Playground here"
        }
        findObjects(["test", "string"], nil)
    }
}

var test = CMVServices()

test.testFunc()

and for full-on test of closures themselves, this also works (again, in XCode6-Beta4):

import UIKit

class CMVServices : UIViewController {
    var times: [AnyObject] = []

    func testClosure() {
        { (objects : [AnyObject]!, error : NSError!) -> Void in
            self.times = objects
            NSLog("still evaluating")
        }(["new", "test", "string"], nil)
    }
}

var test = CMVServices()

test.testClosure()
test.times // ["new", "test", "string"]


来源:https://stackoverflow.com/questions/24914670/swift-accessing-class-variable-inside-closures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!