【发布时间】:2014-07-23 15:27:14
【问题描述】:
为什么在 Objective-C 中我可以在块中设置实例变量:
@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];
}
但我不能在 Swift 中使用?
class ViewController: UIViewController
var times :AnyObject[]!
query.findObjectsInBackgroundWithBlock { (objects: AnyObject[]!, error: NSError!) -> Void in
self.times = objects
}
消息是: (AnyObject[]?) times = parent 无法评估:变量不可用
【问题讨论】:
-
我们需要更完整的代码。你当然可以在 Swift 的闭包中修改实例变量。理想情况下,最好能提供给我们一些代码,我们可以将其放入操场并重现错误。