2017-07-06 4 views
0

Après section de code résultats magnifiquement: - (void)setRepresentedObject:(id)representedObject { DLOG()NSRegularExpression: stringByReplacingMatchesInString ne reconstruit pas la chaîne avec plusieurs lignes

- (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); 

}

Mais au lieu de mettre une seule ligne d'entrée dans la chaîne variable si je tente pour lire un fichier de code dit fichier ViewController.m qui contient le code suivant et le mettre dans cette variable: cela ne fonctionne pas.

// 
// ViewController.m 
// ForCommonCoding 
// 
// Created by A Programmer on 7/6/17. 
// Copyright ? 2017 A Programmer. All rights reserved. 
// 

#import "ViewController.h" 


@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view. 
} 
- (void)setRepresentedObject:(id)representedObject { 
     [super setRepresentedObject:representedObject]; 

    // Update the view, if already loaded. 
} 

- (void)sampleFunction { 
    NSLog(@"This is sampleFunction"); 
} 
@end 

NSLog affiche la même chose que l'entrée. DLog() non apposé Quelqu'un peut-il aider à le résoudre?

Répondre

0

Cela devrait fonctionner comme vous attendez:

NSError *error = nil; 
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^(-[^{]+\{)" options:NSRegularExpressionAnchorsMatchLines error:&error]; 
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"$1 DLOG()"]; 
NSLog(@"%@", modifiedString); 

Les trois problèmes: votre code

  • NSRegularExpressionAnchorsMatchLines était manquante, mais vous correspondant à la ligne de départ avec ^

  • Le groupe correspondant est manquant dans regex qui vous permet d'utiliser le résultat correspondant en remplacement: ()

  • chaîne de modèle de remplacement utilise l'ensemble string au lieu du premier match $1

Code de travail Playground:

let line = "- (void)setRepresentedObject:(id)representedObject {" 
let file = "#import \"ViewController.h\"\n\n\[email protected] ViewController\n\n- (void)viewDidLoad {\n [super viewDidLoad];\n\n // Do any additional setup after loading the view.\n}\n- (void)setRepresentedObject:(id)representedObject {\n   [super setRepresentedObject:representedObject];\n\n  // Update the view, if already loaded.\n}\n\n- (void)sampleFunction {\n NSLog(@\"This is sampleFunction\");\n}\[email protected]" 
let regex = try! NSRegularExpression(pattern: "^(-[^{]+\\{)", options: [.anchorsMatchLines]) 
regex.stringByReplacingMatches(in: line, options: [], range: NSMakeRange(0, line.characters.count), withTemplate: "$1 DLOG()") 
regex.stringByReplacingMatches(in: file, options: [], range: NSMakeRange(0, file.characters.count), withTemplate: "$1 DLOG()")