if (stash != 0) {
for (i=1; i<=6; i++) {
a[1][i]=a[1][i]/stash;
}
}
else
{
NSLog (#"Matrix is Not Invertible");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Matrix is not invertible!" delegate:nil cancelButtonTitle:#"Review Input" otherButtonTitles:nil, nil];
[alert show];
}
I Want to stop the program if the variable "stash" is zero, but i can't use break since it's not in a loop, i wanted to use return but it says that void should not return any value... what should i do to get this working? thanks for all your help....
I don't know if you need to exit the method only but you could use:
return;
or redefine the method to return an integer -(int)myMethod; and then return 0;
//Add return statement, returning nothing
return;
- (void)yourMethod {
//your code
//your declerations
if (stash != 0) {
for (i=1; i<=6; i++) {
a[1][i]=a[1][i]/stash;
}
}
else
{
NSLog (#"Matrix is Not Invertible");
UI *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Matrix is not invertible!" delegate:nil cancelButtonTitle:#"Review Input" otherButtonTitles:nil, nil];
[alert show];
//Add return statement here, returning nothing
return;
}
//other code in your method
//your code
}
Related
I'm trying to create a function in order to save some code, however I have no idea how I should do it.
if (count > 100 && count < 120)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:[NSString stringWithFormat:#"You're really good at this! You scored %i points", count - 1]
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert show];
}
else if(count > 120)
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:[NSString stringWithFormat:#"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1]
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert2 show];
}
else
{
UIAlertView *alert3 = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:[NSString stringWithFormat:#"Not very good, you scored %i points", count - 1]
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert3 show];
}
As you can see the code is very repetitive and I would like to create a function where I can change the value of the "alert" variable and the message string. So when I call the function I only enter the functions name and the value the alert variable should have, in this case; 1, 2 and 3 and the message text depending on what condition we are looking at.
So is there some way of doing this? There has to be, because repetitive code like this is never good looking! I would appreciate if someone could help me out, I've tried looking at some references but none of them made me get any closer to solving the problem.
Simply look at the duplicated functionality and examine the values that are different. In this case it's just the message:
NSString *message = nil;
if (count > 100 && count < 120)
{
message = [NSString stringWithFormat:#"You're really good at this! You scored %i points", count - 1];
}
else if(count >= 120)
{
message = [NSString stringWithFormat:#"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1];
}
else
{
message = [NSString stringWithFormat:#"Not very good, you scored %i points", count - 1];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:message
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
[alert show];
(Edit: You missed the value 120 which would have slipped through the cracks).
I am create a new function that creates a new alert with given message :
-(void)showAlertWithMsg:(NSString *)msg tag:(NSInteger)tag
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Time is up!"
message:msg
delegate:self
cancelButtonTitle:#"Play Again"
otherButtonTitles:nil];
alert.tag = tag;
[alert show];
}
To call this function write the below code:
if (count > 100 && count < 120)
{
[self showAlertWithMsg:[NSString stringWithFormat:#"You're really good at this! You scored %i points", count - 1] tag:1];
}
else if(count > 120)
{
[self showAlertWithMsg:[NSString stringWithFormat:#"TEACH ME MASTER, YOU'RE A GOD! You scored %i points", count - 1] tag:2];
}
else
{
[self showAlertWithMsg:[NSString stringWithFormat:#"Not very good, you scored %i points", count - 1] tag:3];
}
One more thing I am also pass the tag.So if you want to perform different operation for different alert.
You can set "title" property exactly for existing UIAlertView instance:
UIAlertView * alertView = [[UIAlertView allow] init...];
alertView.title = #"Some message here.";
I have an app that uses Motion activity feature. On the first run there is a pop up asking user permission to use the feature. But after re-installing the app, the pop up does not show up anymore.
Is this an intended behavior or am I missing something?
so it figures you need to instantiate CMMotionActivityManager for this to work.
make sure you don't miss out on the first line
_motionActivityManager =[[CMMotionActivityManager alloc] init];
if([CMMotionActivityManager isActivityAvailable])
NSLog(#"available");
else
NSLog(#"not available");
[_motionActivityManager startActivityUpdatesToQueue:[NSOperationQueue new] withHandler: ^(CMMotionActivity *activity) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"activity"
message: activity.description
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[theAlert show];
if(activity.stationary)
{
NSLog(#"stationatry");
}
if(activity.walking)
{
NSLog(#"walking");
}
if(activity.automotive)
{
NSLog(#"automotive");
}
if(activity.cycling)
{
NSLog(#"cycling");
}
if(activity.unknown)
{
NSLog(#"unknown");
}
});
}];
I am new to programming. I am having trouble finding out what all is wrong with this. It is an alert view that i am trying to randomize the text displayed in the message.
-(void)alert:(id)sender{
int randomNumber;
randomNumber = (randomNumber() %3 + 1);
NSLog(#"%i", randomNumber);
if (randomNumber == 1) {
self.YouWin.text = [NSString stringWithFormat:#"You Win"];
}
else if (randomNumber == 2) {
self.YouWin.text = [NSString stringWithFormat:#"You Lose"];
}
else if (randomNumber == 3) {
self.YouWin.text = [NSString stringWithFormat:#"Tie"];
}
NSLog(#"%#",YouWin);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello" message:[NSString stringWithFormat:#"%#",YouWin] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
button.hidden = YES;
Try this one:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:self.YouWin.text
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
You needed text-value stored in YouWin, but you passed the YouWin object itself.
*Note: you can use arc4random() for generating random numbers.
There are many good suggestions; I agree with AKB8085.
Replacing the randomNumber() with arc4random() will help at compile time.
But you might want to re-think implementing a random number generator. The reason is in fairness to your user. I pose the question, “Is it fair to assume you want your user to guess a number with this large of a number range?”
Did you Know?
Using the arc4random(3), rand(3), or random(3) you are using a C function.
You are asking the user to guess a number with the following ranges:
arc4random(3) = 0 to 4294967296
rand(3) and random(3) that top out at a RAND_MAX of 0x7fffffff (214748647)
To help in answering your question, answer the following requirement questions:
Is there a min/max range restraint?
What type of compelling delays will happen
by using arc4random(3), rand(3), or random(3)?
Is using NSArray like in the Fisher–Yates_shuffle a better answer?
SUGGESTION:
Read an article on random numbers and NSArray.
NOTE:
Random numbers tend to task the compiler and your user experience will be hindered.
As Anoop noted you are using stringWithFormat but you're not providing a format string at all.
You should do
[NSString stringWithFormat:#"%#", #"You Win"];
but that's extremely redundant, although correct, and it's totally equivalent to just using #"You Win".
Also an advice for the general approach on the problem. Instead of having a big if-else statement, it's better to store all your string into a data structure and then randomly access to it.
In code this would translate to
NSArray * outcomes = #[ #"You Win", #"You lose", #"Tie" ];
int randomIndex = arc4random_uniform(outcomes.count);
NSString * randomOutcome = outcomes[randomIndex];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:randomOutcome
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
button.hidden = YES;
Note the usage of arc4random_uniform() that gives you back a random number between 0 and the argument provided, excluded.
Replace
randomNumber = (randomNumber() %3 + 1);
with
randomNumber = arc4random() %3 + 1;
Also use this...
if (randomNumber == 1) {
self.YouWin.text = [NSString stringWithFormat:#"You Win"];
}
else if (randomNumber == 2) {
self.YouWin.text = [NSString stringWithFormat:#"You Lose"];
}
else if (randomNumber == 3) {
self.YouWin.text = [NSString stringWithFormat:#"Tie"];
}
NSLog(#"%#",YouWin);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:self.YouWin.text
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
button.hidden = YES;
I am trying to make a guessing app for school.
-(IBAction)check:(id)sender
{
if (answerField.text.length > 0)
{
[self number];
}
else
{
UIAlertView *error = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Please type in more than one chracter"
delegate:self
cancelButtonTitle:#"Okay!"
otherButtonTitles:nil];
[error show];
}
}
-(void)number
{
for (int i = 0; i < 100; i++)
{
if (answerField.text > strNumber)
{
hlLabel.text = #"Guess Lower";
answerField.text = #"";
}
if (answerField.text < strNumber)
{
hlLabel.text = #"Guess Higher";
answerField.text = #"";
}
}
}
The Problem: The for loop does not work. It only checks once. Also, everytime it checks, it always says "Guess Higher" when in fact, I guess higher than the computer generate number.
Other Information: I have a method where every time I click the keyboard done from the textfield, it will check the number method.
answerField.text is returning an NSString, not a number.
Try answerField.text.intValue.
And strNumber must be an integer for that to work, btw.
It took me some time to understand what you were trying to do. But I suspect that you are just trying to have a user enter some number in a field to guess a number with up to 100 attempts. Something like that.
But a for loop is not how you can make a user retry something. What you need is some state saved in the object (i.e. an ivar) which counts each attempt.
Second problem here is that it seems you are numerically comparing strings which does not make sense here. You want to compare numerical values.
So your program would likely look like:
-(IBAction)check:(id)sender
{
if (answerField.text.length > 0)
{
[self checkFieldValueAgainstCorrectAnswer]; // use better naming
}
else
{
UIAlertView *error = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Please type in more than one chracter"
delegate:self
cancelButtonTitle:#"Okay!"
otherButtonTitles:nil];
[error show];
}
}
-(void)checkFieldValueAgainstCorrectAnswer
{
NSUInteger fieldValue = [answerField.text unsignedIntegerValue];
NSUInteger correctValue = [strNumber unsignedIntegerValue];
if (fieldValue > correctValue) {
// guess lower
} else if(fieldValue < correctValue) {
// guess higher
} else {
// you won!
return;
}
answerField.text = #"";
attempts++;
if (attempts > 100) {
// Too many attemps, you lost!
}
}
I am creating an iPhone app which is taking many values from the user and assigning them to variables.
I want to display an alert message if more than two of the variables' values are equal to zero.
Basically, if the user has two empty fields, it should show an alert stating that there is insufficient data.
Any idea how to do this?
Your question is a bit vague but what about
Find errors
Display an alert
Somewhere alone the lines of this pseudo code:
int errorCount = 0;
if(var1 == 0) {
errorCount++;
}
if(var2 == 0) {
errorCount++;
}
// check all variables...
// Show alert if there are any errors
if(errorCount > 0) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Title"
message:[NSString stringWithFormat:#"You have %d errors", errorCount]
delegate:nil
cancelButtonTitle:#"GoFightWin"
otherButtonTitles: nil, nil];
[alert show];
[alert release];
}