-1

I'd like to print number of elements and one of element in NSLog. I create new dictionary variable and add it to the NSMutableArray. Then in NSLog I'd like to see content of an element but it returns null and zero elements. My code is below:

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

}
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
DKM
  • 270
  • 3
  • 13
  • 3
    Did you initialise your `subtitlesArray` as well? – tbaranes Apr 14 '15 at 12:53
  • 2
    The last line should cause a crash because you can't add nil to an NSMutableArray, so I would agree with @Ckouta - your subtitlesArray is nil. – Paulw11 Apr 14 '15 at 12:57
  • 1
    why are you add nil at last of your array? "[subtitlesArray addObject:nil]; " i think this cause the problem. and make sure your array is initialise. – Chirag Shah Apr 14 '15 at 12:57
  • Variable subtitlesArray is defined as a property and synthetize in class.m file. It doesn not crash as well. – DKM Apr 14 '15 at 17:48
  • I have allocated variable as you've written instead of using @synthetize and it works. – DKM Apr 14 '15 at 20:16
  • `@synthesize` doesn't allocate an object instance - it merely provides some control over the declaration of the backing variable. If you are happy with the default name, setter and getter then you don't need `@synthesize` but you *always* need to alloc/init the property – Paulw11 Apr 14 '15 at 20:46
  • Thank you for the comment.....but I've read that using @synthesize it creates for me: setter and getter and creates object which will be used. – DKM Apr 15 '15 at 09:23

1 Answers1

2

You can not add nil to an array. If you want a NULL value use:

[subtitlesArray addObject:[NSNull null]]

You need to use the debugger to find out what is happening inside your scan loop and also make sure that you have allocated and initialized subtitlesArray. Quite possibly, the array is not 8 in size, so when you ask for index 7 you get nil.

Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28