Page 1 of 1 [ 8 posts ] 

Madbones
Veteran
Veteran

User avatar

Joined: 7 Mar 2010
Age: 27
Gender: Male
Posts: 777
Location: In the zone

24 Jan 2012, 9:06 pm

Hey!
Im coding an iPhone app and I have stumbled apon this rather strange problem. Help!
This is my code:
-(void) viewDidLoad{
[self plusValByOne];

[super viewDidLoad]
}
-(void) plusValByOne{
int one = 0;
one++;
NSLog(@"%i",one);
}
When I go to another view, and come back to the main view ( thats the code above) the value one in "plusValByOne" does not increase. Help! What do I do?
The reason I have a system like this is because I have background music on the main menu that is executed in the view did load method. But every time I go to my about screen and come back to the main menu it plays the music again in the background (So you get the same tracks playing at the same time) so this is why I have added the plusValByOne method, so if(one < 2){[backgroundMusic play]}
Thanks!
Sorry if it seems I have been flooding the WP CMST today, just been doing allot today.
Thanks guys :D
PS:
I know this is really simple (likely) but for some reason my head is in a huge cloud of fog at the moment!
:lol:


_________________
?Sometimes when you innovate, you make mistakes. It is best to admit them quickly, and get on with improving your other innovations.? -Steve Jobs.


mercercles
Butterfly
Butterfly

User avatar

Joined: 28 Sep 2011
Age: 38
Gender: Male
Posts: 9
Location: Michigan, USA

24 Jan 2012, 11:14 pm

I'm not at all familiar with iPhone development, but what happens if you put "static" in front of "int one = 0;"? Or move the declaration / initialization of "one" out of the function and into a class field or global variable?



NeantHumain
Veteran
Veteran

User avatar

Joined: 24 Jun 2004
Age: 44
Gender: Male
Posts: 4,837
Location: St. Louis, Missouri

24 Jan 2012, 11:19 pm

Madbones wrote:
Hey!
Im coding an iPhone app and I have stumbled apon this rather strange problem. Help!
This is my code:
-(void) viewDidLoad{
[self plusValByOne];

[super viewDidLoad]
}
-(void) plusValByOne{
int one = 0;
one++;
NSLog(@"%i",one);
}
When I go to another view, and come back to the main view ( thats the code above) the value one in "plusValByOne" does not increase. Help! What do I do?
The reason I have a system like this is because I have background music on the main menu that is executed in the view did load method. But every time I go to my about screen and come back to the main menu it plays the music again in the background (So you get the same tracks playing at the same time) so this is why I have added the plusValByOne method, so if(one < 2){[backgroundMusic play]}
Thanks!
Sorry if it seems I have been flooding the WP CMST today, just been doing allot today.
Thanks guys :D
PS:
I know this is really simple (likely) but for some reason my head is in a huge cloud of fog at the moment!
:lol:

I don't know Objective-C, but I assume it has something to do with this method:
Code:
-(void) plusValByOne {
    int one = 0;
    one++;
    NSLog(@"%i",one);
}

1. A method is declared named plusValByOne whose return type is void.
2. Inside this method, a local variable is declared named one, initialized to the integer literal value 0.
3. The variable one is incremented by one and assigned the new value using the post-increment operator.
4. Some sort of constructor or function called NSLog (again, I'm not familiar with Objective-C, OPENSTEP, the Cocoa API, or the iOS SDK) is called that appears to be logging the value of one, formatted as a string. I would expect the logged value to be 1.



NeantHumain
Veteran
Veteran

User avatar

Joined: 24 Jun 2004
Age: 44
Gender: Male
Posts: 4,837
Location: St. Louis, Missouri

24 Jan 2012, 11:50 pm

Here's a rough attempt at what you want, given no prior knowledge of Objective-C, OPENSTEP or the Cocoa API, the iOS SDK, etc.:

Code:
#include <NSObjCRuntime.h>
#include <NSView.h>

// Class MyView inherits from superclass NSView
@interface MyView : NSView {
    // Declare a protected field 'one'
    @protected
      int one;
}
// Method to increment 'one' by 1 and log the sum
- (void)plusValByOne;
@end

@implementation MyView
// Initialize the object
- (id)init {
    self = [super init];
    if (self) {
        // The initial value of 'one' should be 0 for any instance of MyView
        one = 0;
    }
    return self;
}

// Override method viewDidLoad from superclass
- (void)viewDidLoad {
    // Call instance method plusValByOne on this object/the self-object
    [self plusValByOne];
    // Now call the overridden method viewDidLoad to retain the API's behavior
    [super viewDidLoad];
}

// Increment the value of field 'one' by 1
- (void)plusValByOne {
    // Increment 'one' by 1
    one++;
    // Log the sum
    NSLog(@"%i", one);
}
@end



NeantHumain
Veteran
Veteran

User avatar

Joined: 24 Jun 2004
Age: 44
Gender: Male
Posts: 4,837
Location: St. Louis, Missouri

25 Jan 2012, 12:11 am

It may help to explain the concepts behind the example, briefly. In an object-oriented programming language like Objective-C, one works with objects, which amount to glorified data values, but whereas instead of working only with simple types of data like integers or floating-point numbers, one works with classes that are composite types of these primitive data types.

Classes evolved from the simpler data structures seen in older programming languages like C and even some macro assemblers. The concept of the class tied the kinds of behavior that could be performed on a data structure more tightly with the definition of the data type. These are typically called methods of the class. The concept also abstracts away the internal implementation of a class (i.e., its definition) from its interface (i.e., its declaration). This is accomplished by limiting the scope of a class's fields and methods. For example, fields and methods of private scope are usable only to the class itself whereas those in public scope can be used by any code outside the class as well. Class-inheritance/type hierarchies facilitate reuse of code with predictable interfaces.

The object-oriented paradigm effectively creates a higher level of abstraction away from the computer so that the programmer can conceive of the program in terms of interacting objects rather than a sequence of commands (mere imperative programming, of which OOP is a superset) or the allocation and de-allocation of data from ranges of memory or registers on the CPU followed by operations encoded in the processor (of course, this is the machine code that is actually run by the physical computer).



mcg
Veteran
Veteran

User avatar

Joined: 26 Jan 2010
Age: 34
Gender: Male
Posts: 538
Location: Sacramento

25 Jan 2012, 2:31 am

You made this exact same thread 4 months ago: http://www.wrongplanet.net/postt175848.html



Madbones
Veteran
Veteran

User avatar

Joined: 7 Mar 2010
Age: 27
Gender: Male
Posts: 777
Location: In the zone

25 Jan 2012, 7:36 am

mcg wrote:
You made this exact same thread 4 months ago: http://www.wrongplanet.net/postt175848.html

Sorry, I was half a sleep at the time I made this post.


_________________
?Sometimes when you innovate, you make mistakes. It is best to admit them quickly, and get on with improving your other innovations.? -Steve Jobs.


raykusray
Yellow-bellied Woodpecker
Yellow-bellied Woodpecker

User avatar

Joined: 26 Jan 2012
Age: 34
Gender: Female
Posts: 67

31 Jan 2012, 9:47 am

Because every time you execute the loop, you reset the value of variable "one" to 0.

}
-(void) plusValByOne{
int one = 0;
one++;
NSLog(@"%i",one);
}

every time this executes, one=0; then one ++; That returns one ==1, I believe.

Please don't put your variable initializer inside the loop. Put it where you would put an initializer or constant values for the method. I'm not an iphone app programmer, but basic programming logic should still apply.