Why isnt my interger ++ing?
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
PS:
I know this is really simple (likely) but for some reason my head is in a huge cloud of fog at the moment!
_________________
?Sometimes when you innovate, you make mistakes. It is best to admit them quickly, and get on with improving your other innovations.? -Steve Jobs.
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
PS:
I know this is really simple (likely) but for some reason my head is in a huge cloud of fog at the moment!
I don't know Objective-C, but I assume it has something to do with this method:
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.
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.:
#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
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).
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.
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.