一些背景知识:我编写了一个小应用程序并创建了一个名为 stuff.srt 的文件,其中包含驻留在包中的示例;因此,我访问它的方式。
这只是一个快速而肮脏的事情,一个概念验证。请注意,它不检查结果。实际应用程序总是检查他们的结果。如您所见,工作发生在-applicationDidFinishLaunching:
方法中(我在 Mac OS X 中工作,而不是 iOS)。
编辑:
有人指出,最初发布的代码没有正确处理多个文本行。为了解决这个问题,我利用SRT 文件使用 CRLF 作为其换行符这一事实,并搜索此序列的两次出现。然后,根据我在此处观察到的内容,我将文本字符串中所有出现的 CRLF 更改为空格。这不考虑文本每行中的前导或尾随空格。
我将 stuff.srt 文件的内容更改为:
1
00:00:00,000 --> 00:00:01,000
This is the first line
and it has a secondary line
2
00:00:01,000 --> 00:00:02,000
This is the second line
3
00:00:02,000 --> 00:00:03,000
This is the last line
and it has a secondary line too
并且代码修改如下(我也将所有内容都放入了@autoreleasepool 指令;在解析文件的过程中可能会生成很多自动释放的对象!):
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"stuff" ofType:@"srt"];
NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSScanner *scanner = [NSScanner scannerWithString:string];
while (![scanner isAtEnd])
{
@autoreleasepool
{
NSString *indexString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&indexString];
NSString *startString;
(void) [scanner scanUpToString:@" --> " intoString:&startString];
// My string constant doesn't begin with spaces because scanners
// skip spaces and newlines by default.
(void) [scanner scanString:@"-->" intoString:NULL];
NSString *endString;
(void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&endString];
NSString *textString;
// (void) [scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&textString];
// BEGIN EDIT
(void) [scanner scanUpToString:@"\r\n\r\n" intoString:&textString];
textString = [textString stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];
// Addresses trailing space added if CRLF is on a line by itself at the end of the SRT file
textString = [textString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// END EDIT
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
indexString, @"index",
startString, @"start",
endString , @"end",
textString , @"text",
nil];
NSLog(@"%@", dictionary);
}
}
}
修改后的输出如下所示:
2013-02-09 16:10:17.727 SRTFileScan[4846:303] {
end = "00:00:01,000";
index = 1;
start = "00:00:00,000";
text = "This is the first line and it has a secondary line";
}
2013-02-09 16:10:17.729 SRTFileScan[4846:303] {
end = "00:00:02,000";
index = 2;
start = "00:00:01,000";
text = "This is the second line";
}
2013-02-09 16:10:17.730 SRTFileScan[4846:303] {
end = "00:00:03,000";
index = 3;
start = "00:00:02,000";
text = "This is the last line and it has a secondary line too";
}
我从今天阅读的内容中学到的另一件事:SRT 文件格式起源于法国,输入中看到的逗号是那里使用的小数分隔符。