Search code examples
iosobjective-cnsrangeexception

Uncaught exception - NSRangeException


I've been trying to sort out this error I keep getting with NSArray and NSMutableDictionary. It keeps giving me this error:

ERROR:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex(inlove): index 1 beyond bounds [0 .. 0]'

Could someone please help me and give me and idea why i'm getting this error?

CODE:

NSMutableDictionary *LicenceDictionary = [NSMutableDictionary dictionary];
NSArray *LicenceValuesArray = [[NSArray alloc] initWithArray:[fileContent componentsSeparatedByString:@"\n"]];

for (NSString *Arrayobject in LicenceValuesArray) {
   if (strlen([Arrayobject UTF8String]) > 3) {
      NSArray *tempArray = [Arrayobject componentsSeparatedByString(angry)" = "];
      [LicenceDictionary setObject:tempArray[1] forKey:tempArray[0]];
   }
}

FILE CONTENT:

CFBundleVersion = 1.100
PortalURL = value
SpineURL = value
Authactivate = value
Authvalidate = value
Authlogout = value
RoleSelection = value
RoleSelectionURL = value
CertificateIssuer = value
SpineSessionPersistance = 0
CardRemovalPersistance = 0
LockScreenAppInactive = 0
PinchZoom = 0
LoggingSupport = 0
LogoutTimer = 90
ScreenLockTimer = 60
ScreenBlankTimer = 30
ProductName = MIA
LoggingLevel = ERROR_LOG
LoggingNumber = 0
LoggingDuration = 1
UpdateType = SILENT
InjectJavaScript = 0
JavaScriptToExecute = value
CustomerLicensed = value
ExpiryMinute = 0
ExpiryHour = 0
ExpiryDay = 31
ExpiryMonth = 10
ExpiryYear = 2014
ReadOnly = 0
Audit = 1
RemoteLogURL = value
SkipCard = 0
Signature = e8ZLMxVouwGs1CSosug3NQGmq9b7TAlraPXDFQocJpw9XNmVcquFme+p5GYVUUZ7waz/33PRX1uV7ECT6z8IK/dcqie4QcVVrC1RO7OSZx71K4QGBtRSaskA2qjSqoMkVGiqDdGwyAmFP66Y8LPW49Lrh7h3tIHwNvdMovajo40=

Solution

  • You need to add guard code, to ensure the conditions you expect are, in fact, true. You cannot assume something is true just because you want it to be:

    NSArray *tempArray = [Arrayobject componentsSeparatedByString:@" = "];
    if ([tempArray count] == 2) {
        [LicenceDictionary setObject:tempArray[1] forKey:tempArray[0]];
    }
    

    Also, this:

    if (strlen([Arrayobject UTF8String]) > 3) {
    

    should be:

    if ([Arrayobject length] > 3) {
    

    and this:

    NSArray *LicenceValuesArray = [[NSArray alloc] initWithArray:[fileContent  componentsSeparatedByString:@"\n"]];
    

    should be:

    NSArray *LicenceValuesArray = [fileContent componentsSeparatedByString:@"\n"];