Questions tagged [fast-enumeration]

An Objective-C language feature which offers more concise enumeration code with better performance than other options (i.e. NSEnumerator)

Instead of using an NSEnumerator object or indices to iterate through a collection, Objective-C 2.0 offers the fast enumeration syntax. In Objective-C 2.0, the following loops are functionally equivalent, but have different performance characteristics.

// Using NSEnumerator
NSEnumerator *enumerator = [thePeople objectEnumerator];
Person *p; 
while ((p = [enumerator nextObject]) != nil) {
 NSLog(@"%@ is %i years old.", [p name], [p age]);

// Using fast enumeration
for (Person *p in thePeople) {
 NSLog(@"%@ is %i years old.", [p name], [p age]);
}

Fast enumeration generates more efficient code than standard enumeration because method calls to enumerate over objects are replaced by pointer arithmetic using the NSFastEnumeration protocol

Reference:
http://en.wikipedia.org/wiki/Objective-C#Fast_enumeration

135 questions
0
votes
2 answers

Getting runtime error when doing fast enumeration over an NSArray of entities from Core Data in iOS

I have an NSArray of Objects of type NSManagedObject that are returned from a fetch result I perform in Core Data. The NSArray contains objects, as I am able to verify this after the query by printing the contents of the NSArray to the console. My…
syedfa
  • 2,801
  • 1
  • 41
  • 74
0
votes
3 answers

Is it safe to perform save operations on an NSManagedContext object when using Objective-C fast enumeration

I was wondering if this sort of code would be ok to use NSManagedObjectContext *moc = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext]; for (NSDictionary *dict in objects) { Object *object =…
0
votes
0 answers

Can I fast enum @property(ies)

I was just wondering if I could do some thing like for (id something in someArray) for properties. For instance, I declared a bunch of properties for a class A and two objects A,B of this class. I want to go through each one of the properties of…
Haoyu
  • 49
  • 1
  • 7
0
votes
3 answers

How to change this loop on Fast Enumeration

I want to execute my For loop faster but i dont have idea about Fast Enumeration. Can any one suggest me how to change this For loop in fast enumeration. NSString *strCorrectWord; for(i=0;i<[self.strCorrectWord length];i++) { }
0
votes
3 answers

Why set types in Obj-c fast enumeration loops?

NSMutableArray *array = [[NSMutableArray alloc] init]; NSString *string = @"string"; [array addObject:string]; NSDate *date = [[NSDate alloc] init]; [array addObject:date]; for (*placeholder* stuff in array) NSLog(@"one"); If I change…
hollow7
  • 1,506
  • 1
  • 12
  • 20
0
votes
1 answer

Vectorization of Matlab Code involving ODE solver at each iteration

I want to write a fast MATLAB code where I need to write a for loop and I need to solve an ordinary differential equation each time.Is there any way to vectorize the code? Following is the part of the code: tspan=0:0.01:20; …
0
votes
1 answer

Objective-C Fast Enumeration Search Doesn't Break

I'm trying to find a matching object in a tree, so I'm using ObjC fast enumeration. The problem is my method finds the matching value, hits the return line, and then sets the value to nil and keeps on iterating. Here's my method: + (INONode…
Aaron Vegh
  • 5,217
  • 7
  • 48
  • 75
0
votes
3 answers

How to recognize the first element in Objective-C style enumeration?

I have an NSMutableArray of NSNumbers, I want to enumerate through all of them with Objective-C styled enumeration. Here's what I've done so far. for ( NSNumber* number in array ) { //some code } I want to be able to recognize the first object…
Shane Hsu
  • 7,937
  • 6
  • 39
  • 63
0
votes
4 answers

Need to modify an NSMutableArray that is pre-loaded with data during fast enumeration in iOS

I have an NSMutableArray that is holding a list of objects. What I am trying to do is iterate through this list of objects, and find the matching object for the I am trying to insert. Once I find the matching object, I then want to simply replace…
syedfa
  • 2,801
  • 1
  • 41
  • 74
0
votes
1 answer

Subclassing JavaScript Array breaks fast enumeration

I'm subclassing Array using prototype, in this way: MyArray = function() { Array.apply(this, arguments); }; MyArray.prototype = new Array(); All works fine as expected and I can initialize a "MyArray" instance in this way: var arr = new…
daveoncode
  • 18,900
  • 15
  • 104
  • 159
0
votes
3 answers

improve fast enumeration performance

I have a huge word list of over 280.000+ words that is loaded from an sqlite database to an NSArray. then I do a fast enumeration to check if a certain string value entered by the user matches one of the words in the Array. Since the array is so…
Ramin Afshar
  • 989
  • 2
  • 18
  • 34
0
votes
2 answers

Traversing two-dimensional array, using for-in syntax

I'm traversing through a two-dimensional array like this: for (menuViewController *aSelection in mainDataArray) { ... } However, how do I access the lower arrays' data? This following code doesn't work, but gives you the idea of what I…
Custom Bonbons
  • 1,609
  • 2
  • 12
  • 17
0
votes
1 answer

iOS - Core Data - Delete records using relationships and fetch request

Overview: I have an iOS project in which I am using core data I have an Employees entity and a Department entity. 1 department can contain many employees So the entity Department has a "to many" relationship with the entity Employees, the…
user1046037
  • 16,755
  • 12
  • 92
  • 138
0
votes
1 answer

checking user input via textfield

I've been struggling with this for quite a few days now; my app has a diagram with uitextfields to represent labelling of the picture. I would like to check the user input against a dictionary (for the answer) and if it is correct, increase the…
Rob W
  • 591
  • 1
  • 7
  • 22
0
votes
3 answers

How to get strings with specific length from NSArray?

I want to get strings that have specific length in NSArray. The array has many elements and I don't want to use fast enumeration. Is there a possible way?
Jun
  • 3,422
  • 3
  • 28
  • 58
1 2 3
8
9