-1

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=
YaBCK
  • 2,949
  • 4
  • 32
  • 61
  • Add `NSAssert([tempArray length] == 2, @"Oops");` *after* the `NSArray *tempArray =` line and see if that fires (alternatively use a debugger and break on the line *after* and check the array). You are assuming it contains two objects and it probably doesn't – Droppy Oct 23 '14 at 14:29
  • Your code assumes `tempArray` has at least 2 objects but it only has one. – rmaddy Oct 23 '14 at 14:30
  • BTW - why are you using `strlen` to check the length of `ArrayObject`? Just do: `if (ArrayObject.length)`. And please use proper naming standards. Only class names should start with uppercase. Method and variable names should start with lowercase. – rmaddy Oct 23 '14 at 14:31
  • @Droppy The NSAssert fires, but i had to use NSAssert([ArrayObject length] == 2, @"Oops"); instead of NSAssert([tempArray length] == 2, @"Oops"); – YaBCK Oct 23 '14 at 14:33
  • @rmaddy tempArray[1] is the second value in my file, tempArray[0] is the first value in my file. I'm accessing the values from my file content – YaBCK Oct 23 '14 at 14:36
  • @Chris No, that is not true. `tempArray[0]` is the part before the ` = ` text of a single line and `tempArray[1]` is the part after the ` = ` of the same line. The problem is that there won't be a `tempArray[1]` if the line doesn't contain the text ` = `. – rmaddy Oct 23 '14 at 14:39
  • @rmaddy That's what i'm doing and there is a value after the '=' – YaBCK Oct 23 '14 at 14:47

1 Answers1

2

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"];
Droppy
  • 9,691
  • 1
  • 20
  • 27