So, I'm kind of new to Obj-c, and I have a strange crash happening with the following code :
- (NSMutableArray*)followNonBlackPixels:(int)startX withY:(int)startY
{
NSMutableArray* result;
NSMutableArray* adjacents = [self getAdjacents:startX withY:startY];
int r = 0;
int i = 0;
int tempX;
int tempY;
int max = [adjacents count];
CGPoint tempPoint;
while(i < max)
{
int tempX = (int)[[adjacents objectAtIndex:i] CGPointValue].x;
int tempY = (int)[[adjacents objectAtIndex:i] CGPointValue].y;
result = [self getAdjacents:tempX withY:tempY];
for(r = 0; r < [result count]; r++)
{
tempPoint = [[result objectAtIndex:r] CGPointValue];
//[adjacents addObject:[NSValue valueWithCGPoint:CGPointMake(tempPoint.x, tempPoint.y)]];
}
i++;
max = [adjacents count];
}
return adjacents;
}
This code runs fine, but as soon as I uncomment the line where I add an object to the "adjacents" NSMutableArray, then the program crashes.
The signature of the getAdjacents method is as follows:
- (NSMutableArray*)getAdjacents:(int)startX withY:(int)startY;
I'm developing a Cordova plugin under Windows so I do not have any debug info to provide... But maybe my mistake would be clear to an experienced obj-c developer ?
Thanks a lot for you help !
If you change your code to the following,
NSMutableArray *adjacents = [[NSMutableArray alloc] initWithArray:[[self getAdjacents:startX withY:startY] mutableCopy]];
Related
I am trying to copy a specific values at specific index from one array to another like this:
for (int i = 0; i < 100; i++) {
if ([subID[i] isEqual: #"0"]) {
NSLog(#"state : %#",arrayTempState[i]);
NSString *str = arrayTempState[i];
[arrayState addObject:str];
NSLog(#"%#",arrayState[i]);
}
arrayState is NSMutableArray and arrayTempState is NSArray
but arrayState is null every time.
I tried arrayState[i] = arrayTempState[i]; but it did not work.
arrayState was not initialised that is why it couldn't store the value.
plz try this
arrayState = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
if ([subID[i] isEqual: #"0"]) {
NSLog(#"state : %#",arrayTempState[i]);
NSString *str = arrayTempState[i];
[arrayState addObject:str];
NSLog(#"%#",arrayState[i]);
}
I have the following code and i am trying to wrap the 3D array of integers in an NSArray because i cannot return multidimensional arrays in C. Can anyone suggest how i could achieve it an a way that allows for easy access to the arrays after the function is returned?
-(NSArray *) Foo{
int array[2][[self getWidth]][[self getHeight]];
int x = 0;
int y = 0;
for (int i = 0; i < [self getWidth]; i++) {
for (int j = 0; i < [self getHeight]; j++) {
// get th values for current position
tmp = [NSMutableArray arrayWithArray:[self convertAtPoint:i withYValue:j]];
x = (int) [tmp objectAtIndex:0];
y = (int) [tmp objectAtIndex:1];
array[0][i][j] = x;
array[1][i][j] = y;
}
}
// How can i put the 3D array into a NSArray?
NSArray *returnArr = [[NSArray alloc] init];
return returnArr;
}
I ended up not using NSArray and reverted to a C array using malloc to allocate the space necessary for the array.
I'm a total noob and need help with a method I'm writing.
The method creates a deck of cards (using NSMutableArray). I'm first experimenting with loading the array with numbers 1-13 randomly (each number appearing once).
When I run a simple test program to print the values in the array, I get a "Build Successful" but an error once the program starts. The error says "[__NSArrayM insertObject:atIndex:]: object cannot be nil".
Once I understand what I'm doing wrong, I can then expand on the method properly. Thanks!
NB: This is my first post. Is this type of question ok?
- (void) createDeck {
int r;
BOOL same;
deck = [[NSMutableArray alloc]init];
NSNumber *randNum;// = nil;
randNum = [[NSNumber alloc]init];
[randNum initWithInt: (arc4random()%13)+1];
[deck addObject: randNum]; // First card added to deck
same = FALSE;
while (!same) {
for (int i=1; i<13; i++) {
same = FALSE;
for (r=0; r<=i; r++) {
[randNum initWithInt: (arc4random()%13)+1];
if ([deck objectAtIndex:r] == [deck objectAtIndex:i]) {
same = TRUE;
}
[deck addObject: randNum]; // Next card added to deck
}
}
}
}
you cannot re-init randNum:
NSNumber *randNum;// = nil;
randNum = [[NSNumber alloc]init];
[randNum initWithInt: (arc4random()%13)+1];
and the third line is missing the assignment anyway. just do:
NSNumber *randNum = [[NSNumber alloc] initWithInt:(arc4random()%13)+1];
and put that inside the inner for loop, like this:
BOOL same = FALSE;
NSMutableArray *deck = [[NSMutableArray alloc] initWithCapacity:13];
[deck addObject:[[NSNumber alloc] initWithInt:(arc4random()%13)+1]]; // First card added to deck
while (!same) {
for (int i = 1; i < 13; i++) {
same = FALSE;
for (int r = 0; r <= i; r++) {
NSNumber *randNum = [randNum initWithInt:(arc4random()%13)+1]; // modern ObjC will assign a register for this outside the while loop, but restrict the variable's scope to the inner-most loop
if ([deck objectAtIndex:r] == [deck objectAtIndex:i])
same = TRUE;
[deck addObject: randNum]; // Next card added to deck
}
}
Note that I haven't thought through the logic of what you are trying to do here, I've only attempted to resolve the NULL object reference. The error was referring to the first line [deck addObject: randNum] outside of the loop.
Try to use this line of code where you using NSNumber
NSNumber * randNum = [NSNumber numberWithInt: (arc4random%13)+1];
Instead of
[[NSNumber alloc] initWithInt: ]
I'm having trouble with this block of code:
for (int i = 0; i < [tempInviteeArray count]; i++)
{
NSArray *tempContact = [tempInviteeArray objectAtIndex:i];
NSDictionary *tempContactDictionary = [tempContact objectAtIndex:1];
int tempContactDelay = [[tempContact objectAtIndex:2] intValue];
FlokContact *tempContact = [[FlokContact alloc] initWithJSONData:tempContactDictionary andDelay:tempContactDelay];
}
That last line throws an error:
"Redefinition of 'tempContact' with a different type
initWithJSONData: accepts NSDictionary
andDelay: int
I've tried to rewrite this code, with different types and all,
I'm just not sure what I'm doing
You already declared a variable in this scope named tempContact (NSArray *tempContact...). Change the name of one of them.
NSArray *tempContact and FlokContact *tempContact have the same name, that's the problem.
Change FlokContact *tempContact to FlokContact *temp_Contact or what you want.
try this
for (int i = 0; i < [tempInviteeArray count]; i++)
{
NSArray *tempContact = [tempInviteeArray objectAtIndex:i];
NSDictionary *tempContactDictionary = [tempContact objectAtIndex:1];
int tempContactDelay = [[tempContact objectAtIndex:2] intValue];
{
FlokContact *tempContact = [[FlokContact alloc] initWithJSONData:tempContactDictionary andDelay:tempContactDelay];
}
}
I have my sqlite execute query code as below. Instruments catches a memory leak in the NSDictionary alloc and release lines (inside the while loop). Can someone point out what is wrong in those alloc/release ?
- (NSMutableArray *)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]; // instrument marks leak 0.1 % on this line
NSUInteger rcnt= arrayList.retainCount;
int columnCount = sqlite3_column_count(sqlStmt);
while ([self hasData:sqlStmt]) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; // instrument marks leak 13% on this line
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];
[dictionary release];// instrument marks leak 86.9 % on this line
}
sqlite3_finalize(sqlStmt);
rcnt=arrayList.retainCount;
return arrayList ;
}
Any help/pointer is very much appreciated. Have been struggling with this for few days.
If you are not using ARC then I suggest you change your return line for:
return [arrayList autorelease];
Otherwise you are probably leaking the complete arrayList.