Strange NSRangeException beyond bounds error in NSMutablearray - iphone

In my app, I'm doing audio analysis.
Every second, I'm calling some methods, like this one :
- (NSNumber *) arrayAverage: (NSMutableArray *)array
{
int countArray = [array count];
if(!countArray)
return nil;
else if (countArray <= 5 )
return [array valueForKeyPath:#"#avg.doubleValue"];
else
{
// Création et initialisation d'un tableau temporaire
NSMutableArray *averageArray = [[NSMutableArray alloc] initWithArray:array];
NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:YES];
[averageArray sortUsingDescriptors:[NSArray arrayWithObject:desc]];
for (int j = 0; j < countArray / 3; j++)
{
[averageArray removeLastObject];
[averageArray removeObjectAtIndex:0];
}
NSNumber * average = [averageArray valueForKeyPath:#"#avg.doubleValue"];
return average;
}
}
The problem is that I sometime receive a NSRangeException error (after 1min or a few hours...it depends...) saying that the array index is beyond bounds. The strange thing is that the index is NOT out of bounds...
Those methods are only called on the main thread.
Thanks in advance for your help !
EDIT 1 :
With the help of Anim and Abhinav, I have changed my code as following.
It has worked for more than 2hours 45min (which is a record) and then crash with a EXC_BAD_ACCESS code 1 error...
- (NSNumber *) arrayAverage: (NSMutableArray *)array
{
NSArray *arrayCopy = [[NSArray alloc] initWithArray:array]; // CRASH with EXC_BAD_ACCESS error
int countArray = [arrayCopy count];
if(!countArray)
return nil;
else if (countArray <= 5 )
return [arrayCopy valueForKeyPath:#"#avg.doubleValue"];
else
{
// Création et initialisation d'un tableau temporaire
NSMutableArray *averageArray = [[NSMutableArray alloc] initWithArray:arrayCopy];
NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:YES];
[averageArray sortUsingDescriptors:[NSArray arrayWithObject:desc]];
int startOfRange = countArray / 3;
int rangeLength = countArray - 2 * (countArray / 3);
NSArray* slicedArray = [averageArray subarrayWithRange:NSMakeRange(startOfRange, rangeLength)];
NSNumber * average = [slicedArray valueForKeyPath:#"#avg.doubleValue"];
return average;
}
}

With given piece of code it would more of guessing on whats going on here. Please make sure following:
Before calling "removeLastObject" or "removeObjectAtIndex", it makes sense to put a check on the array count. Call them only when there is something to remove.
Given that NSArray and NSMutableArray are not thread safe, make sure that the arrays you are operating on are not getting modified and used at the same time.
I would like to induce following statements inside your For loop:
if (averageArray.count > 0) {
[averageArray removeLastObject];
}
if (averageArray.count > 0) {
[averageArray removeObjectAtIndex:0];
}

you can do one more thing, just add one condition before the loop:
here is the snippet, have added comment:
- (NSNumber *) arrayAverage: (NSMutableArray *)array
{
int countArray = [array count];
if(!countArray)
return nil;
else if (countArray <= 5 )
return [array valueForKeyPath:#"#avg.doubleValue"];
else
{
// Création et initialisation d'un tableau temporaire
NSMutableArray *averageArray = [[NSMutableArray alloc] initWithArray:array];
NSSortDescriptor* desc = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:YES];
[averageArray sortUsingDescriptors:[NSArray arrayWithObject:desc]];
if ([array count] > 0) //Added one condition here---PR Singh
{
for (int j = 0; j < countArray / 3; j++)
{
[averageArray removeLastObject];
[averageArray removeObjectAtIndex:0];
}
}
NSNumber * average = [averageArray valueForKeyPath:#"#avg.doubleValue"];
return average;
}
}

Related

search the values in array in iPhone sdk

I have the array like:
(
{
id=1;
Title="AAAA";
period_id=1;
},
{
id=2;
Title="BBBB";
period_id=2;
},
{
id=3;
Title="CCCC";
period_id=2;
},
{
id=4;
Title="DDDD";
period_id=2;
},
{
id=5;
Title="EEEE";
period_id=3;
},
)
Question: How can i know that Period_id=2 is multiple times in the array?
Help me solve this.
Thank you,
There are lots of ways to do so, Some of them are here ..
A:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"period_id == %#", #"2"];
NSArray *newArray = [array filteredArrayUsingPredicate:predicate];
NSLog(#"%d", [newArray count]);
B:
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (id obj in array)
{
if([obj[#"period_id"] isEqualToString:#"2"]){
[newArray addObject:obj];
}
}
NSLog(#"%d", [newArray count]);
C:
NSArray *allIds = [array valueForKey:#"period_id"];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:allIds];
for (id item in set)
{
NSLog(#"period_id=%#, Count=%d", item,[set countForObject:item]);
}
D:
NSArray *allIds = [array valueForKey:#"period_id"];
__block NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSString *valueToCheck = #"2";
[allIds enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([obj isEqualToString:valueToCheck])
[newArray addObject:obj];
}];
NSLog(#"%d", [newArray count]);
E:
NSIndexSet *indexes = [array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:#"period_id"] isEqualToString:#"2"];
}];
NSArray *newArray = [array objectsAtIndexes:indexes];
NSLog(#"%d", [newArray count]);
try like this,
NSIndexSet *indices = [questionSections indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [[obj objectForKey:#"period_id"] isEqualToString:#"2"];
}];
NSArray *filtered = [questionSections objectsAtIndexes:indices];
NSLog(#"duplictae:%d\n %#",[indices count],filtered);
O/P:-
duplicate: 3
(
{
name = bbbb;
"period_id" = 2;
},
{
name = ccccc;
"period_id" = 2;
},
{
name = ddddd;
"period_id" = 2;
}
)
if the array is sorted, as it seems at your case, just check if the next item has the same value as this one
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == array[i + 1].id) {
// Duplicate
}
}
if you just want to know about id = 2
int idCount = 0;
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == 2) {
idCount++;
}
}
if you also want to know the location
int idCount = 0;
int idarr[array.size()];
for(int i = 0; i < array.size() - 1; i++) {
if (array[i].id == 2) {
idarr[idCount++] = i;
}
}
I think this is a JSON response from what I gather. Yes you can get the period_id. Add all the period_id's in an NSMutableArray.
Then simply search for the period_id from within this array for the values of the period_id to be same . You will get the index on which the period_id's are same.
NSSet *uniqueElements = [NSSet setWithArray:myArray];
for(id element in uniqueElements) {
// iterate here
}
You could also use NSPredicate to check duplicate.
Try this Example:
NSPredicate *testPredicate = [NSPredicate predicateWithFormat:#"period_id.intValue == %d",value];
NSMutableArray *data = [[NSMutableArray alloc] init];
NSArray *testArray= [yourArray filteredArrayUsingPredicate:testPredicate];
NSLog(#"duplicate:%d",[testArray count]);

Separate array into many in a dynamic way? Obj-C

I currently have this method:
-(void)seperateGuides
{
DLog("GetGuideListCount: %i", [[appDelegate getGuideList] count]);
columnOneArray = [[NSMutableArray alloc] init];
columnTwoArray = [[NSMutableArray alloc] init];
columnThreeArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [[appDelegate getGuideList] count]; i = i + 3) {
[columnOneArray addObject:[[appDelegate getGuideList]objectAtIndex:i]];
}
for (int i = 1; i < [[appDelegate getGuideList] count]; i = i + 3) {
[columnTwoArray addObject:[[appDelegate getGuideList]objectAtIndex:i]];
}
for (int i = 2; i < [[appDelegate getGuideList] count]; i = i + 3) {
[columnThreeArray addObject:[[appDelegate getGuideList]objectAtIndex:i]];
}
}
And need to do this more dynamic, so I can define how many arrays I want and then get the arrays.
Different possibilities I'm considering is making it a mutli-dimensional array (although I'm not sure how to handle it in Objectvie-C), or making a method that simply loops through as many times as I define, the problem there is that I'm not quite sure how to get the different arrays.
A simple algorithm or another possible solution would be greatly appreciated.
The algorithm you're describing sounds equivalent to what we do when we deal a deck of cards into multiple hands, so I'd do it like this:
- (NSArray *)dealObjects:(NSArray *)objects intoArrays:(NSInteger)numArrays
{
NSMutableArray *arrays = [NSMutableArray arrayWithCapacity:numArrays];
for (a = 0; a < numArrays; a++) {
[arrays addObject:[NSMutableArray arrayWithCapacity:[objects count] / numArrays];
}
for (i = 0; i < [objects count]; i++) {
[[arrays objectAtIndex:i % numArrays] addObject:[objects objectAtIndex:i]];
}
return arrays;
}
You can add multiple array to another arrays as this,
-(void)seperateGuides:(int)columnCount
{
rootArray=[[NSMutableArray alloc] init];
for(int i=0;i<columnCount;i++)
{
column = [[NSMutableArray alloc] init];
for(int j=i;j<[[appDelegate getGuideList] count];j=j+3)
{
[column addObject:[[appDelegate getGuideList]objectAtIndex:j]];
[rootArray addObject:column];
[column release];
}
}
}
Quite simple really. Just add new NSArray objects to a root array as you iterate through your dataset.
- (void)seperateGuides {
DLog("GetGuideListCount: %i", [[AppDelegate getGuideList] count]);
NSArray *root = [[NSArray alloc] init];
int dimensions = anyIntGreaterThanZero;
for (int i = 0; i < dimensions; i += dimensions) {
NSArray *branch = [[NSArray alloc] init];
int k = 0;
for (k += i; k < [[AppDelegate getGuideList] count]; k += dimensions) {
[branch addObject:[[AppDelegate getGuideList] objectAtIndex:k]];
}
[root arrayByAddingObject:branch];
}
}
-(void)seperateGuides
{
NSMutableArray *parentArray = [[NSMutableArray alloc]init];
int count = [[appDelegate getGuideList] count];
int TOTAL_COLUMNS = 3;//Define number of columns here
for (int i = 0; i <count; i++)
{
int columnNo = i % TOTAL_COLUMNS;
if(parentArray.count > columnNo)
{
NSMutableArray *innerArray = [parentArray objectAtIndex:columnNo];
[innerArray addObject:[[appDelegate getGuideList]objectAtIndex:i]];
}
else
{
NSMutableArray *innerArray = [NSMutableArray arrayWithObject:[[appDelegate getGuideList]objectAtIndex:i]];
[parentArray insertObject:innerArray atIndex:columnNo];
}
}
}
Hope this helps...
Here parentArray will have NSMutableArray as its members. Each array represents the objects in a column.

iPhone - NSArray - Pick 4 different items

I've NSArray with over 100 strings.
I would like to pick 4 different strings randomly. I can write traditional way of code using for/while loops and get the task done.
But is there any better way to pick 4 different random strings?
Shuffle an array as described in JEFF LAMARCHE's blog and use first four items)
create a NSSet from your NSArray and fetch first 4 elements.
I wrote some utils as category on NSArray.
You could use it like this:
#import "NSArray+RandomUtils.h"
NSArray *array = [NSArray arrayWithObjects:#"aa", #"ab",#"c",#"ad",#"dd", nil];
NSSet *set = [array setWithRandomElementsSize:4];
This will give you a set of 4 unique random elements.
If you want to allow objects to be in your collection more than once you can do:
NSArray *array = [NSArray arrayWithObjects:#"aa", #"ab",#"c",#"ad",#"dd", nil];
NSArray *resultArray = [array arrayWithRandomElementsSize:4];
#import "NSArray+RandomUtils.h"
#implementation NSArray (RandomUtils)
-(NSMutableArray *)mutableArrayShuffled
{
NSMutableArray *array = [[self mutableCopy] autorelease];
[array shuffle];
return array;
}
-(NSMutableArray *)arrayShuffled
{
return [NSArray arrayWithArray:[self mutableArrayShuffled]];
}
-(id)randomElement
{
if ([self count] < 1) return nil;
NSUInteger randomIndex = arc4random() % [self count];
return [self objectAtIndex:randomIndex];
}
-(NSSet *)setWithRandomElementsSize:(NSUInteger)size
{
if ([self count]<1) return nil;
if (size > [self count])
[NSException raise:#"NSArrayNotEnoughElements"
format:#"NSArray's size (%d) is too small to fill a random set with size %d", [self count], size];
NSMutableSet *set = [NSMutableSet set];
NSMutableArray *array = [self mutableArrayShuffled];
if (size == [array count])
return [NSSet setWithArray:array];
while ([set count]< size) {
id object = [array objectAtIndex:0];
[array removeObjectAtIndex:0];
[set addObject:object];
}
return [NSSet setWithSet:set];
}
-(NSArray *)arrayWithRandomElementsSize:(NSUInteger)size
{
if ([self count]<1) return nil;
NSMutableArray *array = [NSMutableArray array];
while ([array count] < size) {
[array addObject:[self randomElement]];
}
return [NSArray arrayWithArray:array];
}
#end
#implementation NSMutableArray (RandomUtils)
-(void)shuffle
{
NSUInteger count = [self count];
for (NSUInteger i = 0; i < count; ++i) {
NSUInteger nElements = count - i;
NSUInteger n = (arc4random() % nElements) + i;
[self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
#end
This has been answered multiple times in the forum. Your key to generate random numbers is
(arc4random() % 100) +1
Above code is capable of generating a random number ranging from 1 to 100. You can use this to get what you want. If you received a random number that you already got, ignore and call again to get a unique random number.

How to sort NSMutableArray elements?

I have model class which contains NSString's- studentName, studentRank and studentImage. I wanna sort the NSMutableArray according to studentRanks. what I have done is
- (void)uploadFinished:(ASIHTTPRequest *)theRequest
{
NSString *response = nil;
response = [formDataRequest responseString];
NSError *jsonError = nil;
SBJsonParser *json = [[SBJsonParser new] autorelease];
NSArray *arrResponse = (NSArray *)[json objectWithString:response error:&jsonError];
if ([jsonError code]==0) {
// get the array of "results" from the feed and cast to NSArray
NSMutableArray *localObjects = [[[NSMutableArray alloc] init] autorelease];
// loop over all the results objects and print their names
int ndx;
for (ndx = 0; ndx < arrResponse.count; ndx++)
{
[localObjects addObject:(NSDictionary *)[arrResponse objectAtIndex:ndx]];
}
for (int x=0; x<[localObjects count]; x++)
{
TopStudents *object = [[[TopStudents alloc] initWithjsonResultDictionary:[localObjects objectAtIndex:x]] autorelease];
[localObjects replaceObjectAtIndex:x withObject:object];
}
topStudentsArray = [[NSMutableArray alloc] initWithArray:localObjects];
}
}
How can I sort this topStudentsArray according to the ranks scored by the Students and If the two or more student have the same rank, How can I group them.
I did like this
TopStudents *object;
NSSortDescriptor * sortByRank = [[[NSSortDescriptor alloc] initWithKey:#"studentRank" ascending:NO] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortByRank];
NSArray * sorted = [topStudentsArray sortedArrayUsingDescriptors:descriptors];
but this is not displaying results properly. please help me to overcome this problem. thanks in advance.
doing something like this might do the trick
Initially sort the arrGroupedStudents in the (ascending/descending) order of studentRank
//Create an array to hold groups
NSMutableArray* arrGroupedStudents = [[NSMutableArray alloc] initWithCapacity:[topStudentsArray count]];
for (int i = 0; i < [topStudentsArray count]; i++)
{
//Grab first student
TopStudents* firstStudent = [topStudentsArray objectAtIndex:i];
//Create an array and add first student in this array
NSMutableArray* currentGroupArray = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
[currentGroupArray addObject:firstStudent];
//create a Flag and set to NO
BOOL flag = NO;
for (int j = i+1; j < [topStudentsArray count]; j++)
{
//Grab next student
TopStudents* nextStudent = [topStudentsArray objectAtIndex:j];
//Compare the ranks
if ([firstStudent.studentRank intValue] == [nextStudent.studentRank intValue])
{
//if they match add this to same group
[currentGroupArray addObject:nextStudent];
}
else {
//we have got our group so stop next iterations
[arrGroupedStudents addObject:currentGroupArray];
// We will assign j-1 to i
i=j-1;
flag = YES;
break;
}
}
//if entire array has students with same rank we need to add it to grouped array in the end
if (!flag) {
[arrGroupedStudents addObject:currentGroupArray];
}
}
Finally your arrGroupedStudents will contain grouped array with equal rank. I have not test run the code so you might need to fix a few things falling out of place. Hope it helps
If you want to display in the order of ranks, you should set the ascending as YES.
NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:#"studentRank" ascending:YES];
static int mySortFunc(NSDictionary *dico1, NSDictionary *dico2, void *context)
{
NSString *studentName1 = [dico1 objectForKey:#"studentName"];
NSString *studentName2 = [dico2 objectForKey:#"studentName"];
return [studentName1 compare:studentName2];
}
- (IBAction)sortBtnTouched:(id)sender
{
[topStudentsArray sortUsingFunction:mySortFunc context:NULL];
}

How to solve Memory leaks for following Sqlite code?

I am getting memory leaks in Instruments in the following Sqlite Code.
NSArray *result = [self executeQuery:sql arguments:argsArray];
It calls following method.
- (NSArray *)executeQuery:(NSString *)sql arguments:(NSArray *)args {
sqlite3_stmt *sqlStmt;
if (![self prepareSql:sql inStatament:(&sqlStmt)])
return nil;
int i = 0;
int queryParamCount = sqlite3_bind_parameter_count(sqlStmt);
while (i++ < queryParamCount)
[self bindObject:[args objectAtIndex:(i - 1)] toColumn:i inStatament:sqlStmt];
NSMutableArray *arrayList = [[NSMutableArray alloc] init];
int columnCount = sqlite3_column_count(sqlStmt);
while ([self hasData:sqlStmt]) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
for (i = 0; i < columnCount; ++i) {
id columnName = [self columnName:sqlStmt columnIndex:i];
id columnData = [self columnData:sqlStmt columnIndex:i];
[dictionary setObject:columnData forKey:columnName];
}
[arrayList addObject:[dictionary autorelease]];
}
sqlite3_finalize(sqlStmt);
return arrayList;
}
How do I solve it ?
We'd need to see the code of your executeQuery method - it should be returning an auto-released result, but perhaps it isn't.
You could try ;
NSArray *result = [[self executeQuery:sql arguments:argsArray] autorelease];
But I'd be wary of just blindly trying that without actually seeing what executeQuery does in detail.
EDIT:
OK, here's your problem;
NSMutableArray *arrayList = [[NSMutableArray alloc] init];
Either create it as an auto-released array, or finish the method with;
return [arrayList autorelease];