The instance variables of an object work to maintain the state of the object. As the application journeys through multiple touch events, the state of some objects must be retained in ways which repeatedly allocating and releasing objects can not accomplish.
OOP must provide for this in some way other than by the use of declaring class variables instead of instance variables. I am an old assembly language programmer from the Z80 days. I have been playing with the Objective C platform for the IPhone and can not find another way to maintain the object "oporate"'s state variables through multiple "testViewController" event methods without declaring "oporate"'s methods as class instead of instance.
Below is the simple test code I used to demonstrate this. Perhaps, my old brain just missed something obvious.
In the simple test code below, the "oporate" class variable "remember" maintains its value through multiple "testViewController" methods. The compile succeeds and the simulation runs with out fault, but the compiler grunts "warning: instance variable -remember- accessed in class method"
Is there another way to declare this variable? one such that it retains its value. Perhaps some use of the retain method, but in what way?
How does the adroit OOPs programmer maintain the values of the state variables of an object that lives the life of the application?
Jerry
testViewController.h
testViewController.m
oporate.h
oporate.m
Holy rats!!
Is getting rid of the compiler hicup as simple as?
Instead of
But this still leaves the question: Must state values which must be maintained through multiple methods be declared in classes instead of instances of classes?
Jerry
OOP must provide for this in some way other than by the use of declaring class variables instead of instance variables. I am an old assembly language programmer from the Z80 days. I have been playing with the Objective C platform for the IPhone and can not find another way to maintain the object "oporate"'s state variables through multiple "testViewController" event methods without declaring "oporate"'s methods as class instead of instance.
Below is the simple test code I used to demonstrate this. Perhaps, my old brain just missed something obvious.
In the simple test code below, the "oporate" class variable "remember" maintains its value through multiple "testViewController" methods. The compile succeeds and the simulation runs with out fault, but the compiler grunts "warning: instance variable -remember- accessed in class method"
Is there another way to declare this variable? one such that it retains its value. Perhaps some use of the retain method, but in what way?
How does the adroit OOPs programmer maintain the values of the state variables of an object that lives the life of the application?
Jerry
testViewController.h
Code:
/*
*testViewController.h
*/
#import <UIKit/UIKit.h>
#import "oporate.h"
@interface testViewController : UIViewController {
IBOutlet UILabel *label; }
-(IBAction) button1: (id) sender;
-(IBAction) button2: (id) sender;
@property (nonatomic, retain) UILabel *label;
@end
Code:
/*
*testViewController.m
*/
#import "testViewController.h"
#import "oporate.h"
@implementation testViewController
@synthesize label;
-(IBAction) button1: (id) sender {
[oporate setrem];
label.text = [NSString stringWithFormat: @"%i", [oporate addone]]; }
-(IBAction) button2: (id) sender {
[oporate setrem2];
label.text = [NSString stringWithFormat: @"%i", [oporate addten]]; }
-(void) viewDidLoad {
[oporate start];
[super viewDidLoad]; }
-(void) dealloc {
[super dealloc]; }
@end
Code:
/*
*oporate.h
*/
#import <Foundation/Foundation.h>
@interface oporate : NSObject {
int remember; }
+(void) start;
+(void) addone;
+(void) addten;
+(int) recallrem;
@end
Code:
/*
*oporate.m
*/
#import "oporate.h"
@implementation oporate
+(void) start {
remember = 1; }
+(void) addone {
remember = remember + 1; }
+(void) addten {
remember = remember + 10; }
+(int) recallrem {
return remember; }
@end
Holy rats!!
Is getting rid of the compiler hicup as simple as?
Code:
/*
*oporate.h
*/
#import <Foundation/Foundation.h>
int remember;
@interface oporate : NSObject { }
....
Code:
/*
*oporate.h
*/
#import <Foundation/Foundation.h>
@interface oporate : NSObject {
int remember; }
....
But this still leaves the question: Must state values which must be maintained through multiple methods be declared in classes instead of instances of classes?
Jerry