Cocoa touch NSInteger plusing 4 not 1 - xcode

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.

Related

xcode integer shows strange values

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.

Pre-Select UIPickerView based on previous string in Xcode

I have the following code that populates a UIPickerView with the string value that was previously selected stored in the variable "preValue."
NSUInteger currentIndex = [arrayColour indexOfObject:preValue];
[ColourAndShadePicker selectRow:currentIndex inComponent:0 animated:YES];
The problem is that it doesn't seem to be matching the value exactly when there are spaces involved.
For instance if the 3 options are
Red
Green 1
Green 2
and the user selects Green 2, the Green 2 value is stored but on re-populate it's selecting Green 1 and not Green 2.
I think it has something to do with the spaces, and picking the first similar option?
Any ideas how to solve?
I'm unable to use the row number and need to match the string exactly.
EDIT:
To populate the array in the first place I'm using the following:
arrayColour = [[NSMutableArray alloc] init];
for (int i = 0; i < [substrings count]; i++)
{
//parse out each option (i)
NSString* companyoption = [substrings objectAtIndex:i];
//add as option to component
[arrayColour addObject:companyoption];
}
Thanks!

Is it possible to animate a NSTextField

I´m trying a pretty simple thing for testing and can´t get it to work, sorry.
What I want to achieve is that the changes in the textfield label are seen on the screen as well as the color changes.
I already tried several things without success and I´m pretty sure I haven´t got the clue yet.
Any ideas what I´m doing wrong?
I have this right now:
- (IBAction) writeTextToLabel:(id)sender
{
NSAnimation *animation;
[animation setDuration:10];
int n = 100;
NSString *string1 = #"";
[animation startAnimation];
for (int i = 1 ; n >= i ; i++) {
[label setTextColor:[self ccRandom:1.0 :0.0]];
string1 = [NSString stringWithFormat:#"Hello World %.3i\n",i];
[label setStringValue:string1];
}
[animation stopAnimation];
}
You can use AnimTextFieldUsingNSAnimationContext by ipmcc
This code makes fadeIn/fadeOut effect of changing text.
But you can add changing color code after [self setStringValue: aString]; to change your color.
Don't forget to include Quartz.framework to your project.

How do I make 2 labels add together with each other?

Hi I'm kinda new to Xcode and I'm trying to make an app where you press a button and the number will go up; and I have 2 buttons and 2 labels. I've got it to where the 2 labels will count up, but now I'm wanting the numbers from both labels to add together and show in a different label. Is there any line I can add to the buttons to make them just count up in the other label as well or do I need to have a separate action and/or button?
Thanks
Straight up:
int sum = [[label1 text] intValue] + [[label2 text] intValue];
label3.text = [NSString stringWithFormat:#"%#", sum];
Should work, just make sure to replace the pointers I used with the ones you're using.
esqew's answer would do the trick, but the format specifier is incorrect.
If the variable sum is in fact an int ...
label3.text = [NSString stringWithFormat:#"%#", sum];
should be:
label3.text = [NSString stringWithFormat:#"%d", sum];
%# is for Objective-C objects, an int is not an Objective-C object.
Reference:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

Easy way to set a single character of an NSString to uppercase

I would like to change the first character of an NSString to uppercase. Unfortunately, - (NSString *)capitalizedString converts the first letter of every word to uppercase. Is there an easy way to convert just a single character to uppercase?
I'm currently using:
NSRange firstCharRange = NSMakeRange(0,1);
NSString* firstCharacter = [dateString substringWithRange:firstCharRange];
NSString* uppercaseFirstChar = [firstCharacter originalString];
NSMutableString* capitalisedSentence = [originalString mutableCopy];
[capitalisedSentence replaceCharactersInRange:firstCharRange withString:uppercaseFirstChar];
Which seems a little convoluted but at least makes no assumptions about the encoding of the underlying unicode string.
Very similar approach to what you have but a little more condense:
NSString *capitalisedSentence =
[dateString stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[dateString substringToIndex:1] capitalizedString]];
Since NSString is immutable, what you have seems to be a good way to do what you want to do. The implementations of (NSString*)uppercaseString and similar methods probably look very much like what you've written, as they return a new NSString instead of modifying the one you sent the message to.
I had a similar requirement, but it was for characters within the string. This assuming i is your index to the character you want to uppercase this worked for me:
curword = [curword stringByReplacingCharactersInRange:NSMakeRange(i,1)
withString:[[curword substringWithRange:NSMakeRange(i, 1)] capitalizedString]];
If you profile these solutions they are much slower then doing this:
NSMutableString *capitolziedString = [NSMutableString stringWithString:originalString];
NSString *firstChar = [[capitolziedString substringWithRange:NSMakeRange(0,1)] uppercaseString];
[capitolziedString replaceCharactersInRange:NSMakeRange(0, 1) withString:firstChar];
in testing on an iphone 4 running iOS 5:
#doomspork's solution ran in 0.115750 ms
while above ran in 0.064250 ms;
in testing on an Simulator running iOS 5:
#doomspork's solution ran in 0.021232 ms
while above ran in 0.007495 ms;
Aiming for maximum readability, make a category on NSString and give it this function:
NSString *capitalizeFirstLetter(NSString *string) {
NSString *firstCapital = [string substringToIndex:1].capitalizedString;
return [string stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:firstCapital];
}
Then in your code where you want it:
NSString *capitalizedSentence = capitalizeFirstLetter(dateString);
This kind of code rarely belongs in the spot where you need it and should generally be factored away into a utility class or a category to improve legibility.

Resources