views:

70

answers:

1

I'm getting error:

nested functions are disabled, use -fnested-functions to re-enable. 

Could be a syntax. I just can't wrap my head around it.

- (NSArray *)sortedVariants {
   NSInteger alphabeticSort(id object1, id object2, void *reverse) {
       if ((NSInteger *)reverse == NO) {
          return [[object1 name] localizedCaseInsensitiveCompare:[object2 name]];
       }
       return [[object2 name] localizedCaseInsensitiveCompare:[object1 name]];
   };
   return [variants sortedArrayUsingFunction:alphabeticSort context:NULL];
}
@end
+3  A: 

It's not (usually) allowed to define a function inside a function (or a method or whatever.) You define alphabeticSort inside -sortedVariants, right?

Instead do

NSInteger alphabeticSort(id object1, id object2, void *reverse) {
   if ((NSInteger *)reverse == NO) {
      return [[object1 name] localizedCaseInsensitiveCompare:[object2 name]];
   }
   return [[object2 name] localizedCaseInsensitiveCompare:[object1 name]];
};

- (NSArray *)sortedVariants {
   return [variants sortedArrayUsingFunction:alphabeticSort context:NULL];
}

Note that in Objective-C, a C-function defined between @implementation ... @end is just a function defined at the file scope, not associated to the class.

Yuji