fast-enumeration

Does fast enumeration in Objective-C guarantee the order of iteration?

荒凉一梦 提交于 2019-11-27 15:46:59
问题 Can I expect it to go from the start of an array to the end in order? Can't find anything in the docs about this. i.e. is for (id val in array) { NSLog(@"%@", val); } always going to print out the same as for (int i = 0; i < [array count]; ++i) { NSLog(@"%@", [array objectAtIndex:i]); } 回答1: From Apples' Objective-C documentation on fast enumeration: For collections or enumerators that have a well-defined order—such as NSArray or NSEnumerator instance derived from an array—the enumeration

Fast Enumeration Vs NSEnumerator in Objective-C

南笙酒味 提交于 2019-11-27 02:02:07
问题 I have seen this over and over, why exactly is it faster to use fast enumeration in loops rather than an NSEnumerator using nextObject: . 回答1: NSEnumerator is the old way to enumerate over collections. It involves creating an object to represent the enumeration, then calling a method on it for every single iteration. While this was perfectly serviceable for many years, it's not terribly efficient, as it involves at least one message send for every iteration of the loop. NSFastEnumeration is

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

谁说胖子不能爱 提交于 2019-11-26 21:34:30
I've been using enumerateObjectsUsingBlock: a lot lately for my fast-enumeration needs, and I'm having a hard time understanding the usage of BOOL *stop in the enumeration block. The NSArray class reference states stop : A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block. So then of course I can add the following in my block to stop the enumeration: if (idx == [myArray indexOfObject:[myArray lastObject]]) { *stop = YES; } From what I

Loop through subview to check for empty UITextField - Swift

隐身守侯 提交于 2019-11-26 19:52:02
问题 I"m wondering how to essentially transform the objective c code below into swift. This will loop through all the subviews on my desired view, check if they are textfields, and then check if they are empty of not. for (UIView *view in contentVw.subviews) { NSLog(@"%@", view); if ([view isKindOfClass:[UITextField class]]) { UITextField *textfield = (UITextField *)view; if (([textfield.text isEqualToString:""])) { //show error return; } } } Here is where i am with swift translation so far: for

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

前提是你 提交于 2019-11-26 08:01:56
问题 I\'ve been using enumerateObjectsUsingBlock: a lot lately for my fast-enumeration needs, and I\'m having a hard time understanding the usage of BOOL *stop in the enumeration block. The NSArray class reference states stop : A reference to a Boolean value. The block can set the value to YES to stop further processing of the array. The stop argument is an out-only argument. You should only ever set this Boolean to YES within the Block. So then of course I can add the following in my block to