Array-within-array items replacement - objective-c

I have a list of arrays, each of elements of these is array to. I know, it's a little freaky, but it's necessary. So, I need to replace elements of deepest array. I've tried this:
for (int i = 0; i < [myArray count]; i++) {
for (int j = 0; j < [[myArray objectAtIndex:i] count]; j++) {
for (int k = 0; k < [[[myArray objectAtIndex:i] objectAtIndex:j] count]; k++) {
[[[myArray objectAtIndex:i] objectAtIndex:j] replaceObjectAtIndex:k withObject:#"Some_string"];
}
}
}
and got an error *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance. But I can to log this element, e.g. NSLog(#"%#", [NSString stringWithFormat:#"%#",[[[myArray objectAtIndex:0] objectAtIndex:0]objectAtIndex:0]]);, it's ok.
What it can be? Thanks for your help.

Are your arrays instances of NSMutableArray instead of immutable NSArray?

Related

Compare array of images to another array of images to find duplicate images

i am finding Duplicate image using this code.
for (int i=0; i<arrImageData1.count; i++)
{
for (int j=i+1; j<arrImageData1.count; j++)
{
if ([arrImageData1[i]isEqualToData:arrImageData2[j]])
{
NSNumber *iValue = [NSNumber numberWithInt:i];
NSNumber *jValue = [NSNumber numberWithInt:j];
[arr addObject:iValue];
[arr addObject:jValue];
}
}
}
NSLog(#"%#",arr);
NSLog(#"%lu",(unsigned long)arr.count);
for (int k=0; k<arr.count; k++)
{
[arrDuplicateImages addObject:[arrImage objectAtIndex:[[arr objectAtIndex:k] integerValue]]];
[arrAsset addObject:[self.assetsFetchResults objectAtIndex:[[arr objectAtIndex:k] integerValue]]];
}
when i run on simulator it works perfectly but when i run it in device it gives me lots of duplicate images.number of images in device is 1206 and it gives me duplicate images 84448. any solution for this?

Error with using local variable to access array index

For some reason this occurs every time i try to access a specific index from the array by specifying a variable int in the index in a forloop. When i do i get a Thread 1 Error, but if i use a variable that has not be declared in the forloop itself than it seem to work fine.
Code:
for(int i =0 ; i<= [array count]; i++) {
NSNumber *convert = [array objectAtIndex:i]; //results in error
NSLog(#"%i", [convert intValue]);
}
Problem is you are trying to access array beyond its capacity. Array starts with 0 index and goes upto array.count - 1. That said, please try with below code and you should be good:
for (int i = 0 ; i <= array.count - 1 ; i++) {
NSNumber *convert = [array objectAtIndex:i];
NSLog(#"%i", [convert intValue]);
}
Another variation could be:
for (int i = 0 ; i < array.count ; i++) {
NSNumber *convert = [array objectAtIndex:i];
NSLog(#"%i", [convert intValue]);
}

Error with index 1 beyond bounds

As following code I wanna send matrix1Col and matrix2Col to multiply method.
But it's error with index 1 beyond bounds.
From inside UIButton code
double kk[2][2] = {{1,2},{5,6}};
double t [2][1] = {1,2};`
if (!matrix1Col) {
matrix1Col = [NSMutableArray array];
}
for (unsigned int i=0; i<2; i++) {
matrix1Row = [NSMutableArray new];
for (unsigned int j=0 ; j<2; j++) {
[matrix1Row addObject:#(kk[i][j])];
}
[matrix1Col addObject:matrix1Row];
}
if (!matrix2Col) {
matrix2Col = [NSMutableArray array];
}
for (unsigned int i=0; i<2; i++) {
matrix2Row = [NSMutableArray new];
for (unsigned int j=0; j<1; j++) {
[matrix2Row addObject:#(t[i][j])];
}
[matrix2Col addObject:matrix2Row];
}
NSMutableArray *resultMultiply = [self multiply:matrix1Col :matrix2Col];
Another method in ViewController.m:
-(NSMutableArray*)multiply:(NSMutableArray*)matrix1 :(NSMutableArray*)matrix2{
int matrix1RowCount = [matrix1 count];
int matrix2RowCount = [matrix2 count];
int matrix2ColCount = [[matrix2 objectAtIndex:0] count];
NSMutableArray *multiplyMatrix = [NSMutableArray arrayWithCapacity:matrix1RowCount];
for (int i=0; i< matrix1RowCount; i++) {
NSMutableArray *matrixInRow = [NSMutableArray arrayWithCapacity:matrix2ColCount];
for (int j=0; j<matrix2RowCount; j++) {
double valueTotal = 0;
for (int k=0; k<matrix2ColCount; k++) {
double value1 = [[[matrix1 objectAtIndex:i] objectAtIndex:k] doubleValue];
double value2 = [[[matrix2 objectAtIndex:k] objectAtIndex:j] doubleValue];
valueTotal += value1*value2;
}
[matrixInRow addObject:[NSNumber numberWithDouble:valueTotal]];
}
[multiplyMatrix addObject:matrixInRow];
}
return multiplyMatrix;
}
This is error code:
2013-02-14 06:26:06.595 matrixMutableArray[21578:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
I can't find it where it's error.
Thanks you for your comment.
The error occurs on this row:
double value2 = [[[matrix2 objectAtIndex:k] objectAtIndex:j] doubleValue];
When the error occurs k=0 and j=1. The objectAtIndex:0 for matrix2 only has one object, hence the error.

Displaying factors of a number

I want to display every factor of a number typed in a textfield to achieve this I tried using an array. But I am always getting the error 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' . Any suggestions to eliminate this error?
NSMutableArray *array;
array = [NSMutableArray arrayWithCapacity:100];
for (factorsNumber=1; factorsNumber<=number; factorsNumber++) {
if (number%factorsNumber == 0) {
[array addObject:[NSString stringWithFormat:#"%d", factorsNumber]];
}
}
for (int i = 0; i <= [array count]; i++) {
factors.text = [NSString stringWithFormat:#"%d", i, [[array objectAtIndex:i] intValue]];
}
for (int i = 0; i <= [array count]; i++) {
should be
for (int i = 0; i <= [array count] - 1; i++) {
or
for (int i = 0; i < [array count]; i++) {
The valid indices of an n-element array are 0 to n-1.
In your for loop, remove the = so that it reads:
for (int i = 0; i < [array count]; i++)
you problem is here:
for (int i = 0; i < [array count]; i++) { // < instead of <=
factors.text = [NSString stringWithFormat:#"%d", i, [[array objectAtIndex:i] intValue]];
}

How to copy from NSMutableArray to NSMutableString?

Im trying to add some strings into NSMutableArray,than want to copy element 0(zero)
from NSMutableArray to NSMutableString
[MS appendString:str]
this line raises error.
what is the problem ?
a MutableArray element cannot be assigned to NSString ?
(thanks for any responses)
error output is :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x6c82440'
here is my code :
NSMutableArray *A=[[NSMutableArray alloc]init];
NSMutableString *MS=[NSMutableString stringWithString:#"first"];
for (int i=0; i<=15;i+=2) {
[A addObject:[NSNumber numberWithInteger:i]];
}
NSString *str;
for (int x=0; x<[A count]; x++) {
str=[A objectAtIndex:0];
[MS appendString:str];//ERROR HERE
}
[A release];
You are adding NSNumbers to the array and not strings n the folioing line:
[A addObject:[NSNumber numberWithInteger:i]]; // here you are adding numbers
So when you read them from the array later, they are still numbers and you have to create strings from them:
NSString *str;
NSNumber *number;
for (int x=0; x<[A count]; x++) {
number = [A objectAtIndex:0]; // You should probably replace 0 by x here.
str = [number stringValue]; // transform number into a string
[MS appendString:str];
}
[A release];
But if you want the array to store strings and not numbers, then replace the first loop instead with the following:
for (int i=0; i<=15;i+=2) {
[A addObject:[[NSNumber numberWithInteger:i] stringValue]];
}
Or
for (int i=0; i<=15;i+=2) {
[A addObject:[NSString stringWithFormat:#"%d", i]];
}