Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Convert property names in a text string with their value
- object = user defined object
- // your text below with the property names enclosed in square brackets
- NSString *result = @"Your text here with macros eg. [name], [value], [description]";
- NSRegularExpression *regEx = [NSRegularExpression regularExpressionWithPattern:@"\\[(.*?)\\]" options:0 error:NULL];
- NSArray *matches = [regEx matchesInString:result options:0 range:NSMakeRange(0, [text length])];
- if (matches) {
- for (NSTextCheckingResult *match in matches) {
- NSString *macro = [[[text substringWithRange:match.range] stringByReplacingOccurrencesOfString:@"[" withString:@""] stringByReplacingOccurrencesOfString:@"]" withString:@""];
- if ([object respondsToSelector:NSSelectorFromString(macro)])
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
- result = [result stringByReplacingCharactersInRange:match.range withString:[NSString stringWithFormat:@"%@", [object performSelector:NSSelectorFromString(macro)]]];
- #pragma clang diagnostic pop
- else
- result = [result stringByReplacingCharactersInRange:match.range withString:@""];
- }
- }
- NSLog(@"%@", result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement