快速访问闭包中的类变量。

时间:2021-07-07 00:15:29

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

为什么在Objective-C中我可以在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

消息是:(AnyObject[]?) times = parent failed to evaluate: variable not available

1 个解决方案

#1


2  

Two things:

两件事:

  1. Swift syntax for Array declarations has changed from AnyObject[] to [AnyObject]
  2. 数组声明的Swift语法已从AnyObject[]更改为[AnyObject]
  3. If you're still in the class scope, simply use times instead of self.times.
  4. 如果您仍然在类范围内,只需使用times而不是self.times。

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

这对我在XCode6-Beta4游戏中很有用:

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):

对于闭包本身的全面测试,这同样有效(同样,在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"]

#1


2  

Two things:

两件事:

  1. Swift syntax for Array declarations has changed from AnyObject[] to [AnyObject]
  2. 数组声明的Swift语法已从AnyObject[]更改为[AnyObject]
  3. If you're still in the class scope, simply use times instead of self.times.
  4. 如果您仍然在类范围内,只需使用times而不是self.times。

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

这对我在XCode6-Beta4游戏中很有用:

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):

对于闭包本身的全面测试,这同样有效(同样,在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"]

相关文章