トップ «前の日記(2012-06-28) 最新 次の日記(2012-06-30)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2012-06-29 [OSX][iOS]データ解析(2)

現在のXcodeはビルドした結果の出力先が隠されている為、コマンドライン・ツールをビルドして、操作するという流れが面倒になっている為、標準入力から値を得る部分をデバッグ用に内部変数から値を得る様に修正する。

#ifndef DEBUG
        NSFileHandle    *fhi = [NSFileHandle fileHandleWithStandardInput];
#endif
        NSFileHandle    *fho = [NSFileHandle fileHandleWithStandardOutput];
 
#ifndef DEBUG
        NSData  *datainput = [fhi readDataToEndOfFile];
        NSString    *str = [[NSString alloc] initWithData:datainput encoding:NSUTF8StringEncoding];
#else
        NSString    *str = @"12:23:45 start\n1,10\n2,13";
#endif

行単位に切り出せたら、Perlのchompのように末尾の改行文字を削ろう。

            NSCharacterSet  *charSet = [NSCharacterSet newlineCharacterSet];
            line = [line stringByTrimmingCharactersInSet:charSet];

著者はよくやっているのだが、ログのある時刻の抜き出しをやってみよう。

            regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d\\d:\\d\\d:\\d\\d)"
                                                              options:NSRegularExpressionCaseInsensitive
                                                                error:&error];
            NSTextCheckingResult    *match = [regex firstMatchInString:line
                                                               options:0
                                                                 range:NSMakeRange(0, line.length)];
            if (0 < [match numberOfRanges]) {
                NSString    *tm = [line substringWithRange:[match rangeAtIndex:0]];
                NSLog(@"time: %@", tm);
            }

完成。

@autoreleasepool {        
#ifndef DEBUG
        NSFileHandle    *fhi = [NSFileHandle fileHandleWithStandardInput];
#endif
        NSFileHandle    *fho = [NSFileHandle fileHandleWithStandardOutput];
 
#ifndef DEBUG
        NSData  *datainput = [fhi readDataToEndOfFile];
        NSString    *str = [[NSString alloc] initWithData:datainput encoding:NSUTF8StringEncoding];
#else
        NSString    *str = @"12:23:45 start\n1,10\n2,13";
#endif
        
        NSError *error = NULL;
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(.+)\n|(.+)"
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:&error];
        NSArray    *array = [regex matchesInString:str
                                           options:0
                                             range:NSMakeRange(0, str.length)];
        NSTextCheckingResult    *matches;
        for (matches in array) {
            NSString    *line = [str substringWithRange:[matches rangeAtIndex:0]];
 
#ifdef DEBUG
            NSData  *dataout = [[NSData alloc] initWithBytes:[line UTF8String]
                                                      length:[line lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];
            [fho writeData:dataout];
#endif
            
            NSCharacterSet  *charSet = [NSCharacterSet newlineCharacterSet];
            line = [line stringByTrimmingCharactersInSet:charSet];
 
            regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d\\d:\\d\\d:\\d\\d)"
                                                              options:NSRegularExpressionCaseInsensitive
                                                                error:&error];
            NSTextCheckingResult    *match = [regex firstMatchInString:line
                                                               options:0
                                                                 range:NSMakeRange(0, line.length)];
            if (0 < [match numberOfRanges]) {
                NSString    *tm = [line substringWithRange:[match rangeAtIndex:0]];
                NSLog(@"time: %@", tm);
            }
        }
    }

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/mac/analog - GitHub

トップ «前の日記(2012-06-28) 最新 次の日記(2012-06-30)» 編集