Objective-C: Adding to variable before assignment - objective-c

I have the following code:
NSInteger variableScene = 10;
NSInteger numberOfRowsXX = //an Integer value; <-- I would like replace XX with the value of 'variableScene'
So it'll look like this:
NSInteger numberOfRows10 == //an Integer value;
How can I replace XX with the value of variableScene?
*Update*
Why I'm trying accomplish this?
It's a bit complicated. I'm using Core Data with remote Database. In a nutshell: I have 10 Scenes that'll be presented based on the User selection order. For each Scene I need to assign the numberOfRows to present at the end of all the Scenes with a UITableViewController.
I have a variableScene that I pass around from Scene to Scene.
I need to assign numberOfRowsSceneXX = //an integer
XX will come from variableScene, which could be any number from 1 - 10.
So when we get to the last scene, we'll have a value for each Section (numberOfRowInSection) which will be represented by each Scene: numberOfRowScene1, numberOfRowsScene2, etc.
I tried to simplify the question. Hope it makes sense.

Use arrays.
set it up as:
#define MAX_SCENES 10
NSInteger numberOfRows[MAX_SCENES];
then when you want to set the value:
if (variableScene < MAX_SCENES) {
numberOfRows[variableScene] = variable;
}

Related

Selection a bool through randomizer

I have a total of 6 booleans and the only thing separating them is a number. They're named checker0 though 5.
So checker0, checker1, checker2, checker3, checker4 and checker5.
All of these grants or denies access to certain parts of the app wether the bool is true or false.
I then have a randomiser using:
randomQuestionNumber = arc4random_uniform(5);
So say we get number 3, checker3 = true;
But my question now is would it be possible to set this one to true without having to go thru if statements.
My idea was to implement the way you print a int to say the NSLog using the %d.
NSLog(#"The number is: %d", randomQuestionNumber);
So something like:
checker%d, randomQuestionNumber = true.
Would something like that be possible? So i won't have to do like this:
if (randomQuestionNumber == 0) {
checker0 = true;
}
else if (randomQuestionNumber == 1)
{
checker1 = true;
}
Thanks you very much! :)
Every time you find yourself in a situation when you name three or more variables checkerN you know with a high degree of probability that you've missed a place in code where you should have declared an array. This becomes especially apparent when you need to choose one of N based on an integer index.
The best solution would be to change the declaration to checker[6], and using an index instead of changing the name. If this is not possible for some reason, you could still make an array of pointers, and use it to make modifications to your values, like this:
BOOL *ptrChecker[] = {&checker0, &checker1, &checker2, ...};
...
*ptrChecker[randomQuestionNumber] = true;

Is there a less ugly way to do input in D than scanf()?

Currently the only way I know how to do input in D is with the scanf() function. But god damn it's ugly. You would think that since it's an upgrade from C that they would have fixed that.
I'm looking for a way to do it with a single argument. Currently you have to do:
int foo = 0;
scanf("%i", &foo);
writeln("%i", foo);
But it would look a lot cleaner with a single argument. Something like:
int foo = 0;
scanf(foo);
writeln(foo);
Thanks.
readf("%d", &foo); allows working with std.stdio.File rather than C FILE*
foo = readln().strip().to!int();
For reading entire files with lines formatted in the same way:
int[] numbers = slurp!int("filename", "%d");
There's a really cool user-input module here:
https://github.com/Abscissa/scriptlike/blob/master/src/scriptlike/interact.d
Example code:
if (userInput!bool("Do you want to continue?"))
{
auto outputFolder = pathLocation("Where you do want to place the output?");
auto color = menu!string("What color would you like to use?", ["Blue", "Green"]);
}
auto num = require!(int, "a > 0 && a <= 10")("Enter a number from 1 to 10");
The above answers are great. I just want to add my 2 cents.
I often have the following simple function lying around:
T read(T)()
{
T obj;
readf(" %s", &obj);
return obj;
}
It's generic and pretty handy - it swallows any white space and reads any type you ask. You can use it like this:
auto number = read!int;
auto floating_number = read!float;
// etc.

How do I compare a constant value to a continuously-updated Accelerometer value each time round a loop?

