Simple iPhone tally method question - iphone

Just trying to create a simple method that counts up 1 on the tally when the button is pressed. My knowledge is extremely limited and I am pretty sure that my problem is somewhere in the method implementation:
-(IBAction) updateTally:(id) sender {
NSString *text;
int total = 0;
total = total + 1;
text=[[NSString alloc] initWithFormat: #"%i", total];
lblTally.text = text;
}
I have done the necessary interface declarations for the lblTally UILabel and the updateTally method. I suspect that there is some sort of an NSString/int/%i/%# mixup that I am making but I'm not sure how to fix it. When I run the program as it currently is it displays a 0 in the lblTally label field on the iphone. When I press the button it then displays a 1 in that field. However, if I continue to press the button - nothing happens, it just remains a 1. How do I make it count up as I continually press the button over and over?

The problem is that you are reinitializing the total in each updateTally. You need to either store the tally in a member variable or extract the existing tally from the string, update it, then write it back to the string.
- (IBAction) updateTally:(id) sender
{
int oldtally = [lblTally.text integerValue];
int newtally = oldtally + 1;
lblTally.text = [NSString stringWithFormat:"%d",newtally];
}
I should also point out that your current code has a memory leak (you invoke alloc/init, but then you don't invoke release after you have assigned the result to the lblTally.text variable). You should either invoke [text release] after lblTally.text = text, or you should use stringWithFormat like I have used above, which uses autorelease so that the string does not need to be explicitly released (since it will be automatically released).

You are re-initialising the total variable each time the method runs.
int total = 0;
Move than line of code outside of the method. I dont do apple development but it'll be something like:
int total = 0;
-(IBAction) updateTally:(id) sender {
NSString *text;
total = total + 1;
text=[[NSString alloc] initWithFormat: #"%i", total];
lblTally.text = text;
}

Related

NSNumber stores zero value instead of the correct numeric value

I developing a simple calculator iPhone application. Just for practicing purpose. I have an IBAction method that stores the digits that the user entered. The whole concept is that the calculator app piles up pending oprations so the user can perform multiple actions and the screen shows the result the following way: 1 + 2 - 4 + 10 = X. So i have two NSMutableArray that stores NSNumber numbers and the operator actions. When the user clicks an operator button, a new array element created for the new number. When the users entering the digits, the last array element is updating itself until an operator button is pressed.
The problem is that every array element is zero. Inside the method it stores the corrent value when i set it, but when the method is executed and called again it contins nothing but zeros instead of the entered numbers. The NSNumber objects are present, the array contains every number, but every number is 0.
Here is the method:
// Processing digit buttons
- (IBAction)clickDigit: (UIButton *) sender {
double currentNumber = [[numbers lastObject] doubleValue];
// Get the digit
double digit = sender.tag;
// Do nothing when the currentNumber and the digit both zero
if(currentNumber == 0 && digit == 0) {
return;
}
// When currentNumber is zero, override the value
if(currentNumber == 0) {
currentNumber = digit;
[numbers removeLastObject];
[numbers addObject: [NSNumber numberWithDouble: currentNumber ]];
// Else, add the digit to currentNumber
} else {
currentNumber = currentNumber * 10 + digit;
[numbers removeLastObject];
[numbers addObject: [NSNumber numberWithDouble: currentNumber ]];
}
// Update the screen
[self updateDisplay];
}
I have no clue what's wrong. Any suggestions?
Thanks in advance
UPDATE: it turns out that the clickClear method is automatically called after each button press. It sets the value zero. I linked the full source code below this post in the comment section. The only question is: why this method called? What calls this method?
UPDATE2: with The Saad's help i managed to solve this problem. Thanks to everyone! :)
The only possible way you get this problem is if sender.tag == 0 all the time. So you should definitely check that. My point here is that there is not other possible scenario that produces those symptoms, so it has to be it.
ok got it,
one thing here is that first check your sender's tag, either by braekpoint or by NSLog in click Digit method, i guess tags never gets up than zero, and hence the last object in array will always be zero, further, place nslog or breakpoint on all places where you are adding object to array.
in interface.
#property(nonatomic, retain) NSMutableArray* numberArray;
in implementation,
#synthesize numberArray = _numberArray;
in dealloc
[_numberArray release];
ok now next thing is to make an array in init which is
NSMutableArray* arr = [NSMutableArray alloc] init];
self.numberArray = arr;
[arr release];
[self.numberArray addObject:[NSNumber numberWithDouble:0]];

Couple Obj C issues including an error

So basically in this code I placed below there is a couple things I would like to accomplish. 1) In that first one I would like the Text Field to close after done is pressed, but it is not doing so. (Found that code on other forums). 2)On that first button when it is pressed down I would like it to send a time to be saved for when the button is released. 3) On release of that button I would like it to calculate the time between the two times along with some other calculations later.
Problem that I am mainly getting here is the error from the NSTimeInterval. It keeps telling me that NSTimeInterval is incompatiable with type NSTimeInterval. So a double is incompatible with a double??? (I have also tried it with NSTimeInterval *timePassed in the .h and just trying to set timePassed = to it and it doesn't work either. Similar error happens.
#import "MphViewController.h"
#implementation MphViewController
#synthesize speed, distance;
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(IBAction)triggerDown:(id)sender{
timeStart = [NSDate date];
}
-(IBAction)triggerUp:(id)sender{
NSInteger *dist;
NSString *display;
NSTimeInterval *timePassed = [timeStart timeIntervalSinceNow];
if ([distance.text length]== 0) {
display = #"Please enter a distance";
}
else{
dist = atoi(distance.text);
display = [[NSString alloc] initWithFormat:#"%d MPH",dist];
}
speed.text = display;
[display release];
}
Also if you have a chance to glance at the else statement. I'm not sure if that will work (seeing is after I enter a distance I still cant get rid of the numberpad). Id like to pass mph once it is calculated into that to be displayed on the screen. Very confused with how parsing is done on obj c. Anyways thanks for any and all help.
As for the textField part. My textField that it is assigned to is distance. Should that Bool be written differently to have distance in it or is that just the way it is supposed to be written?
You are denoting primitive types as pointers where they do not need to be. Remove the star (*) from the NSInteger and NSTimerIntervaldeclaration. The if check looks fine but instead of atoi just use:
dist = [distance.text integerValue];
Edit:
Notice a couple of memory management issues.
1 . Make sure you properly retain/release the date then later release it in dealloc.
-(IBAction)triggerDown:(id)sender{
[timeStart release];
timeStart = [[NSDate alloc] init];
}
2 . Your if statement will result in trying to release an NSString literal which likely will not crash but is incorrect memory management. Just use an autoreleased string and remove the [display release];
...
else{
dist = [distance.text integerValue];
display = [NSString stringWithFormat:#"%d MPH",dist];
}
speed.text = display;
}

Pass variable by reference to method (Objective-C Iphone SDK)

Hi :) This is messing me up quite the bit...
Say I have this method:
-(void) initEvent:(NSMutableArray*) Day day:(float)DayNum
{
//[Day addObject:[[[LifeEvent alloc] init] initVars:100]];
[Day addObject:#"Hello"];
[Day addObject:#"there :)"];
}
I call that method in a small loop:
for (int i = 0; i < 7; i++)
{
NSMutableArray * DayTMP = [[NSMutableArray alloc] init];
[self initEvent:DayTMP day:i];
}
Theoretically, this should pass DayTMP to the function, and add data to that variable. Not the case, instead, it creates a copy of DayTMP, adds data to that value, and drops it. DayTMP never gets modified!
I need to know how to pass values to functions as pointers, not copies, so that DayTMP is modified.
Actually what you are doing here is that you are creating 7 NSMutableArray type objects and the same variable name DayTMP is used for all of them ....... so loose the access of all 6 of them and and you only can access the last one because in every itteration of the loop DayTMP is pointing to new location ....... so to achieve what you want you should do following...
NSMutableArray * DayTMP = [[NSMutableArray alloc] init];
for (int i = 0; i < 7; i++)
{
[self initEvent:DayTMP day:i];
}
Normally your code should work fine except that you never release DayTMP creating a memory leak. You are indeed passing a pointer to an NSMutableArray : (NSMutableArray*).
You "init" DayTMP inside the loop!!!
that means you create (and never release!) the same object many time, overriding it every time, so you kill the old one each time, and you get just the last one: it's a memory error

EXC_BAD_ACCESS in iPhone app - memory management issue

For reference, I've already read:
memory management question -- releasing an object which has to be returned
iPhone: Return NSMutableArray in method while still releasing
Releasing objects returned by method
Which I thought would help :).
This app is a teaching tool and is intended to help people visualize simple genetics. Just some background so the variable names and stuff will make sense. Here's the main code that executes when the app runs:
- (void)viewDidAppear:(BOOL)animated {
ThingzCore *core = [[ThingzCore alloc] init];
ThingzThing *thing1 = [[ThingzThing alloc] init];
thing1.breed = #"AB";
thing1.pattern = #"AC";
thing1.color = #"CA";
thing1.gender = #"XY";
thing1.generation = 1;
thing1.isEgg = NO;
ThingzThing *thing2 = [[ThingzThing alloc] init];
thing2.breed = #"CD";
thing2.pattern = #"BA";
thing2.color = #"CB";
thing2.gender = #"XX";
thing2.generation = 1;
thing2.isEgg = NO;
NSLog(#"Breeding GD BR PT CL G");
ThingzThing *child = [core mateFather:thing1 withMother:thing2];
NSLog(#"Round 1: %# %# %# %# %d",child.gender,child.breed,child.pattern,child.color,child.generation);
sleep(10);
child = [core mateFather:thing1 withMother:thing2];
NSLog(#"Round 2: %# %# %# %# %d",child.gender,child.breed,child.pattern,child.color,child.generation);
sleep(10);
child = [core mateFather:thing1 withMother:thing2];
NSLog(#"Round 3: %# %# %# %# %d",child.gender,child.breed,child.pattern,child.color,child.generation);
sleep(10);
child = [core mateFather:thing1 withMother:thing2];
NSLog(#"Round 4: %# %# %# %# %d",child.gender,child.breed,child.pattern,child.color,child.generation);
sleep(10);
[thing1 release];
[thing2 release];
[core release];
}
And here's what happens when I run it in various ways:
Running without breakpoints, it crashes, with no console message, after the 2nd sleep() but before the "Round 3" NSLog.
Running with breakpoints enabled, but none defined, it runs through the entire sequence. After the fourth sleep(), it crashes with EXC_BAD_ACCESS.
Running with breakpoints enabled and NSZombiesEnabled, it does the same thing as above - no further information, just EXC_BAD_ACCESS.
Running in Instruments, no leaks are shown.
This is the routine being called four times:
-(ThingzThing *)mateFather:(ThingzThing *)father
withMother:(ThingzThing *)mother {
// will we be doing a mutation?
int mutationPercentage = father.generation + mother.generation;
int mutationNumber = (arc4random() % ((unsigned)100 + 1));
BOOL isMutation = NO;
if (mutationNumber <= mutationPercentage) {
isMutation = YES;
}
// get possibilities
NSArray *possibilities = [self possibilitiesByMatingFather:father
withMother:mother
mutations:isMutation];
// randomly select one of the possibilities
int keeping = (arc4random() % ((unsigned)[possibilities count]));
return [possibilities objectAtIndex:keeping];
}
Without pasting in the ENTIRE code, the possibilitiesByMatingFather:withMother:mutations function is returning an NSMutableArray. That routine declares the array by using:
NSMutableArray *possibilities = [NSMutableArray array];
It then:
return possibilities;
It does not send a release or autorelease message to possibilities; my understanding is that creating the array the way I have is an implicit autorelease. I didn't want to alloc the array, because I'm returning it, so wouldn't have the opportunity to explicitly release it.
The objects held in the possibilities NSMutableArray are of a custom class. They are added as follows:
ThingzThing *newThing = [[ThingzThing alloc] init];
newThing.breed = choiceBreed;
newThing.gender = choiceGender;
newThing.color = choiceColor;
newThing.pattern = choicePattern;
newThing.generation = mother.generation + father.generation;
newThing.name = #"";
newThing.isEgg = YES;
[possibilities addObject:newThing];
[newThing release];
Which seems to work most of the time. At least, when breakpoints are enabled, the program runs through the code without complaint until the end, as noted above.
Any suggestions on what I'm doing wrong, here? It's obviously memory management issues of some kind, but I can't sort it in my head.
BTW, in a vain, throwing-things-at-the-wall attempt to figure it out, I did modify the one line from the main routine as follows:
// get possibilities
NSArray *possibilities = [[self possibilitiesByMatingFather:father
withMother:mother
mutations:isMutation] retain];
To no avail. Same results. So the problem isn't in retaining the array returned by possibilitiesByMatingFather:withMother:mutations. Forcing a retain on that return isn't helping.
Frequently in this type of situation, the actual error is not at the location shown in the debugger when the app halts.
For example, the debugger may be pointing to a method call, but the problem actually occurs inside the method -- not when the method is called.
To track things down, I would suggest setting a breakpoint just before the method call that triggers the error -- in your case, this would be the mateFather: withMother: call. Then step into that method. There is a good chance you will find that the problem happens inside that method -- or even inside a method called from within that method.
Check you have the correct property declarations of the string properties of class ThingzThing.
e.g.
#property (nonatomic, retain) NSString* breed;
NEED to be retain or copy NOT assign...
Found it. Buried eight method calls down, I was sending release to an NSArray that was obviously autoreleasing. When the initial calling routine (viewDidAppear) fell down into autorelease mode, it tried to autorelease the already-released object, and exploded. Good grief - is there any way XCode could have helped me track that down?
In any event, in case anyone runs across this, make bloody sure you're not sending a release message to something that you didn't explicitly alloc yourself. If you didn't alloc it, odds are it was autoreleasing itself, and you sending a release to it takes the retain count negative, and the Foundation framework vomits with an EXC_BAD_ACCESS.

Why is my button clicked event being called 4 times?

Probably a noob question, but I'm trying to write a simple iPhone app that increments a label by the number of times a button is clicked. I have the following code:
#import "Controller.h"
int *i = 0;
#implementation Controller
- (IBAction)buttonClicked:(id)sender {
NSString *numTimesClicked = [NSString stringWithFormat:#"%d",i++ ];
myLabel.text = numTimesClicked;
}
#end
When I click the button, the label updates in multiples of 4 (4,8,12,16, etc). What might I be doing wrong here?
Look at the definition of i:
int *i = 0;
i isn't an integer — it's a pointer to an integer. The size of an int is 4 bytes on your architecture, so the pointer increments by 4 (which would be the address of the next int in an array of ints). You want to declare it as just int i = 0.