variable for textfield Objective c [duplicate] - objective-c

This question already has answers here:
Create multiple numbered variables based on a int
(2 answers)
Closed 8 years ago.
I'm using
for(int k=i;k<6;k++){
int q=k+1;
switch (q) {
case 1:
textbox1.hidden=YES;
break;
case 2:
textbox2.hidden=YES;
break;
case 3:
textbox3.hidden=YES;
break;
case 4:
textbox4.hidden=YES;
break;
case 5:
textbox5.hidden=YES;
break;
case 6:
textbox6.hidden=YES;
break;
default:
textbox1.hidden=NO;
break;
}
}
I was wondering if there isn't anyway to use make something like this:
[#"textbox%#.hidden] = YES
or something like that..
The second question
I have to do something likes this:
[textbox2 setKeyboardType:UIKeyboardTypeDecimalPad];
But since Im on a for .. I can't put textbox2 I need to put "textbox%#, i" So it can detect on which textbox it is analyzing any idea?

You cannot make the replacement you're asking about. It's probably possible to figure out some alternative using reflection but the resulting code will be much uglier than what you've already got. You could, however, take advantage of your q being 1-6 and use an array of text boxes:
id textboxes[] = {textbox1, textbox2, ... textbox6};
if ((q >= 1) && (q <= 6)) textboxes[q-1].hidden = YES;
else textbox1.hidden=NO;

Related

Use variables as case constants in swich statement

I'm trying to use a variable as the case match, however I get "Expression is not an integer in Objective-C.
Is it possible to use variable in switches in this manner?
int count = [array count];
switch ([number to check]) {
case 0:
//first statement
break;
case 1 ... (count -1):
//somewhere between 1 and the next to last statement
//Basically the middle
break;
case count:
//Last statement
default:
break;
}
Objective-C (and C) switch only supports a single primitive constant value for each case statement (or a range as pointed out in the answer by TwoStraws). You would be much better off writing your code using if/else:
if ([number to check] == 0) {
} else if ([number to check] >= 1 && [number to check] < count) {
} else if ([number to check] == count) {
} else {
}
Objective-C's switch statement does support ranges of values as you've seen, but doesn't support variable matches I'm afraid.
So, the below code is valid because I've used exact integers:
int numberOfKittens = 12;
NSString *kittenDescription;
switch (numberOfKittens) {
case 0 ... 5:
kittenDescription = #"Need more kittens";
break;
case 6 ... 10:
kittenDescription = #"That's a good number of kittens.";
break;
case 11 ... 20:
kittenDescription = #"Are you sure you can handle that many kittens?";
break;
default:
kittenDescription = #"You must really love kittens!";
}
…but trying to put a variable in place of any of those will fail.
If this is something you desperately want, consider using Swift because it has a much more expressive switch matching system. Here's that same code in Swift, now with a variable being used to match a case:
let upperLimit = 20
let numberOfKittens = 19
var kittenDescription = ""
switch (numberOfKittens) {
case 0 ... 5:
kittenDescription = "Need more kittens"
case 6 ... 10:
kittenDescription = "That's a good number of kittens."
case 11 ... upperLimit:
kittenDescription = "Are you sure you can handle that many kittens?"
default:
kittenDescription = "You must really love kittens!"
}

How do I loop through or alternate this switch for my iOS game button?

I currently have a button in my game that calls a different ad every time its tapped. Here is the code.
- (IBAction)VideoAds:(id)sender{
int random = arc4random_uniform(2);
switch (random)
{
case 0:
{
VungleSDK* sdk = [VungleSDK sharedSDK];
[sdk playAd:self];
NSLog(#"Case 0 Displayed - Vungle");
}
break;
case 1:
[AdColony playVideoAdForZone:#"APP_ID" withDelegate:nil];
NSLog(#"Case 1 Displayed - AdColony");
break;
}
}
Everything works fine and when I tap the button I get either Case 0 or Case 1 randomly. I want to change that so that get each case in order or for them to alternate back and forth. I do not want random. I know it's probably just a simple 10 second fix, but I've spent over an hour searching Google and Stackoverflow trying to figure out how to do it. Thanks for any help!
Try this :
iHoldCaseNo will be golbal variable which hold the iHoldCaseNo case no to be execute.
- (IBAction)VideoAds:(id)sender{
int random = iHoldCaseNo;
switch (random)
{
case 0:
{
VungleSDK* sdk = [VungleSDK sharedSDK];
[sdk playAd:self];
iHoldCaseNo=1;
NSLog(#"Case 0 Displayed - Vungle");
}
break;
case 1:
[AdColony playVideoAdForZone:#"APP_ID" withDelegate:nil];
NSLog(#"Case 1 Displayed - AdColony");
iHoldCaseNo=0;
break;
}
}
Or alternate way is to set the sender i.e button tag as 0 or 1 and according to that handle the switch case.
//For setting the tag use.
sender.tag=1 in case 0 and sender.tag=1 in case 1
//to get random no,
int random = sender.tag;

Boolean ? : operation syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the question mark and the colon (?: ternary operator) mean in objective-c?
I have seen code where it uses a syntax something like...
someValue = someBoolean ? valueOne : valueTwo;
Or something like this.
I've never used this and I'm not sure what it's called.
Please can someone explain how to use it or provide a link to a resource about it.
It's ternary opertaor.
It evaluates the someBoolean condition.
If it is true then pass the valueOne to someValue
If it is false then pass valueTwo to someValue
It is equal to:
if(someBoolean)
{
someValue = valueOne;
}
else
{
someValue = valueTwo;
}
This is a good link which explains about ternary operator
This is called ternary operator ( ?: )
1 ? 2 : 3
1 is the condition.
2 is executed when 1 it is true.
3 is executed when 1 is false.
Similar to: (Below is not a running code, 1,2,3 shows only placeholders for some expressions and statements.
if(1){ //condition
2 //true
}
else{
3 //false
}
You can shorten it as for :
int bigger;
(10<100) ? bigger=100 : bigger=10;
in short way:
int bigger = (10<100) ? 100 : 10 ;
NOTE:
Its precedence order is among the least and it is much slower then if-else and switch case statements.
It is a ternary operator (also known as the conditional operator). You can find explanation at this link.
Basically your expression is saying that if someBoolean is true someValue will get valueOne if not it will get valueTwo.
It is similar to:
if(someBoolean)
{
someValue = valueOne;
}
else
{
someValue = valueTwo;
}
which offers less visibility in your code. I recommend using this operator in case you want to assign a value which depends on one condition.
Note that it is an expression not specific to Objective-C, you can use it in C and C++ too.
The result of the assignment is valueOne is the condition is true, and valueTwo if the condition is false.
See it here on wikipedia. It also makes the case with other languages, just skip them and see the C syntax example.
Suppose user needs to answer some question and you change background color of your view to red if he was wrong, green if he was correct.
- (void)handleAnswer:(BOOL)correct {
UIColor *color = (correct) ? [UIColor greenColor] : [UIColor redColor];
self.view.backgroundColor = color;
}
It works same as the following
if (someBoolean)
{
someValue = valueOne;
}
else
{
someValue = valueTwo;
}

Obj-C: ++variable is increasing by two instead of one

I am writing a program that asks users yes/no questions to help them decide how to vote in an election. I have a variable representing the question number called questionnumber. Each time I go through the switch-break loop, I add 1 to the questionnumber variable so that the next question will be displayed.
This works fine for the first two questions. But then it skips the third question and moves on to the fourth. When I have more questions in the list, it skips every other question. Somewhere, for some reasons, the questionnumber variable is increasing when I don't want it to.
Please look at the code below and tell me what I'm doing wrong.
Thank you!
Eli
#import "MainView.h"
#import <Foundation/Foundation.h>
#implementation MainView
#synthesize Question;
#synthesize mispar;
int conservative = 0;
int liberal = 0;
int questionnumber = 1;
- (IBAction)agreebutton:(id)sender { ++liberal; }
- (IBAction)disagreebutton:(id)sender { ++conservative; }
- (IBAction)nextbutton:(id)sender
{
++questionnumber;
switch (questionnumber)
{
case 2: Question.text = #"Congress should ...."; break;
case 3: Question.text = #"It is not fair ..."; break;
case 4: Question.text = #"There are two ..."; break;
case 5: Question.text = #"Top quality h..."; break;
default: break;
}
}
#end
It's a bit hard to read, if you can copy it exactly how it is in the implementation file and use the code sample feature for posting code snippets.
To answer the previous question
number++;
That just adds 1 to the value.
number+=anotherNumber;
That will add anotherNumber to number, and is a quick way of saying
number = number + anotherNumber;
As for your code, is there a chance that the nextButton method is being called more then once?
does ++ automatically add 1 to the variable or does it add the variable + itsself
so if questionnumber = 1 then
does ++questionumber add questionnumber + questionnumber if so it will only work the 1st time and will skip 3 so when questionnumber is 2 you would be adding questionnumber + questionnumber which = 4
I would change to questionnumber = questionnumber + 1 or if the language supports it questionnumber += 1
Where do you set the increment seed for ++ i believe this functionality is typically used with a for loop.

How do I use a boolean operator in a case statement?

I just Don't understand how to use a boolean operator inside a switch statement
switch (expression) {
case > 20:
statements
break;
case < -20:
statements
break;
}
Edit:
I don't want an If () statement.
You can't. Use if() ... else ....
The nearest thing available to what you want uses a GCC extension and is thus non-standard. You can define ranges in case statements instead of just a value:
switch(foo)
{
case 0 ... 20: // matches when foo is inclusively comprised within 0 and 20
// do cool stuff
break;
}
However, you can't use that to match anything under a certain value. It has to be in a precise range. Switches can only be used to replace the comparison operator against a constant, and can't be used for anything more than that.
switch ((expression) > 20) {
case true:
statements
break;
case false:
default:
statements
break;
}
What.. you want more than 1 boolean in a case? You could do this
int ii = ((expression) > 20) + 2 * ((expression) < -20);
switch (ii) {
case 1:
statements
break;
case 2:
statements
break;
}
This, IMO is pretty bad code, but it is what you asked for...
Just use the if statement, you'll be better off in the long run.