I'm trying to output and C++ Array (int inverse[3]) using NSLog, but If I try this way:
NSLog([NSString stringWithFormat:#"%d", inverse]);
It just dont work, But if I try like this:
NSLog([NSString stringWithFormat:#"%d", inverse[0]]);
I get the right output.
My objective is to get the whole array outputed.
Any ideas? Thanks
Use a for loop to print all the values.
for (int i=0; i<3; i++) {
NSLog(#"%i", inverse[i]);
}
or:
NSLog(#"%i, %i, %i", inverse[0], inverse[1], inverse[2]);
There is no need need to convert for string format conversion. You can print like these -
for ( int i=0; i<3; ++i )
NSLog(#"%i", inverse[i]);
Related
I'm trying to write all the hex values, from 0 - 255 in a for loop. It's not printing in hex. I think the %x is what is causing the issue.
int i=0;
for (i = 0; i < 256; i++)
{
[_d writeXYZ:[NSString stringWithFormat:#"%x", i]];
}
Does anyone know the proper syntax to display 0-255 in hex?
When I do it like this:
int i=0;
for (i = 0; i < 256; i++)
{
NSLog(#"%x", i);
}
It runs perfectly, so it looks like my issue is within NSString.
The code below produces and error in the line [myArray objectAtIndex:i]; and I can't seem to figure out why?
Any ideas?
int total = 0;
for (int i = 0; i < myArray.count; i++) {
int tempNumber = [myArray objectAtIndex:i];
total = total + tempNumber;
}
It could be because you are setting an object to an int. By definition, objectAtIndex returns an object.
Depending on the type of object in myArray, you can try something like this:
int tempNumber = [[myArray objectAtIndex:i] intValue];
If you didn't put ints into your array, then you need to do something extra to get ints out. If you're getting an error with your code above, then it's because you don't have ints in the array, and you need
int myInteger = [[myArray objectAtIndex:i] intValue];
Or something to get the ints out of the array, depending on the rest of your code.
If I have an NSArray, and I want to get each of the numbers in it, is there a simple way to do that?
I tried the advice from
http://code.flamingleaf.com/category/objective-c/
that said instead of (the method used in other programming languages for subscripts within arrays):
someArray[i]
to use
[someArray objectAtIndex:i];
Is there some way to get the numeric value?
I keep getting an error of the type:
“Subscript requires size of interface 'NSArray', which is not constant in non-stable ABI”
Thank you for the suggestion by Kurt Revis to put in real code. I could not recreate the problem. But this is my closest:
for (int i=0; i<4; i++)
NSLog(#"%i",[[temp objectAtIndex: i]length]);
for (int i=0; i<4; i++)
tempNumbers[i]=[[temp objectAtIndex:i] length];
for (int i=0; i<4; i++)
NSLog(#"The count at i=%i is %i", i, tempNumbers[i]);
and it worked as:
3
3
5
2
the value of i = 0 and tempNumbers[i]=3.000000
the value of i = 1 and tempNumbers[i]=3.000000
the value of i = 2 and tempNumbers[i]=5.000000
the value of i = 3 and tempNumbers[i]=2.000000
Conclusion: Until I can write simple code to recreate the problem, I won't clog this question area.
2nd Conclusion: I will try the solution suggested by edc1591.
It's hard to tell without seeing what class the objects are, but if they are of type NSString or NSNumber you can use any of the following methods to get the numerical value:
[[someArray objectAtIndex:i] floatValue];
[[someArray objectAtIndex:i] doubleValue];
[[someArray objectAtIndex:i] intValue];
[[someArray objectAtIndex:i] unsignedIntValue];
etc...
How would you go about storing a 2 dimensional array of ints as a class variable?
If you want an array of ints you go:
Class declaration
int * myInts;
Implementation
int ints[3] = {1,2,3};
myInts = ints;
But what if you want to store an array of arrays with ints?
Like this:
int ints[3][3] = {{1,2,3}, {1,2,3}, {1,2,3}};
I don't wanna limit the size of the arrays in the class declaration so I guess I have to go with pointers, but how?
For future reference, this is my conclusion:
Class declaration
int ** ints;
Implementation
int rows = 2;
int cols = 5;
ints = (int**)malloc(rows*sizeof(int*));
ints[0] = (int*)malloc(cols*sizeof(int));
ints[0][0] = 123;
ints[0][1] = 456;
ints[0][2] = 789;
// etc
This is my own interpretation of links provided in comments and my C skills are pretty low so take that into consideration ;) Maybe there are better ways to put in multiple numbers at a time with {123,456,789} or something, but that is beyond my requirements for now!
I've wrote sample for you:
int N = 10, M = 15;
NSMutableArray *ints = [NSMutableArray arrayWithCapacity:N]; // array[N][M]
for (int i=0; i<N; i++)
{
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:M];
for (int j=0; j<M; j++)
{
[arr addObject:[NSNumber numberWithInt:(i+1)*(j+1)]];
}
[ints addObject:arr];
}
// print
for (int i=0; i<[ints count]; i++)
{
NSString *line = #"";
NSMutableArray *arr = [ints objectAtIndex:i];
for (int j=0; j<[arr count]; j++)
line = [NSString stringWithFormat:#"%# %#", line, [arr objectAtIndex:j]];
NSLog(#"%#", line);
}
If you want to dynamically allocate memory, in other words define the size of the arrays at runtime, then you need to declare the array as a pointer, malloc it, and then add another array of ints to each index at runtime. You can't really declare and dynamically allocate at the class level. If you are using cocoa/iphone sdk you can use NSMutableArray.
You could also create your own class that constructs a two dimensional array and exposes methods to push and pop int objects like [IntegerArray push:x,y,n];
Here's and example of using a double reference as Daniel R Hicks pointed out.
I have this code
NSArray *food = [NSArray arrayWithObjects:#"Apples:",#"bacon",#"corn",#"donuts",#"elfs",#"fidge",nil];
for(int i = 0; i<6; i++){
NSLog(#"item at index %i is %#",i,[food objectAtIndex:i]);
}
and right now they are all printed to the console instantly. How can I make a variable to decrease or increase the speed they are logged?
I'm new at objective-C so thanks a lot for your help! :)
NSArray *food = [NSArray arrayWithObjects:#"Apples:",#"bacon",#"corn",#"donuts",#"elfs",#"fidge",nil];
// the number of seconds to wait between printing each item
double secondsToSleep = 1.0;
for(int i = 0; i<6; i++){
[NSThread sleepForTimeInterval:secondsToSleep];
NSLog(#"item at index %i is %#",i,[food objectAtIndex:i]);
}
There's a sleepForTimeInterval: method on NSThread that might do what you're looking for. The documentation is here.
Edit: Sorry, for Objective-C newbies, you would just type something like this:
[NSThread sleepForTimeInterval:0.01];
See the sleep() function.