fast-enumeration

Keep track of index in fast enumeration

南楼画角 提交于 2020-01-10 07:35:33
问题 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|? } 回答1: 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

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-12-30 04:00:10
问题 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

How to implement the NSFastEnumeration protocol?

此生再无相见时 提交于 2019-12-29 03:27:06
问题 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? 回答1: 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

Fast Enumeration on NSArray of Different Types

感情迁移 提交于 2019-12-28 06:48:10
问题 I have this question here (as well other quesrtions on SO), and the Apple docs about Objective-C collections and fast enumeration. What is not made clear is if an NSArray populated with different types, and a loop is created like: for ( NSString *string in myArray ) NSLog( @"%@\n", string ); What exactly happens here? Will the loop skip over anything that is not an NSString ? For example, if (for the sake of argument) a UIView is in the array, what would happen when the loop encounters that

Objective c “for each” (fast enumeration) — evaluation of collection?

寵の児 提交于 2019-12-22 03:55:18
问题 It seems from experimentation that the collection expression is evaluated only once. Consider this example: static NSArray *a; - (NSArray *)fcn { if (a == nil) a = [NSArray arrayWithObjects:@"one", @"two", @"three", nil]; NSLog(@"called"); return a; } ... for (NSString *s in [self fcn]) NSLog(@"%@", s); The output is: 2010-10-07 07:37:31.419 WidePhotoViewer Lite[23694:207] called 2010-10-07 07:37:31.420 WidePhotoViewer Lite[23694:207] one 2010-10-07 07:37:31.425 WidePhotoViewer Lite[23694:207

Objective c “for each” (fast enumeration) — evaluation of collection?

≯℡__Kan透↙ 提交于 2019-12-22 03:55:08
问题 It seems from experimentation that the collection expression is evaluated only once. Consider this example: static NSArray *a; - (NSArray *)fcn { if (a == nil) a = [NSArray arrayWithObjects:@"one", @"two", @"three", nil]; NSLog(@"called"); return a; } ... for (NSString *s in [self fcn]) NSLog(@"%@", s); The output is: 2010-10-07 07:37:31.419 WidePhotoViewer Lite[23694:207] called 2010-10-07 07:37:31.420 WidePhotoViewer Lite[23694:207] one 2010-10-07 07:37:31.425 WidePhotoViewer Lite[23694:207

Why does fast enumeration not skip the NSNumbers when I specify NSStrings?

别等时光非礼了梦想. 提交于 2019-12-19 09:06:36
问题 I thought that I knew how to use fast enumeration, but there is something I don't understand about it. If I create three NSString objects and three NSNumber objects and put them in an 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

How for in loop works internally - Objective C - Foundation

為{幸葍}努か 提交于 2019-12-19 03:40:58
问题 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

fast enumeration for NSDictionary instance ordered by the key

≡放荡痞女 提交于 2019-12-12 11:34:24
问题 Overview I am using fast enumeration to iterate through an NSDictionary instance I expected the NSDictionary instance to be enumerated based on the ascending order of the key but that doesn't seem to be the case What I want to do: I want to be able iterate through the NSDictionary instance in the ascending order of the key using fast enumeration Note: Pls see expected output vs actual output Questions Am i making a mistake with my implementation ? Does NSDictionary's fast enumeration

Vectorization of Matlab Code involving ODE solver at each iteration

微笑、不失礼 提交于 2019-12-12 05:15:47
问题 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; dw=rand(p,1); M0=repmat([0 0 1],p,1)'; for p=1:ns [t,M(:,:,p)]=ode45(@(t,M) testfun(t,M,dw(p)),tspan,M0(:,p)); end where function dM=testfun(t,M,w1) M_x=M(1); M_y=M(2); M_z=M(3); dM=[w1*M_y;-w1*M_x+w1*M_z-2*w1*M_y;-w1*M_y-(1-M_z)]; 回答1: Try this and let me know how it