Swift accessing class variable inside closures

2019-07-14 22:13发布

问题:

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"]