0

I am trying with this string to locate first line of every function in my app developed in Objective C: ^-[^{]+{.

This works fine in XCode`s Find>Regular Expression searching.

But when I am trying to use this string in the following function, the function is always returning NULL.

- (void) putLogInFunctionCalls{

    NSString *string = @"- (void)setRepresentedObject:(id)representedObject {";
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^-[^{]+\{" options:NSRegularExpressionCaseInsensitive error:&error];

    NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:[NSString stringWithFormat:@"%@ DLOG()", string]];

    NSLog(@"%@", modifiedString);
}

But it should return:

"- (void)setRepresentedObject:(id)representedObject { DLOG()"

This question is specific to detecting a function starting line of Objective-C. Anyone attempting to write meta-codes on iOS/MacOS apps developed in Objective-C might encounter this problem and might find answer usefull. The answer that is marked as duplicate of this one is more generic.

2
  • 1
    In short: @"^-[^{]+\\{" Commented Jun 19, 2017 at 6:30
  • Thanks. It worked. Commented Jun 19, 2017 at 6:36

1 Answer 1

0

You need to use

@"^-[^{]+\\{"

The pattern matches:

^ - start of string - - - a hyphen - [^{]+ - one or more characters other than { - \\{ - a single literal { char.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.