块中使用的NSMutableArray实例 [英] NSMutableArray instance used in a block

查看:67
本文介绍了块中使用的NSMutableArray实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段使用GCD计算一组整数并将其存储在数组中.

The following code snippet uses GCD to compute a set of integers and store them in an array.

factArray = {1 !, 2 !, ... n!}其中k!表示阶乘(k)= k *(k-1)* ... * 2 * 1.

factArray = {1!, 2!, ... n!} where k! designates factorial(k)=k*(k-1)*...*2*1.

我想知道为什么不使用__block限定符声明factArray,却可以向该块内的factArray变量(NSMutableArray的实例)添加对象.

I wonder why I can add objects to the factArray variable (instance of NSMutableArray) inside the block though factArray isn't declared with the __block qualifier.

NSUInteger n = 10;
NSMutableArray *factArray = [[NSMutableArray alloc] initWithCapacity:n];
__block NSUInteger temp = 1;
dispatch_apply(n,
               dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
               ^(size_t s) {
                   NSUInteger w;
                   temp *= floor(s) + 1;
                   w = temp;
                   [factArray addObject:@(w)];
               });

__block NSArray *sortedArray;
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    sortedArray = [factArray sortedArrayUsingComparator:^NSComparisonResult(NSNumber *n1, NSNumber *n2) {
        return [n1 compare:n2];
    }];
    NSLog(@"%@", sortedArray);
});

当然,如果我们添加__block限定符,此代码仍然有效.

Of course, this code still works if we add the __block qualifier.

推荐答案

factArray是变量,例如temp.这是一个指针;它保存其他内容的内存地址.块获得该指针的副本,就像它获得temp的副本一样.但是,指向的对象不会被复制.它留在同一位置,并保留为NSMutableArray.您可以通过指针向它发送消息.不会改变指针的值.它仍然指向同一位置,并且可以在不更改指针的情况下修改对象的内容.

factArray is a variable, like temp. It's a pointer; it holds the memory address of something else. The Block gets a copy of that pointer, just like it gets a copy of temp. The object that's pointed to, though, isn't copied. It stays in the same place, and remains an NSMutableArray. You can send messages to it through the pointer; that doesn't change the pointer's value. It still points to the same place, and the contents of an object can be modified without changing the pointer.

如果要摆脱该现有数组并制作一个新数组,然后调用该数组factArray:

If you wanted to get rid of that existing array and make a new one, and call that array factArray:

dispatch_apply(n, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
               ^(size_t s) {
                   factArray = [NSMutableArray array];
});

然后,您将更改指针的值,然后 then ,您将需要__block说明符.

then you would be changing the pointer's value, and then you would need the __block specifier.

通俗地说,由于ObjC对象只能通过指针进行访问,因此我们通常将指针称为对象.这种区别在很大程度上是不重要的.这是它的时代之一.

Colloquially, since ObjC objects can only be accessed via pointers, we often refer to the pointer as if it were the object. The distinction is largely unimportant; this is one of the times it is.

这篇关于块中使用的NSMutableArray实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