UISharedApplication bug that cannot be determined - iphone

-(void) setupMyLocation {
NSArray *viewControllerArray = [navigationUpdateFromDetail.navigationController viewControllers];
NSUInteger parentViewControllerIndex = [viewControllerArray count] - 2;
NSLog(#"Parent view controller: %#", [viewControllerArray objectAtIndex:parentViewControllerIndex]);
switch(parentViewControllerIndex){
case 0:
self.myLocation = navigationUpdateFromDetail.currentLocation;
break;
case 1:
YANAVAppDelegate *currentObject = (YANAVAppDelegate*)[[UIApplication sharedApplication]delegate];
// self.myLocation = currentObject.givenLocation;
break;
default: break;
}
}
I cannot seem to figure out why i keep getting an error "expecting expression before YANAVAppdelegate".
I cannot seem to put my finger on why this will not compile. any input is appreciated.. thanks in advance.

Don't define the variable (currentObject) inside the switch statement.

If you need to define a variable inside a case statement, you can do it using curly brackets (this technique also works in C++, by the way):
-(void) setupMyLocation {
NSArray *viewControllerArray = [navigationUpdateFromDetail.navigationController viewControllers];
NSUInteger parentViewControllerIndex = [viewControllerArray count] - 2;
NSLog(#"Parent view controller: %#", [viewControllerArray objectAtIndex:parentViewControllerIndex]);
switch(parentViewControllerIndex) {
case 0:
self.myLocation = navigationUpdateFromDetail.currentLocation;
break;
case 1:
{
YANAVAppDelegate *currentObject = (YANAVAppDelegate*)[[UIApplication sharedApplication]delegate];
// self.myLocation = currentObject.givenLocation;
break;
}
default:
break;
}
}
A C++ guru explained me once that this gives your variables a context stack for them to exist, which the switch / case statement does not provide automatically. Remember to delete / release objects if you create any in that context, otherwise it's an easy way to have a memory leak.
I personally always use curly brackets in my case statements, if you ask me ;) you never know if in the future you'll need them, it makes things easier to understand.

Related

Sprite Kit Calling a random method

I am currently making a game in sprite kit and i have 8 methods, I have written all the timing code E.T.C so it calls a method every 1 second, but i want it to call a random one of the eight methods, I have been trying to get this working for weeks, any help would be muchly appreciated, Here is my timing code -
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {
self.lastSpawnTimeInterval += timeSinceLast;
if (self.lastSpawnTimeInterval > 5) {
self.lastSpawnTimeInterval = 0;
[self shoot1];
}
}
- (void)update:(NSTimeInterval)currentTime {
// Handle time delta.
// If we drop below 60fps, we still want everything to move the same distance.
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1) { // more than a second since last update
timeSinceLast = 1.0 / 60.0;
self.lastUpdateTimeInterval = currentTime;
}
[self updateWithTimeSinceLastUpdate:timeSinceLast];
}
As you can see instead of [self shoot1]i want it to randomly call one of the eight methods, Also all the methods are named Shoot1, Shoot2, all the way to Shoot8. Thankyou
I can think of two options...
Option 1
Just pick a random number between 1 and 8, and use a switch statement:
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {
self.lastSpawnTimeInterval += timeSinceLast;
if (self.lastSpawnTimeInterval > 5) {
self.lastSpawnTimeInterval = 0;
int randomNumber = arc4random_uniform(8);
switch(randomNumber) {
case 0:
[self shoot1];
break;
case 1:
[self shoot2];
break;
// ... cases 2-6
case 7:
[self shoot8];
break;
}
}
}
Option 2
Rewrite your shootN methods so that you only have one method that takes an integer as a parameter:
- (void)shoot:(int)index;
Then, you can just do the following:
[self shoot:arc4random_uniform(8)];
You could also live dangerously...
int random = arc4random() % 8;
NSString *selectorName = [NSString stringWithFormat:#"shoot%i", random];
SEL selector = NSSelectorFromString(selectorName);
if ([self respondsToSelector:selector]) {
[self performSelector:selector];
}
This will, by the way, generate a warning when using ARC.

value get released from variable when value is float number

I am facing such strange issue right now, I am trying to implement calculator in my project the demo. The place I want to implement is ARC enable and my project ARC disable everything is working perfectly in that demo its working perfect in my project tOO but when i try to do operation on float values then my application crashes says EXC_BAD_ACCESS(code 1.... below is my code
_currentValue and _previousValue are like this in .h file
#property (retain,nonatomic) NSNumber* currentValue;
#property (retain,nonatomic) NSNumber* previousValue;
in my .m file there are 2 methods where i face problem
- (NSString *)calculateValueToString:(NSString *)valueString ForType:(enum State)type{
_currentValue = [numberFormatterFormal numberFromString:valueString];
NSLog(#"%#",_currentValue); //whatever number i input it get prints here
[self calculateValue]; // this method get called
state = type;
if (state != Equal){
_previousValue = _currentValue;
NSLog(#"%#",_previousValue); // get print
_currentValue = #0 ;
}
NSLog(#"_previousValue%#",_previousValue); // get print
NSLog(#"_currentValue%#",_currentValue); // get print
return [numberFormatterFormal stringFromNumber:_currentValue];
}
- (void)calculateValue{
switch (state) {
case Plus:
_currentValue = [NSNumber numberWithDouble:[_previousValue doubleValue] + [_currentValue doubleValue]];
break;
case Minus: //GET ONLY EXECUTE ONLY IF OPERATION IS -
NSLog(#"%#",_currentValue); // it has value
--->>>>>>> HERE APP CRASH NSLog(#"%#",_previousValue); // app crashes here
_currentValue = [NSNumber numberWithDouble:[_previousValue doubleValue] - [_currentValue doubleValue]];
NSLog(#"%#",_currentValue);
// THIS ALL WORK PERFECTLY IN THAT DEMO WHICH IS ARC ENABLE
break;
case Multiple:
_currentValue = [NSNumber numberWithDouble:[_previousValue doubleValue] * [_currentValue doubleValue]];
break;
case Divide:
_currentValue = [NSNumber numberWithDouble:[_previousValue doubleValue] / [_currentValue doubleValue]];
break;
default:
break;
}
}
You're accessing your instance variables directly, and using manual reference counting. This isn't going to work.
All of the memory management is done in the synthesised accessor methods, so you must use the property accessors, e.g:
self.currentValue = ....;
Never
_currentValue = ...;
Or you will be bypassing all of the retain / release calls that the accessors are doing for you.

How to implement Kobold2D KKInput gestureSwipeDirection to if else statements?

I'm finding it hard to learn from documentation on how I can use the Kobold2D KKInput gestureSwipeDirection to detect swipes left/right/up/down and have them carry out if else statements. Can anyone help by providing me with a sample code. Thanks
KKInput* input = [KKInput sharedInput];
KKSwipeGestureDirection dir = input.gestureSwipeDirection;
switch (dir)
{
case KKSwipeGestureDirectionRight:
// direction-specific code here
break;
case KKSwipeGestureDirectionLeft:
// direction-specific code here
break;
case KKSwipeGestureDirectionUp:
// direction-specific code here
break;
case KKSwipeGestureDirectionDown:
// direction-specific code here
break;
}
I think you're making a mistake, you're placing the code in a single method, but you should use two hands, one to determine the KKInput, and one to check the status, plus you forgot gestureSwipeEnabled
try to do this:
-(id) init {
if ((self=[super init])) {
input = [KKInput sharedInput];
input.gestureSwipeEnabled = YES;
[self schedule:#selector(theTime:)];
}
return self;
}
-(void) theTime:(ccTime)time {
if (input.gestureSwipeRecognizedThisFrame) {
KKSwipeGestureDirection dir = input.gestureSwipeDirection;
switch (dir)
{
case KKSwipeGestureDirectionRight:
// direction-specific code here
break;
case KKSwipeGestureDirectionLeft:
// direction-specific code here
break;
case KKSwipeGestureDirectionUp:
break;
case KKSwipeGestureDirectionDown:
// direction-specific code here
break;
default:
break;
}
}
}

Whats wrong with this line?

My app crashes when I press the "own" button. I don't know what's wrong with my code. I'm new to this, so if I totally screwed up, don't be to hard please :). EDIT: This is what Xcode says:
#1 0x000144fd in -[UIApplication sendAction:to:from:forEvent:] ()
The app crashes on the line:
if (thing.hidden == NO) {
This is the code that belongs to "own":
- (IBAction)own {
if (thing.hidden == NO) {
int rNumber = rand() % 4;
switch (rNumber) {
case 0:
result.text = #"A";
break;
case 1:
result.text = #"B";
break;
case 2:
result.text = #"C";
break;
case 3:
result.text = #"D";
break;
default:
break;
}
}
if (thing.hidden == YES) {
int rNumber = rand() % 3;
There may be several rason
IBAction is actually just a hint that tells interface builder where to find the methods in your objects so you can link controls to methods.
In iOS, actions can take zero, one or 2 parameters.
If one parameter, that parameter is the object sending the message:
-(IBAction) someAction: (id) sender;
If two parameters, it takes the form:
- (IBAction) someAction:(id) sender forEvent: (UIEvent*) event;
you can use -(void)own and connect to your button in nib file
Nothing seems wrong in your code, assuming that the objects thing and result are retained properly.
- (IBAction)own {
if (thing.hidden == NO) {
int rNumber = rand() % 4;
NSString *myText = #""; //
switch (rNumber) {
case 0:
myText = #"A";
break;
case 1:
myText = #"B";
break;
case 2:
myText = #"C";
break;
case 3:
myText = #"D";
break;
default:
break;
}
result.text = myText;
}
if (thing.hidden == YES) {
int rNumber = rand() % 3;
I just met this question. It's because of the memory management, I send the button related action to a released viewController. Hope this help.(BTW,I use the arc mode)

On shake... iOS

So I have an IBAction yesNo that I want to be ran on a shake event. Not all too sure why this is not working. Have followed all the documentation.
-(BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake)
{
[self yesNo];
}
}
And then the IBAction itself:
- (IBAction)yesNo
{
int rNumber = rand() % 26;
NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
if ([[def objectForKey:#"activeVersion"] isEqualToString:#"0"])
{
switch (rNumber) {
case 0:
result.text = #"Never";
break;
case 1:
result.text = #"If you're lucky...";
break;
case 3:
result.text = #"Think twice";
break;
...
default:
break;
}
}
else if ([[def objectForKey:#"activeVersion"] isEqualToString:#"1"])
{
switch (rNumber) {
case 0:
result.text = #"Never1";
break;
case 1:
result.text = #"If you're lucky...1";
break;
...
case 25:
result.text = #"Very doubtful2";
break;
default:
break;
}
}
else if ([[def objectForKey:#"activeVersion"] isEqualToString:#"3"])
{
switch (rNumber) {
case 0:
result.text = #"Never3";
break;
...
case 25:
result.text = #"Very doubtful3";
break;
default:
break;
}
}
}
Basically what I have is a fortune ball type thing and when the iPhone is shaken I need that IBAction run.
Have you made that view the first responder? I.e. [yourView becomeFirstResponder]; (probably from some viewDidAppear: method).
You might want to check if it actually is the first responder when you shake your device.
When I was switching from defining everything graphically in IB to programmatically inline in Xcode, I forgot to make the view the first responder at all. Here's the code that ultimately fixed it:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self becomeFirstResponder];
NSLog(#"self is first responder %i",[self isFirstResponder]);
}
First thing I would question is whether this should be defined as an IBAction. If you only call it from code then you might want to consider using (void) instead (Simply a style choice).
Secondly, are you sure that the method is actually being called? Throw an NSLog in there to make sure.
Thirdly, are you sure that [def objectForKey:#"activeVersion"] returns a string? Is the value returned what you would expect? Throw an NSLog in there to make sure.
My guess is that one of the NSLogs will give you the answer to your question as the rest of your code seems fine.