通过快速枚举来枚举是否安全[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
有没有更好的方法来做到这一点?看起来更好的语法明智的东西?
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) 只是想知道,它似乎工作得很好,但我想确保我使用普遍接受的编码实践,而不是养成坏习惯.
谢谢,
缺口
我以为我知道如何使用快速枚举,但有一些我不明白的事情.如果我创建三个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) 注意:请参阅预期输出与实际输出
#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) 我并不完全理解枚举有多快的细节,但比较以下两种情况:
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)
在第一个例子中,它会在每次迭代时通过整个链来获得数组吗?我应该使用第二种方式吗?
我想知道为什么通过NSArray迭代比通过NSSet迭代更快?我想象它与NSArray的订购事实有关,而NSSet不是,但我是一个经过认证的答案,而不仅仅是猜测.
编辑:
我的问题是:为什么它更快,没有在该主题中解释.而不是它更快.
我有:
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
我在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) 我有一个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) fast-enumeration ×10
objective-c ×7
nsdictionary ×3
cocoa ×2
cocoa-touch ×2
ios ×2
nsarray ×2
coding-style ×1
for-in-loop ×1
iphone ×1
key ×1
nsoperation ×1
nsset ×1
object ×1
optional ×1
swift ×1