尝试在Objective-C中读入RTF文件

时间:2014-06-22 00:18:51

标签: objective-c nsstring nsdata rtf

我正在使用Objective-C中的命令行应用程序读取RTF文件。我正在使用Dave DeLong这个问题的代码:How to read data from NSFileHandle line by line?

我的问题是我的输出是这样的:

    2014-06-21 20:03:45.578 AjrumTest[5663:303] {\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf200
    2014-06-21 20:03:45.579 AjrumTest[5663:303] {\fonttbl\f0\fnil\fcharset0 LucidaGrande;\f1\fnil\fcharset178 AlBayan;\f2\froman\fcharset0 TimesNewRomanPSMT;
    2014-06-21 20:03:45.580 AjrumTest[5663:303] \f3\fnil\fcharset178 GeezaPro;}
    2014-06-21 20:03:45.580 AjrumTest[5663:303] {\colortbl;\red255\green255\blue255;}
    2014-06-21 20:03:45.581 AjrumTest[5663:303] \margl1440\margr1440\vieww10800\viewh8400\viewkind0
    2014-06-21 20:03:45.581 AjrumTest[5663:303] \deftab720
    2014-06-21 20:03:45.581 AjrumTest[5663:303] \pard\pardeftab720
    2014-06-21 20:03:45.582 AjrumTest[5663:303] 
    2014-06-21 20:03:45.582 AjrumTest[5663:303] \f0\fs46 \cf0 1
    2014-06-21 20:03:45.582 AjrumTest[5663:303] \f1 - \'de\'f3\'dc\'c7\'e1\'f3 \'c7\'c8\'fa\'dc\'e4\'f5 \'c2\'c8\'f3\'f8 \'e6\'f3\'c7\'d3\'fa\'e3\'f5\'dc\'e5\'f5 \'e3\'f5\'cd\'f3\'e3\'f3\'f8\'dc\'cf\'f5
...

我意识到因为我正在阅读的文档是RTF文件,所以我需要将必要的属性附加到它。我从主方法传递我的文件,如下所示:

        NSString *pathToMyFile = [[NSBundle mainBundle] pathForResource:@"MyRTFDocument" ofType:@"rtf"];
        DDFileReader * reader = [[DDFileReader alloc] initWithFilePath:pathToMyFile];
        [reader enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
            NSLog(@"%@", line);
        }];

以下是解析文档的方法:

- (NSString *) readLine {
    if (currentOffset >= totalFileLength) {
        return nil;
    }
    NSData *newLineData = [lineDelimiter dataUsingEncoding:NSUTF8StringEncoding];
    [fileHandle seekToFileOffset:currentOffset];
    NSMutableData * currentData = [[NSMutableData alloc] init];
    BOOL shouldReadMore = YES;

    @autoreleasepool {

        while (shouldReadMore) {
            if (currentOffset >= totalFileLength) {
                break;
            }
            NSData *chunk = [fileHandle readDataOfLength:chunkSize];
            NSRange newLineRange = [chunk rangeOfData_dd:newLineData];
            if (newLineRange.location != NSNotFound) {

                //include the length so we can include the delimiter in the string
                chunk = [chunk subdataWithRange:NSMakeRange(0, newLineRange.location+[newLineData length])];
                shouldReadMore = NO;
            }
            [currentData appendData:chunk];
            currentOffset += [chunk length];
        }
    }

    NSDictionary *attrs = @{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType, NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]};

    NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:currentData options:attrs documentAttributes:nil error:nil];

    //NSLog(@"What does this give me? %@", [attrString string]);
    //The above line outputs (null) for [attrString string]
    NSString *line = [[NSString alloc] initWithData:currentData encoding:NSUTF8StringEncoding];
    return line;

}

- (NSString *) readTrimmedLine {
    return [[self readLine] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

#if NS_BLOCKS_AVAILABLE
- (void) enumerateLinesUsingBlock:(void(^)(NSString*, BOOL*))block {
    NSString *line = nil;
    BOOL stop = NO;
    while (stop == NO && (line = [self readLine])) {
        block(line, &stop);
    }
}
#endif

当我在创建NSAttributedString之前将正确的文档属性附加到NSData对象时,我得到一个null的对象:

NSDictionary *attrs = @{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType, NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:currentData options:attrs documentAttributes:nil error:nil];
NSLog(@"%@", [attrString string]);

在另一个iOS应用程序中,我有以下代码,它完全符合我的要求:

NSString *path = [[NSBundle mainBundle] pathForResource:@"AjrumiyyahPoemRawTextRTF" ofType:@"rtf"];
NSData *testData = [NSData dataWithContentsOfFile:path];
NSDictionary *attrs = @{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType, NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:testData options:attrs documentAttributes:nil error:nil];
NSLog(@"The text of the document is: %@", [attrString string]);

我需要对命令行应用程序做什么才能正确地将文档属性附加到NSAttributedString对象,以便输出正确?

1 个答案:

答案 0 :(得分:0)

我没有立即回答,但关于这行代码:

NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:currentData options:attrs documentAttributes:nil error:nil];

您应该使用error变量来确定结果为null / nil的原因。所以请写下以下内容:

NSError *error = nil;
NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:currentData options:attrs documentAttributes:nil error:&error];

If (attrString == nil) // parsing failed, so read out error
{
    NSLog(@"%@", error);
}

也许这会帮助你弄清楚出了什么问题。