Tag:

把价值传递给closures?

我想在处理最后一个项目之后做额外的逻辑,但是terminal显示i总是和c一样的值。 任何想法如何传递循环variables? let c = a.count for var i=0; i<c; i++ { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { // .. dispatch_async(dispatch_get_main_queue(), { println("i \(i) c \(c)") if i == c-1 { // extra stuff would come here } }) }) }

我如何创build一个全局UIManagedDocument实例每个文件在磁盘上由我的整个应用程序使用块共享?

我想devise一个帮助器方法,它将检索一个UIManagedDocument,然后打开并返回它,以便我可以从我的应用程序中的几个地方访问相同的UIManagedDocument。 我对这种asynchronous性质有困难,因为我对块不太熟悉。 理想情况下,事件的顺序是这样的: X类调用帮助器方法来检索UIManagedDocument,并包含一个代码块,以在打开的文档返回时运行。 类方法检索UIManagedDocument,并根据需要调用openWithCompletionHandler或saveToURL,并包含一个在打开的文档返回时运行的代码块。 openwithCompletionHandler或saveToURL完成他们的任务,并返回成功=是,并在其代码块中运行代码 类方法完成其任务,并返回一个打开的UIManagedDocument并在其代码块中运行代码 我可以通过原始块通过某种方式吗? 这是我的代码到目前为止。 任何想法非常感激,谢谢。 // This is a dictionary where the keys are "Vacations" and the objects are URLs to UIManagedDocuments static NSMutableDictionary *managedDocumentDictionary = nil; // This typedef has been defined in .h file: // typedef void (^completion_block_t)(UIManagedDocument *vacation); // The idea is that this class method will […]

iOS在Objective-C中构build和返回对象的这种方法的名称

我试图找出这种编码风格是什么,它是一个内联块? 内联范围? 什么? 当遇到这些问题时,编译器会创build什么… – (UIView *)createMyView { return ({ UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)]; /* set some stuff up on the view; */ view; }); } 我问,因为我们在崩溃日志中获得了很多cxx_destruct调用,其行数大于文件的实际大小。 我想知道这种编码方式是否增加了一些奇怪的东西,它的方式。

引用嵌套块中的弱自我

假设我已经创build了一个弱自我使用 __weak typeof(self) weakSelf = self; [self doABlockOperation:^{ … }]; 在那个块里面,如果我嵌套另一个块: [weakSelf doAnotherBlockOperation:^{ [weakSelf doSomething]; } 它会创build一个保留周期吗? 我是否需要为弱者创造另一个弱点? __weak typeof(self) weakerSelf = weakSelf; [weakSelf doAnotherBlockOperation:^{ [weakerSelf doSomething]; }

从块内部返回方法对象

我想知道如何正确地做到以下几点:我有一个方法是返回一个NSData对象。 它从UIDocument获取NSData对象。 NSData对象可以变大,所以我想确保它在响应开始之前完全加载。 因此,我想从块内部返回方法的值。 所以像这样的东西: – (NSData*)getMyData { MyUIDocument *doc = [[MyUIDocument alloc] initWithFileURL:fileURL]; [doc openWithCompletionHandler:^(BOOL success) { if (success) { return doc.myResponseData; // this is to be the return for the method not the block } }]; } 这会导致错误,因为return显然是指block的return 。 我怎样才能做到这一点,而不必做一个线程阻塞等待/ while循环? 谢谢。