Mark_Co
asked on
simple iOS increment/decrement app multiplication question
Feeling pretty silly that I can't get this to work
I'm making a basic app to do this:
I am lazily instantiating my byTwoCounter and byThreeCounter getter methods and I ca't get the compiler to stoop griping about me trying to do simple multiplication:
I'm making a basic app to do this:
I am lazily instantiating my byTwoCounter and byThreeCounter getter methods and I ca't get the compiler to stoop griping about me trying to do simple multiplication:
//
// MVCDemoViewController.m
// TheCounter
//
// Created by on 2/7/13.
// Copyright (c) 2013 All rights reserved.
//
#import "MVCDemoViewController.h"
#import "Counter.h"
//private
@interface MVCDemoViewController ()
@property (nonatomic) Counter *byOneCounter;
@property (nonatomic) Counter *byTwoCounter;
@property (nonatomic) Counter *byThreeCounter;
@property (weak, nonatomic) IBOutlet UILabel *byOneLabel;
@property (weak, nonatomic) IBOutlet UILabel *byTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *byThreeLabel;
@end
@implementation MVCDemoViewController
//lazy instatiation always refers to getter
//this is the getter method
- (Counter *)byOneCounter
{
if(!_byOneCounter) _byOneCounter = [[Counter alloc] initWithInitialCount:0];
return _byOneCounter;
}
- (Counter *)byTwoCounter
{
if (!_byTwoCounter) _byTwoCounter = [[Counter alloc] initWithInitialCount:0];
_byTwoCounter *= 2; <---- THIS LINE WON'T WORK. I TRIED 'SELF' AS WELL BUT NO DICE
return _byTwoCounter;
}
- (Counter *)byThreeCounter
{
if (!_byThreeCounter)
_byThreeCounter = [[Counter alloc] initWithInitialCount:0];
return _byThreeCounter;
}
//ctrl+dragged increment to the yellow
//box Demo View Controller
//to connect same for decrement. Connected them up that way
//once that is done, I can now tap and see action happen
- (IBAction)incrementTapped:(UIButton *)sender
{
//update the byOneCounter instance
[self.byOneCounter increment]; //byOneCounter.increment
//update the labels
self.byOneLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byTwoLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byThreeLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
NSLog(@"increment tapped");
}
- (IBAction)decrementTapped:(UIButton *)sender
{
//update counter
[self.byOneCounter decrement];
//update labels
self.byOneLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byTwoLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byThreeLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
NSLog(@"decrement tapped");
}
@end
ASKER
Sorry, I didn't post the other code because I figured my error was right there. Here it is
//
// Counter.h
// TheCounter
//
#import <Foundation/Foundation.h>
@interface Counter : NSObject
@property (nonatomic) NSInteger currentCount;
// designated initializer
- (id)initWithInitialCount:(NSInteger)initialCount;
- (void)increment;
- (void)decrement;
@end
//
// Counter.m
// TheCounter
//
//
#import "Counter.h"
@implementation Counter
//override the designated init
- (id)initWithInitialCount:(NSInteger)initialCount
{
//must always be done in designated init
//convenience inits must call the des init this way
self = [super init];
if (self)
{
//give the instance variables initial values
self.currentCount = initialCount;
//[self setCurrentCount:initialCount]; //either way works
}
return self;
}
//convenience init
- (id)init
{
return [self initWithInitialCount:0];
}
- (void)increment
{
self.currentCount++; //access the property setter
//not the "_currentCount" instance var
}
- (void)decrement
{
self.currentCount--;
}
@end
//
// StepCounter.h
// TheCounter
//
#import "Counter.h"
//public
@interface StepCounter : Counter
//instance var
@property (nonatomic, readonly) NSInteger stepAmount;
//designated initializer
- (id)initWithInitialCount:(NSInteger)initialCount
stepAmount:(NSInteger)stepAmount;
@end
//
// StepCounter.m
// TheCounter
//
//
#import "StepCounter.h"
@implementation StepCounter
- (id)initWithInitialCount:(NSInteger)initialCount stepAmount:(NSInteger)stepAmount
{
//self = [super init]; rule
//call super init on my superclass's des init
self = [super initWithInitialCount:initialCount];
if (self)
{
//init instance vars
_stepAmount = stepAmount; //getter = param
}
return self;
}
@end
ASKER
_byTwoCounter.currentCount er *= 2;
prevents errors now, so thank you for that. However, the logic isn't working.
My intent is is to make the 2x label increment by twice the current count.
And then for the third label I need to increment by 3x the current count. Am I missing something?
I'll add my controller here for you to see as well:
prevents errors now, so thank you for that. However, the logic isn't working.
My intent is is to make the 2x label increment by twice the current count.
And then for the third label I need to increment by 3x the current count. Am I missing something?
I'll add my controller here for you to see as well:
//
// MVCDemoViewController.h
// TheCounter
//
//
//
#import <UIKit/UIKit.h>
//public
@interface MVCDemoViewController : UIViewController
@end
//
// MVCDemoViewController.m
// TheCounter
//
//
#import "MVCDemoViewController.h"
#import "Counter.h"
//private
@interface MVCDemoViewController ()
@property (nonatomic) Counter *byOneCounter;
@property (nonatomic) Counter *byTwoCounter;
@property (nonatomic) Counter *byThreeCounter;
@property (weak, nonatomic) IBOutlet UILabel *byOneLabel;
@property (weak, nonatomic) IBOutlet UILabel *byTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *byThreeLabel;
@end
@implementation MVCDemoViewController
//lazy instatiation always refers to getter
//this is the getter method
- (Counter *)byOneCounter
{
if(!_byOneCounter) _byOneCounter = [[Counter alloc] initWithInitialCount:0];
return _byOneCounter;
}
- (Counter *)byTwoCounter
{
if (!_byTwoCounter) _byTwoCounter = [[Counter alloc] initWithInitialCount:0];
_byTwoCounter.currentCount *= 2;
return _byTwoCounter;
}
- (Counter *)byThreeCounter
{
if (!_byThreeCounter)
_byThreeCounter = [[Counter alloc] initWithInitialCount:0];
return _byThreeCounter;
}
//ctrl+dragged increment to the yellow
//box Demo View Controller
//to connect same for decrement. Connected them up that way
//once that is done, I can now tap and see action happen
- (IBAction)incrementTapped:(UIButton *)sender
{
//update the byOneCounter instance
[self.byOneCounter increment]; //byOneCounter.increment
//update the labels
self.byOneLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byTwoLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byThreeLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
NSLog(@"increment tapped");
}
- (IBAction)decrementTapped:(UIButton *)sender
{
//update counter
[self.byOneCounter decrement];
//update labels
self.byOneLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byTwoLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byThreeLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
NSLog(@"decrement tapped");
}
@end
That's your code for the Increment button:
In this line:
you call:
What exactly you wanted to do?
Maybe, you need to increment all three elements? Something like:
- (IBAction)incrementTapped:(UIButton *)sender
{
//update the byOneCounter instance
[self.byOneCounter increment]; //byOneCounter.increment
//update the labels
self.byOneLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byTwoLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
self.byThreeLabel.text = [NSString stringWithFormat:@"%d", self.byOneCounter.currentCount];
NSLog(@"increment tapped");
}
In this line:
[self.byOneCounter increment];
you call:
- (Counter *)byOneCounter
{
if(!_byOneCounter) _byOneCounter = [[Counter alloc] initWithInitialCount:0];
return _byOneCounter;
}
What exactly you wanted to do?
Maybe, you need to increment all three elements? Something like:
[self.byOneCounter increment];
[self.byTwoCounter increment];
[self.byThreeCounter increment];
ASKER
Thank you.
when the increment button is clicked I want
by one counter to return and display
1, then 2, then 3 (each click of incrment)....
then byTwoCounter will also display
return 2, 4,6,8....
then also byThreeCounter displays
3,6,9,12...
just like i have in the picture at the very top
when the increment button is clicked I want
by one counter to return and display
1, then 2, then 3 (each click of incrment)....
then byTwoCounter will also display
return 2, 4,6,8....
then also byThreeCounter displays
3,6,9,12...
just like i have in the picture at the very top
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
You've been very helpful. Much appreciated :)
Probably, this line is just an error:
_byTwoCounter *= 2; <---- THIS LINE WON'T WORK. I TRIED 'SELF' AS WELL BUT NO DICE
It could work if _byTwoCounter is NSInteger, but in your case it's Counter. Probably class Counter has a property currentCount and so the line could look like:
_byTwoCounter.currentCount