标签: fast-enumeration

通过[NSOperationQueue操作]枚举是否安全?

通过快速枚举来枚举是否安全[NSOperationQueue operations]?像这样:

for (NSOperation *op in [operationQueue operations]) {
    // Do something with op
}
Run Code Online (Sandbox Code Playgroud)

由于操作是异步的并且在另一个线程上执行,operations因此可以随时更改(包括在主线程执行期间).快速枚举是否可以防止这种情况,或者我应该copy(和autorelease)操作数组?

cocoa cocoa-touch nsoperation fast-enumeration nsoperationqueue

5
推荐指数
1
解决办法
589
查看次数

迅速列举的期权

有没有更好的方法来做到这一点?看起来更好的语法明智的东西?

let a : [Any] = [5,"a",6]
for item in a { 
  if let assumedItem = item as? Int {
     print(assumedItem) 
  } 
}
Run Code Online (Sandbox Code Playgroud)

这样的东西,然后用正确的语法?

  for let item in a as? Int { print(item) }
Run Code Online (Sandbox Code Playgroud)

optional fast-enumeration swift

5
推荐指数
2
解决办法
4286
查看次数

快速查点被认为是不良形式还是被普遍接受?

只是想知道,它似乎工作得很好,但我想确保我使用普遍接受的编码实践,而不是养成坏习惯.

谢谢,

缺口

coding-style objective-c fast-enumeration

4
推荐指数
1
解决办法
185
查看次数

为什么在指定NSStrings时快速枚举不会跳过NSNumbers?

我以为我知道如何使用快速枚举,但有一些我不明白的事情.如果我创建三个NSString对象和三个NSNumber对象并将它们放在NSMutableArray:

NSString *str1 = @"str1";
NSString *str2 = @"str2";
NSString *str3 = @"str3";

NSNumber *nb1 = [NSNumber numberWithInt:1];
NSNumber *nb2 = [NSNumber numberWithInt:2];
NSNumber *nb3 = [NSNumber numberWithInt:3];

NSArray *array = [[NSArray alloc] initWithObjects:str1, str2, str3, nb1, nb2, nb3, nil];
Run Code Online (Sandbox Code Playgroud)

然后我对所有NSString对象进行快速枚举,如下所示:

for (NSString *str in array) {
    NSLog(@"str : %@", str);
}
Run Code Online (Sandbox Code Playgroud)

在控制台中,我得到了这个结果:

2011-08-02 13:53:12.873 FastEnumeration[14172:b603] str : str1
2011-08-02 13:53:12.874 FastEnumeration[14172:b603] str : str2
2011-08-02 13:53:12.875 FastEnumeration[14172:b603] str : str3
2011-08-02 …
Run Code Online (Sandbox Code Playgroud)

objective-c nsarray fast-enumeration

4
推荐指数
3
解决办法
2372
查看次数

按键排序的NSDictionary实例的快速枚举

概观

  • 我正在使用快速枚举来遍历NSDictionary实例
  • 我希望根据密钥的升序来枚举NSDictionary实例,但似乎并非如此

我想做的事:

  • 我希望能够使用快速枚举以密钥的升序迭代NSDictionary实例

注意:请参阅预期输出与实际输出

问题

  1. 我的实施错误了吗?
  2. NSDictionary的快速枚举是否保证基于密钥的排序?
  3. 如果没有,那么有没有解决这个问题,但使用快速枚举?

#import<Foundation/Foundation.h>

