fast-enumeration

How for in loop works internally - Objective C - Foundation

流过昼夜 提交于 2019-11-30 20:58:05
I found this answer: https://stackoverflow.com/a/5163334/1364174 Which presents how for in loop is implemented. NSFastEnumerationState __enumState = {0}; id __objects[MAX_STACKBUFF_SIZE]; NSUInteger __count; while ((__count = [myArray countByEnumeratingWithState:&__enumState objects:__objects count:MAX_STACKBUFF_SIZE]) > 0) { for (NSUInteger i = 0; i < __count; i++) { id obj = __objects[i]; [obj doSomething]; } } The problem is that, I found it wrong. First of all, when you have Automatic Reference Counting (ARC) turned on, you got an error Sending '__strong id *' to parameter of type '_

With fast enumeration and an NSDictionary, iterating in the order of the keys is not guaranteed – how can I make it so it IS in order?

佐手、 提交于 2019-11-30 11:42:29
I'm communicating with an API that sends back an NSDictionary as a response with data my app needs (the data is basically a feed). This data is sorted by newest to oldest, with the newest items at the front of the NSDictionary. When I fast enumerate through them with for (NSString *key in articles) { ... } the order is seemingly random, and thus the order I operate on them isn't in order from newest to oldest, like I want it to be, but completely random instead. I've read up, and when using fast enumeration with NSDictionary it is not guaranteed to iterate in order through the array. However,

Fastest way to check if an array contains the same objects of another array

◇◆丶佛笑我妖孽 提交于 2019-11-30 04:38:43
The goal is to compare two arrays as and check if they contain the same objects (as fast as possible - there are lots of objects in the arrays). The arrays cannot be checked with isEqual: as they are differently sorted. I already tried the solution posted here ( https://stackoverflow.com/a/1138417 - see last code snippet of the post by Peter Hosey). But this doesn't work with differently sorted arrays. The code I'm using now is the following: + (BOOL)arraysContainSameObjects:(NSArray *)array1 andOtherArray:(NSArray *)array2 { // quit if array count is different if ([array1 count] != [array2

Core Data/NSOperation: crash while enumerating through and deleting objects

纵然是瞬间 提交于 2019-11-30 03:32:31
问题 I have a core data based app that has a one object (a list) to many objects (list items) relationship. I'm working on syncing data between devices, and as part of that I import lists from XML files in background threads (via an NSOperation subclass). When I'm updating an existing list, I delete all of its old list items (from the NSManagedObjectContext specific to that thread) and replace them with new ones from the XML file... the delete is handled by enumerating through the items for that

Keep track of index in fast enumeration

淺唱寂寞╮ 提交于 2019-11-30 00:37:05
I want to get the index of the current object when using fast enumeration, i.e. for (MyClass *entry in savedArray) { // What is the index of |entry| in |savedArray|? } Look at the API for NSArray and you will see the method - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block So give that one a try [savedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { //... Do your usual stuff here obj // This is the current object idx // This is the index of the current object stop // Set this to true if you want to stop }]; I suppose the most blunt

Fastest way to check if an array contains the same objects of another array

孤街浪徒 提交于 2019-11-29 01:55:52
问题 The goal is to compare two arrays as and check if they contain the same objects (as fast as possible - there are lots of objects in the arrays). The arrays cannot be checked with isEqual: as they are differently sorted. I already tried the solution posted here (https://stackoverflow.com/a/1138417 - see last code snippet of the post by Peter Hosey). But this doesn't work with differently sorted arrays. The code I'm using now is the following: + (BOOL)arraysContainSameObjects:(NSArray *)array1

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

一笑奈何 提交于 2019-11-29 00:59:25
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]); } Georg Fritzsche 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 proceeds in that order, so simply counting iterations will give you the proper index into the

How to implement the NSFastEnumeration protocol?

拥有回忆 提交于 2019-11-28 18:46:43
I have a class and I want my class to confirm to the NSFastEnumeration Protocol. I've read the documentation but it's not really clear. Can someone please tell me what the protocol method should return and how it works? justin Apple's FastEnumerationSample shows you what to do, but here's a breakdown. The sole NSFastEnumeration method, countByEnumeratingWithState:objects:count: , returns chunks of the collection. It's executed whenever more items are needed, until it indicates that there are no more items by returning 0. A chunk is passed as a C array of id s. Within the method, the state

Fast Enumeration Vs NSEnumerator in Objective-C

谁说我不能喝 提交于 2019-11-28 07:44:20
I have seen this over and over, why exactly is it faster to use fast enumeration in loops rather than an NSEnumerator using nextObject: . Lily Ballard 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 the more modern approach, which leverages native language support to provide a much more

Loop through subview to check for empty UITextField - Swift

蹲街弑〆低调 提交于 2019-11-27 19:21:34
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 view in self.view.subviews as [UIView] { if view.isKindOfClass(UITextField) { //... } } Any help would