ios - NSRange changes itself inside for loop -



ios - NSRange changes itself inside for loop -

i have nsdictionary of nsrange objects, keys index in array. attempting utilize each of these ranges create substrings of larger string, , place each of these substrings in array. have completed, nsrange mysteriously changing it's value in code, causing crash , throw exception. here code:

nsmutablearray*substringarray = [[nsmutablearray alloc] init]; for(id key in adict){ nsrange* range = cfbridgingretain([adict objectforkey:key]); nslog(@"this single range: %@",range); //range changed here somehow nsstring* asubstring = [self.content substringwithrange:*range]; nslog(@"%@",asubstring); [substringarray addobject:asubstring]; }

my log output looks this:

this single range: nsrange: {1874, 72} 2014-06-17 20:07:30.100 testapp[8027:60b] *** terminating app due uncaught exception 'nsrangeexception', reason: '-[__nscfstring substringwithrange:]: range {4318599072, 4} out of bounds; string length 5562'

by popular demand :)

the main issue code straight storing nsrange struct in dictionary. can't this. must wrap nsrange in nsvalue store in dictionary.

to add together nsrange can do:

nsrange range = ... // range nsvalue *value = [nsvalue valuewithrange:range]; dict[somekey] = value;

then posted code becomes:

for (id key in adict) { nsvalue *value = adict[key]; nsrange range = [value rangevalue]; nslog(@"this single range: %@",range); //range changed here somehow nsstring* asubstring = [self.content substringwithrange:range]; nslog(@"%@",asubstring); [substringarray addobject:asubstring]; }

ios objective-c nsrange nsexception nsrangeexception

Comments