NSDictionary 的日志返回空元素和零元素



我想打印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:@"rnrn" intoString:&textString];
            textString = [textString stringByReplacingOccurrencesOfString:@"rn" 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
}

不能将nil添加到数组中。如果需要 NULL 值,请使用:

[subtitlesArray addObject:[NSNull null]]

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

最新更新