As it was suggested to me in a previous post of mine, the following code takes the data coming from the accelerometer the "minute" the assignment : CMAccelerometerData* data = [manager accelerometerData]; is performed, and then extracts from that data the acceleration exercised on the x-Axis and stores its value in a double (double x) :
CMMotionManager* manager = [[CMMotionManager alloc] init];
CMAccelerometerData* data = [manager accelerometerData];
double x = [data acceleration].x;
Suppose the value stored is 0.03 and suppose that I want to use it in a while loop as follows :
while (x > 0)
{
// do something
}
the above loop will obviously run forever
However, what if I used the following code instead :
CMMotionManager* manager = [[CMMotionManager alloc] init];
while([[manager accelerometerData] acceleration].x > 0)
{
// do something
}
wouldn't I be now comparing zero to a different value each time round the loop?
(which is what I'm going for in my project anyway..)
any thoughts?
the reason I'm asking this is the following :
I want to check the values coming from the x-Axis over a certain period of time, rather than keep checking them at regular intervals, so I basically want to write a loop that would look something like this :
if ([[manager accelerometerData] acceleration].x > 0 )
{
// initialiseTimer
}
while ([[manager accelerometerData] acceleration].x > 0 )
{
if( checkTimer >=250ms )
{
stopTimer;
printOut("X-Axis acceleration was greater than zero for at least 250ms");
breakFromLoop;
}
}
I know the code in my 2nd if-block isn't valid Objective-C..This was just to give you an idea of what I'm going for..
This has a simple solution.
1)Declare an instance variable x that you update each time the accelerometer tell you to.
2)Compare this x to whatever value you need in the loop .
Hope this helps.
Regards,
George

Check how close UILabels are to each other

I am trying to make a game which has a jumbled up algebra equation. I have assigned an outlet to each component of the equation. For example, if the equation was:
x2 + y = 2(a+b)
Then x2 (which is x squared), +, y, = and 2(a+b) would all be its own outlet. But the equation is going to be jumbled up and I want the user to move the label outlets to the correct order. I have enabled touchesMoved, but my problem lies in checking if the equation is in the correct order. I would wrap the code into an IBAction button action, but how do I analyze the text? Would I check for the offset between each label? Is there an easy way/API to do this? Thanks!
Assuming your labels are called xLabel,plusLabel, yLabel, equalsLabel, and abLabel, you could do something like this:
NSUInteger xLeftBorder = CGRectGetMinX(xLabel.frame);
NSUInteger plusLeftBorder = CGRectGetMinX(plusLabel.frame);
NSUInteger yLeftBorder = CGRectGetMinX(yLabel.frame);
NSUInteger equalsLeftBorder = CGRectGetMinX(equalsLabel.frame);
NSUInteger abLeftBorder = CGRectGetMinX(abLabel.frame);
if(xLeftBorder < plusLeftBorder && plusLeftBorder < yLeftBorder && yLeftBorder < equalsLeftBorder && equalsLeftBorder < abLeftBorder){
//Correct!
}
else{
//Incorrect
}
This is kind of clumsy, but it works. An even better way to do it would be to put this in a function with each parameter being a label to check. For example:
bool isCorrect = [self checkIf:xLabel isLessThan: plusLabel isLessThan: yLabel isLessThan:equalsLabel isLessThan:abLabel];
This is assuming the function you write returns a bool.
Hope this helped!

Redefine / resize C array in Objective C?

I have a C array in Objective C defined as follows:
id keysArray;
Then in an if block, i would like to redefine the array based on a condition:
if (somethingIsTrue){
id keysArray[4][3];
}
else {
id keysArray[6][1];
}
Then outside of the if block, when i access the array, i get errors saying the keysArray does not exist.
Thanks.
That's because when you leave the scope of the if, all local variables defined within that scope are destroyed. If you want to do this, you will have to use dynamic allocation. I don't know the Objective C way of doing things, but in regular C you shall use malloc.
In C, once created, arrays cannot change size. For that you need pointers and malloc() and friends.
In C99 there's a new functionality called "variable length array" (VLA) which allows you to use arrays with lengths defined at run time (but fixed for the duration of the object)
while (1) {
/* C99 only */
int rows = 1 + rand() % 10; /* 1 to 10 */
int cols = 1 + rand() % 10; /* 1 to 10 */
{
int array[rows][cols];
/* use array, different sizes every time through the loop */
}
}