Obj-C Text fields, float values and SIGBART - objective-c

I am writing an iOS application for (among many features) calculating alcohol content from sugar before and after fermentation for homebrewers. Now the problem is; every time I run my app in the simulator, it crashes with "Thread 1: Signal SIGBART" once I enter the UIViewController with the text fields, labels and buttons used in this function (in the implementation):
- (IBAction)calcAlc:(id)sender {
double ogVal = [[oGtext text]floatValue];
double fgVal = [[fGtext text]floatValue];
double alcoContent = ((1.05*(ogVal-fgVal))/fgVal)/0.79;
NSString *theResult = [NSString stringWithFormat:#"%1.2f",alcoContent];
alcContent.text = theResult;
}
I'm really stuck here -- any help is very appreciated. Thanks in advance!

You should check if fgVal is ever 0, since you are dividing by fgVal.
You will also want to use doubleValue instead of floatValue, since you are declaring them as double.

I think you should try float in place of double.
Now put break points on each and every method and debug it to get the exact point where you application is getting crashed and then you will be able to find the solution.
Also be sure to connect each and every ib outlet and action in your storyboard or nib(whatever you are using).
Please notify if it works..:)

Related

How to call images to a different view in xcode using variables

I am currently working on a project in Xcode 4.4 with objective-c not in cocos2d where I have a view called GameView and a class called Characters. In the GameView have, well, the game, this is the scene in which you play the game. In the Characters, all the characters images and powers or anything unique to them are called. I want it so that I can choose a character in CharacterSelect page and then the character that I selected is the character that is in the GameView and that I am controlling. Right now I am just working with the images. In the GameView I am basically trying to set up a way to call an image of the corresponding character from the Characters class. If I could I would have an animation of the character but I'm just going to go step by step. All I want is the corresponding image for the character while he's facing right
Here is my code in my GameView.m for the function to basically create the image, and I know I need to call the image file from a variable in Characters. bluebox is like defined in the .h as a UIImageView and really it just contains all the things the character can do... such as the left and right buttons allows it to move and so on...
-(void)createCharacter
{
bluebox = [[UIImageView alloc] initWithFrame:
CGRectMake(70, 100, 150, 130)];
bluebox = [[UIImageView alloc] init];
}
In my Characters.m I made an NSString in my .h of Characters in it is called lookingRight1... And then for one of my characters I have this
-(void)megaMan
{
lookingRight1 = #"MegaManRight1.png";
}
Now I suppose I'd need to call looking right. I'm actually confusing my self trying to write this, so please help, if you don't get confused also. I don't have really much code for my CharacterSelect page but what I do have just links a particular button labeled with a character name to the GameView.
If you could please help I'd be extremely thankful. I think that it would help if you could show a mock second character also so that I can see what to do to get the character I want...
Sorry If this is confusing to you.

Objective-C iOS, set a delay for an update of text

I'm trying to find a solution to a problem I just stumbled upon.
Tried to search for it but it's not that I'm looking for.
I'm doing a GPS-app with two tabbars. I track the distance in the map-view (using CLLocation) and when I change tab to a different view the stringtext that say what distance it is from when I started don't update immediately, it take a couple of seconds.
And when I press the stop button I want it to either wait those couple of seconds so the real distance updates. But I dont want to freeze the app.
(*NSDate future = [NSDate dateWithTimeIntervalSinceNow: 3.0 ];
[NSThread sleepUntilDate:future];)
I'm saving the string with the distance in a cell in the second tab, so if I multitask while the app is going I want to just start the app and press stop. And then the new distance will be correct and saved. Hope I didn't confuse you with this much text I hope you understand what I asking for!
thx
[label performSelector:#selector(setText:) withObject:newText afterDelay:3.0];
So what you want to do is use dispatch_after:
dispatch_after(3 seconds, dispatch_get_main_queue(), ^
{
myLabel.text = <the value you want to appear>;
} );

arc4random using input from textField

I am trying to use arc4random but it os causing my app to crash. On my view I have a text field and the user will enter a number, when the hit a button this number is used for the range, the code I use is as follows:
int myInt1 = [textfield.text intValue];
int fromNumber = 1;
int rnumber = (arc4random() % (myInt1 - fromNumber)) + fromNumber;
number1.text = [[NSString alloc] initWithFormat:#"%i",rnumber];
It will work if I use 50 for example instead of myInt1 but I need the user input. Any help is greatly appreciated.
I have noticed that it will work when there is a figure in the textfield, when this is left blank and the button is selected then the app crashed. In the console "Terminating in response to SpringBoard's termination" is displayed.
If the variable 50 is working then there is obviously something wrong with your text field's text. Most likely your text field is nil. Try checking back to your code and see if anything is influencing your app to become nil.
I will tell you this in case something similar happens next time. You should always try to debug your code, for example, instead of straight posting this to stack overflow with minimal code, try figuring out the problem yourself!
For example, a method identify what is wrong with your problem that I suggest you try now and feedback the results to me is checking if textfield is non-zero like below:
if (textfield) {
NSLog (#"Text field is not nil! GOOD!");
}
If the log is called in your console, you know your problem is that textfield is nil.

Strange BAD_ACCESS when printing NSInteger

this is my first time asking something here, so don't be too harsh on me please :-). I have a strange "bad access" issue. In a class of mine I have a NSInteger along with a few other members. I override the - (NSString *)description method to print the values of everything like so (omitted the unrelevant part):
- (NSString *)description {
return [NSString stringWithFormat:#"Duration:%d", duration];
}
and then I print that using NSLog(#"%#", myObject) which is giving me EXC_BAD_ACCESS without any log messages, regardless of the NSZombieEnabled.
I have double checked the order of all formatting specifiers and
parameters - it's correct.
I tried changing the format specifier to %i and %# and didn't get any result
When I init my object I don't initialize
duration. Later, I assign it through a property #property NSInteger
duration. So I tried initializing the duration to 0 in my init
method but to no avail.
If I box duration to a NSNumber prior to
printing, it works.
When I remove the duration and leave all the
other ivars it works again.
I'm stumped, what am I doing wrong here?
Can you help me?
Thanks a lot!
EDIT: To summarize, It seems this is caused by differences between 32 and 64 bit platforms, because it's fine when run on an iphone 4 and has issues only when run in the simulator. I tried 3 different approaches - using %d and %i with the NSInteger variable itself, using %qi and using %ld/ %lx and I also tried to cast to int and long with the various format specifiers. Every time I can run on the device, but get EXC_BAD_ACCESS in the simulator.
The only guess here: NSInteger could be 64 bit in your case and %i (as well as %d) expects 32-bit integer, you can try %qi or (what seems to be better) cast the value explicitly.
When you run the app in the debugger, where exactly is the signal raised? Xcode will show you the precise line and stack of the problem.
When you are sure the crash happens in the stringWithFormat: method it's probably a matter of format specifiers. Apple's String Programming Guide contains information on how to handle NSInteger in a safe and platform independent way.
Maybe you need to synthesyse your property?

Converting float value to NSString

i'm a noob in Objective C. I'm having the following confusion.
How do i convert a float value obtained from progressView.progress in a string?
I tried this
NSString *progressStatus = [NSString stringWithFormat:#"%f", progressView.progress];
label.text = progressStatus;
But this say EX_BAD_ACCESS
some help please.
Thank You.
The snippet you posted reads correctly. The most likely source of your error is that the UIProgressView has yet to be initialized when this code is called. It is also possible that the UILabel is not correctly set up.
If you created these UI elements in the IB, check the connections. If you created them programmatically, then check that they are allocated and initialized correctly.
Also ensure that you are setting the text of the label after the progressView is created.