NSDictionary的日志返回null和零元素

时间:2015-04-14 12:51:01

标签: ios objective-c nsdictionary nslog

我想在NSLog中打印多个元素和一个元素。我创建新的字典变量并将其添加到NSMutableArray。然后在NSLog中,我希望看到元素的内容,但它返回null和零元素。我的代码如下:

while (![scanner isAtEnd])
    {
            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];

        [subtitlesArray addObject:dictionary];

            NSLog(@"%@", dictionary);
    }

    [subtitlesArray addObject:nil];

    // Insert code here to initialize your application

    NSLog(@"%lu", [subtitlesArray count]);
// for example
    NSLog(@"%@", [subtitlesArray objectAtIndex: 7]);
//And here I get 0 and null

}

1 个答案:

答案 0 :(得分:2)

您无法将nil添加到数组中。如果您想要NULL值,请使用:

[subtitlesArray addObject:[NSNull null]]

您需要使用调试器来查找扫描循环内发生的情况,并确保已分配并初始化subtitlesArray。很可能,数组的大小不是8,所以当你要求索引7时,你得到的是nil。