2

I took some examples how to use RegularExpression in Objective-c. The problem is I want to use the same code for Linux (gnustep) and for OSX too. After small changes it works in OSX but not under Linux.

 NSArray* matches = [regex matchesInString:line options:0
                     range: NSMakeRange(0,[line length])];
     if ([matches count] > 0) {
        for (NSTextCheckingResult* match in matches) {
           NSString* matchText = [line substringWithRange:[match range]];
           NSLog(@"match: %@", matchText);
           NSRange group1 = [match rangeAtIndex:1];
           NSLog(@"group1: %@", [line substringWithRange:group1]);
        }
     }else{
        NSLog(@"nomatch %@",[matches count]);
     }

When I want to compile this part of code compilator will return errors:

error:

sending 'id' to parameter of incompatible type 'NSRange' (aka 'struct _NSRange')
           NSString* matchText = [line substringWithRange:[match range]];

initializing 'NSRange' (aka 'struct _NSRange') with an expression of
                                              incompatible type 'id'
           NSRange group1 = [match rangeAtIndex:1];

I don't understand why :-(

property range and rangeAtIndex should by returned by NSTextCheckingResult as NSRange and not id.

Is there a way how to covert id to NSRange please?

Thank you

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Marian
  • 33
  • 4

1 Answers1

3

I suspect the problem here must be that the compiler can't find declaration of your range and rangeAtIndex: methods. Since it can't find them it defaults their return types to id. Make sure you've imported header file with these methods declaration into the file where your adduced code is. Or maybe you have imported that header file but it doesn't contain the declarations, so make sure you've provided them

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • 2
    absolutelly right !!! I put #import into and everythings works as I expected. Thank you very much. – Marian May 22 '14 at 12:39