int main()
{
    system("clear");

    NSDictionary *d1 = nil;

    @autoreleasepool
    {   

        d1 = [[NSDictionary alloc] initWithObjectsAndKeys: @"AAA", [NSNumber numberWithInt:10], 
              @"BBB", [NSNumber numberWithInt:20],               
              @"CCC", [NSNumber numberWithInt:30],               
              nil];
    }   

    for(NSNumber* n1 in d1)     //I expected fast enumeration for NSDictionary to be based on the 
        //ascending order of the key but that doesn't seem to be the case
    {
        printf("key = %p"
               "\t [key intValue] = %i"
               "\t value = %s\n", …
Run Code Online (Sandbox Code Playgroud)

objective-c nsdictionary fast-enumeration

4
推荐指数
1
解决办法
8605
查看次数

这是使用快速枚举的低效方法吗?

我并不完全理解枚举有多快的细节,但比较以下两种情况:

for(NSObject *object in self.myParent.parentsParents.granfathersMother.cousin.unclesNephew.array) {
    // do something
}
Run Code Online (Sandbox Code Playgroud)

NSArray *array = self.myParent.parentsParents.granfathersMother.cousin.unclesNephew.array;
for(NSObject *object in array) {
     // do something
 }
Run Code Online (Sandbox Code Playgroud)

在第一个例子中,它会在每次迭代时通过整个链来获得数组吗?我应该使用第二种方式吗?

objective-c fast-enumeration ios

4
推荐指数
1
解决办法
387
查看次数

为什么迭代NSArray要比通过NSSet迭代更快?

我想知道为什么通过NSArray迭代比通过NSSet迭代更快?我想象它与NSArray的订购事实有关,而NSSet不是,但我是一个经过认证的答案,而不仅仅是猜测.

编辑:

我的问题是:为什么它更快,没有在该主题中解释.而不是它更快.

objective-c nsarray nsset fast-enumeration

4
推荐指数
2
解决办法
3899
查看次数

为什么循环变量在循环后变为`nil`

我有:

NSDictionary* server;

for (server in self.servers)
{
    if (<some criterium>)
    {
        break;
    }
}

// If the criterium was never true, I want to use the last item in the
// the array. But turns out that `server` is `nil`.
Run Code Online (Sandbox Code Playgroud)

循环块永远不会改变server.

servers是一个NSMutableArray词典,一个在循环中没有改变的属性.

为什么循环结束后server有值nil

这是我第一次在循环后使用这样的变量.没有想太多,我认为它会像(在旧的C天)一样工作:

int i;
for (i = 0; i < n; i++)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

cocoa objective-c nsdictionary fast-enumeration nsfastenumeration

4
推荐指数
2
解决办法
160
查看次数

NSDictionary上的快速枚举失败,"[Waypoint countByEnumeratingWithState:objects:count:]:无法识别的选择器发送到实例..."

我在NSDictionary对象中有我的数据,其中键是CGPoints转换为NSValues,对象是UIColors.这是我用来从字典中返回一个对象的方法:

- (UIColor*) getTemperatureColor2 {
    NSDictionary* temperatureColorMap = [Weather getTemperatureColorMap];   

    for(id key in temperatureColorMap) {
        CGPoint point = [key CGPointValue];
        if ( (int)roundf(self.temperature_celsius) >= (int)roundf(point.x)  ) { 
            if ( (int) roundf(self.temperature_celsius) <= (int) roundf(point.y) ) {
                return [temperatureColorMap objectForKey:key];
            }
        }       
    }

    return [UIColor blackColor];    
}
Run Code Online (Sandbox Code Playgroud)

这是getTemperatureColorMap方法,在同一个类(Weather)中实现:

+ (NSDictionary*) getTemperatureColorMap {
    static NSDictionary* temperatureColorMap = nil;

    if (temperatureColorMap == nil) {
        temperatureColorMap = [[[NSDictionary alloc] initWithObjectsAndKeys:
                            RGB2UIColor(0x0E09EE), [NSValue valueWithCGPoint: CGPointMake(-99, -8)],
                            RGB2UIColor(0xB85FC), [NSValue valueWithCGPoint:  CGPointMake(-7, -3) ],
                            RGB2UIColor(0x0BDCFC), [NSValue valueWithCGPoint: …
Run Code Online (Sandbox Code Playgroud)

iphone key object nsdictionary fast-enumeration

3
推荐指数
1
解决办法
6802
查看次数

枚举NSMutableDictionary - 无法从循环内访问对象属性

我有一个NSMutableDictionary,analyzedPxDictionary包含一堆Pixel对象(我创建的自定义类).除此之外,Pixel对象包含一个名为NSArray的属性rgb.该数组将始终包含三个NSNumber对象,其整数值对应于像素的rgb值.

我现在试图枚举analyzedPxDictionary使用快速枚举.但是,似乎我无法从循环内访问Pixel对象的属性.我声明rgb是一个合成属性,以便我可以使用点语法访问它.但是当我尝试从循环中执行此操作时,程序崩溃,给出了如下错误:

'-[NSCFString rgb]: unrecognized selector sent to instance 0xa90bb50'

以下是产生该错误的代码示例:

for (Pixel *px in analyzedPxDictionary) {
    printf("r: %i, g: %i, b: %i",[[px.rgb objectAtIndex:0] integerValue], [[px.rgb objectAtIndex:1] integerValue], [[px.rgb objectAtIndex:2] integerValue]);
}
Run Code Online (Sandbox Code Playgroud)

我试过在那条printf线上设置一个断点来检查px.虽然rgb如果其属性被列为一个并且正确描述为NSArray的实例,但它似乎不包含任何对象.

我相信我rgb正确地初始化.要解释一下,请考虑以下代码:

NSString *key;
for (Pixel *px in analyzedPxDictionary) {
    key = [px description];
}

Pixel *px = [analyzedPxDictionary objectForKey:key];
printf("\nr: %i, g: %i, b: %i",[[px.rgb objectAtIndex:0] integerValue], [[px.rgb objectAtIndex:1] integerValue], [[px.rgb …
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c fast-enumeration for-in-loop ios

3
推荐指数
1
解决办法
258
查看次数