i created a Single View Application in XCode..
My Integer shows up strange values..
I declared the integer score with the value of 0 in the viewDidLoad method and
if two UIImageViews gets hitting each other the value of score gets every time 10 points bigger..
Game.h
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
int score;
#interface Game : UIViewController{
Game.m:
//score
score = 0;
Score.text = [NSString stringWithFormat:#"Score: %i", score];
Collision:
if(CGRectIntersectsRect(BulletFive.frame, Asteroid.frame)){
BulletFive.hidden = NO;
[self Score];
[self PlaceAsteroid];
Score:
-(void)Score{
Score.text = [NSString stringWithFormat:#"Score: %i", score];
score = score + 10;
if (score > HighScore) {
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:#"HighScoreSaved"];
}
}
My problem:
If I click on a store button in the main menu and go to the game again, it shows up very strange values like 16060 or 26110 in a UILabel. The storebutton has nothing to do with the other view controller. What could be wrong or what is wrong with XCode ?
Sorry for my english, Google Translator isn´t very helpful..
when you declare the int or NSInteger make sure, it's not a pointer.
Wrong :
int *variable1;
NSInteger *variable1;
Right one:
int variable1;
NSInteger variable1;
Please add more description, so I can help you further.
Related
Hi I am trying to add (math not put letters together) a textfield's data, and a label's data together. I have preformed the same code you see below for multiplication but when I do the same (A little changed) it does not seem to work. The code I placed below just shows the value of the billAmountTextField. Do you know why? Thanks for your help!
-(IBAction)totalAllUp:(id)sender
{
NSString *billTotalAmountString = [billAmountTextField text];
float billTotalFloat =[billTotalAmountString floatValue];
NSString *tipTotalAmountString = [resultLabel text];
float tipTotalAmountFloat = [tipTotalAmountString floatValue];
float totalAmountAfterAdding = billTotalFloat + tipTotalAmountFloat;
NSString *finalAnswer = [NSString stringWithFormat:#"%0.2f", totalAmountAfterAdding];
[totalAfterAdd setText:finalAnswer];
}
I am making tip calculator and this is the code I currently have: (Below). [To make it look nice], is there a way for the user to just put in (15), and have code make that say .15 in the background? Thanks
- (IBAction)calcTapped:(id)sender {
NSString *billAmountString = [billAmountTextField text];
float billAmountFloat = [billAmountString floatValue];
NSString *tipPercentString = [tipPercentTextField text];
float tipPercentFloat = [tipPercentString floatValue];
float tipAmount = billAmountFloat * tipPercentFloat;
NSString *result = [NSString stringWithFormat:#"Tip: %0.2f",
tipAmount];
[resultLabel setText:result];
}
Try retrieving the string from the textfield, converting it into a NSNumber (or float) and then dividing by 100. You know, since 15/100 = 0.15.
I have this cocoa touch code where you press a button and a NSInteger adds its self with the number 1 and then a label turnes into the NSInteger value
This is the code
- (IBAction)out:(id)sender {
outnum = outnum + 1;
self.outnumberlabel.text = [[NSString alloc] initWithFormat: #"%d", outnum];
Everything works, but the NsInteger is adding 4 when it is ment to add 1
And when I put in
outnum = outnum + 2;
The label turns to 8
It is going up in fours does anybody know why and how to fix it
Check your outnum declaration - you'll see this behaviour if you declare
NSInteger *outnum;
rather than
NSInteger outnum;
It sounds like your out: method is being called four times more often than you expect. Try putting an NSLog line in this code to confirm. If that's the case, you'll need to figure out why the method is being called so much.
I ran 'analyze" in xcode on a current iOS project to try to track down a freeze issue and there are a number of memory alerts that I don't understand (screenshot of one below).
What is going on there: I have a custom ObjC class extending NSObject; in the init method I alloc/init an NSMutableArray and then in a loop, populate it with NSMutableArrays. This nested array is declared as a property and released in dealloc(). It lives for the life of the app.
Am I doing this wrong? I don't understand the alert#3: # object not referenced in this execution path and has a retain count of +1.
Since my class allocs the outer array, it owns it and will clean it up. Do the inner arrays need to be released?
Thanks for any tips - still new at this.
EDIT/ADDITION
Trying to stamp out the additional memory warnings I am getting so I thought I would add to the question here in the event someone stumbles upon this w/ the same issue.
I am getting the following alert with the code below (the 2nd line "[keyArray addObject: etc"). What is going on: I have a custom class (Key - based on NSObject) that I instance and store in an array. Based on answers to my previous question, I guess my alloc increases the retain count and then when it is added to the array, the retain count isn't decremented - so the memory warning occurs.
What is the proper way to handle something like this? Use a placeholder like this:
Key * k = [[Key alloc] initKeyWithPath:path isBlackKey:NO]];
[keyArray addObject: k];
[k release];
Is that the proper way to do it? Or is there I way to write the custom class to return an autoreleased obj? (thanks and sorry to be so long winded!).
Potential leak of an object allocated on line 460
Method returns an Objective-C object with a +1 retain count (owning reference)
Object allocated on line 460 is not referenced later in this execution path and has a retain count of +1 (object leaked)
-(void) addOctaveToArraysWithTransform:(CGAffineTransform*)trans andPath: (CGMutablePathRef) path
{
path = [self createCF_keyUsingTransform: trans];
[keyArray addObject:[[Key alloc] initKeyWithPath:path isBlackKey:NO]];
}
Key.h
#import <Foundation/Foundation.h>
#import "Key.h"
#interface Key : NSObject {
#public
CGMutablePathRef keyPath;
BOOL isBlackKey;
NSValue * path;
int keyState;
BOOL needsRedraw;
}
#property (nonatomic, assign) int keyState;
#property (nonatomic, assign) BOOL needsRedraw;
#property (nonatomic) CGMutablePathRef keyPath;
-(id) initKeyWithPath:(CGMutablePathRef) aPath isBlackKey:(BOOL)flag;
-(CGMutablePathRef) getKeyPath;
#end
Key.m
#import "Key.h"
#implementation Key
#synthesize keyState, needsRedraw, keyPath;
-(id) initKeyWithPath:(CGMutablePathRef) aPath isBlackKey:(BOOL)flag
{
if ((self = [super init])){
isBlackKey = flag;
keyState = 0;
needsRedraw = NO;
keyPath = aPath;
CGPathRetain(keyPath);
}
return self;
}
-(CGMutablePathRef) getKeyPath
{
return keyPath;
}
#end
Yes, you have to release the inner arrays to balance the alloc/init. Remember the outer array will retain each inner array, and the outer array will presumably release those later. But here you are still responsible for the alloc/init you just did.
Hope that helps.
You have an allocation of an NSMutableArray on each iteration of the for-loop. Instead use: NSMutableArray array] which is a convenience method that return an autoreleased NSMUtableArray suitable for adding to fieldNotes which will retain the NSMutableArray.
I have a problem with NSNumber: I don't understand how to increment and decrement it! I've tried int with [NSNumber intValue] and so on, but it didn't work!!!! I just want a page counter which can be converted to an NSNumber at the end.
My second problem is displaying a (partially) transparent image in an UIImageView. It has ever a (white) background.
Thanks for answering,
Le Milonkh
HI there
don't use nsnumbers, use ints. Much easier... (NSNumber is merely a wrapper for storing numbers, whereas int provides the default set of C based mathematical interfaces you are looking for, with relative ease - I'm sure you can do math with NSNumber -> although many people say NSDecimal is better. I say use floats, ints and doubles).
int pagenumber;
for(pagenumber = 0; pagenumber < 5; pagenumber++){
NSLog(#"%i", pagenumber);
}
then if you really need it in an NSNumber then do:
NSNumber *pagenumberns = [NSNumber numberWithInt:pagenumber];
In answer to your second question I have never had that problem, but try doing: [ImageView setOpaque:no] and [ImageView setBackgroundColor:[UIColor clearColor]];
Hope some of that helps.
Thank you for your answer, but I need to declare them globally in my ...ViewController.h & .m files because all my methods use them.
Is is possible to do it like that, or have I to #property and #synthesize the int?
FooViewController.h:
#interface FooViewController : UIViewController {
int *currentPage;
}
-(void)next;
-(void)changePage:(int)page;
FooViewController.m:
#implementation Post_GeheimnisViewController
#synthesize currentPage;
-(void)changePage:(int)page {
//do some stuff here
}
-(void)next {
currentPage++;
[self changePage:currentPage];
}
#end
Thank you for answering!
Le Milonkh