0

How would I suppress the warning in this code snippet?

for (NSDictionary *record in self.records) {
    [deletedRows addObject:[NSIndexPath indexPathForRow:row inSection:RecordSection]];
    row++;
}
Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167
  • #pragma GCC diagnostic ignored "-Wwarning-flag" suppresses warnings http://stackoverflow.com/questions/194666/is-there-a-way-to-suppress-warnings-in-xcode – Rachel Gallen Jul 02 '14 at 07:35
  • 1
    You can use __unused like __unused NSDictionary *record – Ryan Jul 02 '14 at 07:35

1 Answers1

2

Don't use fast enumeration at all:

for (NSUInteger row = 0; row < [self.records count]; row++) {
    [deletedRows addObject:[NSIndexPath indexPathForRow:row inSection:RecordSection]];
}

This is:

  1. Faster, as you are not accessing the array, just generating a sequence of numbers.
  2. Cleaner, as there are no annoying compiler directives.
  3. Correct, as you don't declare a variable you don't use.
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Cool. That seems to be what I should be doing. Thanks. – Calvin Cheng Jul 02 '14 at 07:37
  • 1
    1. Faster <- Its a joke? – Oleg Sobolev Jul 02 '14 at 07:42
  • @OlegSobolev Well I haven't measured it, but I would expect it to be slightly faster. – trojanfoe Jul 02 '14 at 07:43
  • LOL. That's true. I thought fast enumeration should be faster? :D – Calvin Cheng Jul 02 '14 at 07:44
  • Look at results of any test, fast enumeration most fast. Specially, if you create variable in for() loop, like in your example! And you call [count:] method each time! Please, never do like that if you need speed. @trojanfoe – Oleg Sobolev Jul 02 '14 at 07:45
  • @OlegSobolev The `for` loop in my example doesn't enumerate anything (notice I am not accessing the array using `objectAtIndex:`). I would therefore expect it to be faster still. – trojanfoe Jul 02 '14 at 07:47
  • Here's a good article with benchmarks - http://iosdevelopertips.com/objective-c/high-performance-collection-looping-objective-c.html – Calvin Cheng Jul 02 '14 at 07:49
  • In for(0..1000) loop, you will 1000 times init variable of int type, and 1000 times call count: method. That is not fast. – Oleg Sobolev Jul 02 '14 at 07:50
  • @OlegSobolev It would need to be measured to be sure, but I am not going to do that. – trojanfoe Jul 02 '14 at 07:51
  • $ For loop: 0.115235 $ Optimized for loop: 0.083858 $ For…in loop: 0.00683898 @trojanfoe – Oleg Sobolev Jul 02 '14 at 07:52
  • @OlegSobolev Please put your code on a github gist, so I can see it. – trojanfoe Jul 02 '14 at 07:54
  • Look at the link above. @trojanfoe – Oleg Sobolev Jul 02 '14 at 07:55
  • So basically, stick to for in but use `__unused`. – Calvin Cheng Jul 02 '14 at 07:56
  • The code in the link is **accessing the array**; where as the code I provide **does not**. Therefore it's not a valid test. If you are really interested in knowing if it's faster then you need to measure the code in the question with the code in my answer, which that page does not do. – trojanfoe Jul 02 '14 at 08:02
  • @CalvinCheng If you want to make it quicker still (if that's important to you) then initialise the "end index" before the `for` loop, e.g.: `const NSUInteger count = [self.records count]; for (NSUInteger row = 0; row < count; row++) { ... }`. – trojanfoe Jul 02 '14 at 08:05