Objective C Calculator Programming Variable - objective-c

Okay, so I've been working on the Stanford iOS development course that they have posted for free online. I've been working on figuring out how to make a programable variable. It has been working fine so far, aside from the fact that I think the following lines of code are programming the variable to be #"x=" instead of the previous number entered.
The View Controller:
#import "ViewController.h"
#import "CalculatorBrain.h"
#interface ViewController()
#property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
#property (nonatomic) BOOL userPressedSomethingElse;
#property (nonatomic, strong) CalculatorBrain *brain;
#end
#implementation ViewController
#synthesize display;
#synthesize inputHistory;
#synthesize userPressedSomethingElse;
#synthesize userIsInTheMiddleOfEnteringANumber;
#synthesize brain = _brain;
- (CalculatorBrain *)brain
{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
NSString *xValue = #"0";
- (IBAction)enterPressed
// A specific action if enter is pressed
{
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringANumber = NO;
if (self.userPressedSomethingElse)
{
self.inputHistory.text = [self.inputHistory.text stringByAppendingString:#" "];
}
self.userPressedSomethingElse = NO;
}
- (IBAction)variableChanged:(id)sender
{
if (self.userIsInTheMiddleOfEnteringANumber)
{
[self enterPressed];
}
NSString *operation = [sender currentTitle];
xValue = [self.brain programVariable:operation];
self.inputHistory.text = [self.inputHistory.text stringByAppendingString:#"X="];
self.inputHistory.text = [self.inputHistory.text stringByAppendingString:xValue];
}
The Calculator Brain (the .m one):
#import "CalculatorBrain.h"
#interface CalculatorBrain()
#property (nonatomic,strong) NSMutableArray *operandStack;
#end
#implementation CalculatorBrain
#synthesize operandStack = _operandStack;
- (NSMutableArray *) operandStack
{
if (!_operandStack)
{
_operandStack = [[NSMutableArray alloc] init];
}
return _operandStack;
}
- (void) pushOperand:(double)operand
{
NSNumber *operandObject = [NSNumber numberWithDouble:operand];
[self.operandStack addObject:operandObject];
}
- (double)popOperand
{
NSNumber *operandObject = [self.operandStack lastObject];
if (operandObject) [self.operandStack removeLastObject];
return [operandObject doubleValue];
}
- (NSString *) programVariable: (NSString *) operation
{
double result = [self popOperand];
NSString *resultString = [NSString stringWithFormat:#"%.2d",result];
return resultString;
}
The .h Calculator Brain:
#import <Foundation/Foundation.h>
#interface CalculatorBrain : NSObject
- (void) pushOperand: (double) operand;
- (double) performOperation: (NSString *) operation;
- (NSString *) programVariable: (NSString *) operation;
#end
The button that is pushed says "x=", and because of some tracing statements I added, I have figured out that this is being set to xValue. However, I don't know how to fix it... Any ideas?

In your variableChanged:method, you possibly want if (!self.userIsInTheMiddleOfEnteringANumber) at the beginning. Note the logical negation, which I derive the need from the sense of your variable name. I see no way that variable is set, so I trust you are properly setting it elsewhere in your app.
Also, change the variable xValue to a instance variable on ViewController. It is currently a global static variable. If you have more than one ViewController objects created, you will have problems.

Related

Information in text fields do not get retained objective c

This is a simple program that takes two numbers inputed by the user in two separate text fields and adds them together when the 'Add' button is clicked. The way I have done this is by subclassing NSTextField into a class called MyTextField. I believe my error has something to do with memory leaks, but being an inexperienced programmer, I'm not quite sure how to deal with allocating and deallocating memory.
The problem is: When I click the Add button, it works perfectly. However, when I click it again, it does not show the correct amount. It keeps taking my inputs as nil and outputting 0. I have attached my code.
Note: I know that this is much more complicate than it needs to be! I am simply showing a simpler version of a much more complicated program that displays the exact same error.
MyTextField.h
#import <Cocoa/Cocoa.h>
#interface MyTextField : NSTextField
#property (strong) NSString* number;
#property double dblNum;
-(id)initWithNumber:(NSString *)number;
-(NSString *)getNumber;
-(void)setNumber;
-(double)getDblNum;
-(void)setDblNum;
-(double)calcSum:(MyTextField *)other;
-(NSString *)description;
#end
MyTextField.m
#import "MyTextField.h"
#implementation MyTextField
-(id)initWithNumber:(NSString *)number{
self = [super init];
if (self) {
if ([number length] > 0){
_number = number;
}else{
_number = #"0";
}
[self setDblNum];
}
return self;
}
- (id)init {
return [self initWithNumber:[self getNumber]];
}
-(NSString *)getNumber{
return _number;
}
-(void)setNumber{
_number = [self stringValue];
}
-(double)getDblNum{
return _dblNum;
}
-(void)setDblNum{
_dblNum = [_number doubleValue];
}
-(double)calcSum:(MyTextField *)other{
return [self getDblNum] + [other getDblNum];
}
- (NSString *)description{
return [NSString stringWithFormat: #"Number: %f", _dblNum];
}
#end
ViewController.h
#import <Cocoa/Cocoa.h>
#import "MyTextField.h"
#interface ViewController : NSViewController
#property (strong) IBOutlet MyTextField *valueOne;
#property (strong) IBOutlet MyTextField *valueTwo;
#property (strong) IBOutlet NSTextField *answer;
- (IBAction)btnAdd:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "MyTextField.h"
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
}
- (IBAction)btnAdd:(id)sender {
_valueOne = [[MyTextField alloc] initWithNumber:[_valueOne stringValue]];
_valueTwo = [[MyTextField alloc] initWithNumber:[_valueTwo stringValue]];
double ans = [_valueOne calcSum:_valueTwo];
_answer.stringValue = [NSString stringWithFormat:#"%.2f", ans];
}
#end
P.S. Sorry for the annoying amount of code. Thanks!
Your btnAdd: is where the problem is.
- (IBAction)btnAdd:(id)sender {
_valueOne = [[MyTextField alloc] initWithNumber:[_valueOne stringValue]];
_valueTwo = [[MyTextField alloc] initWithNumber:[_valueTwo stringValue]];
double ans = [_valueOne calcSum:_valueTwo];
_answer.stringValue = [NSString stringWithFormat:#"%.2f", ans];
}
Each time the button is tapped, you're recreating both MyTextField instances. Instead, you simply need to get & set the values from your existing valueOne and valueTwo outlets, like:
- (IBAction)btnAdd:(id)sender {
double value1 = [_valueOne.text doubleValue];
double value2 = [_valueTwo.text doubleValue];
double ans = value1 + value2;
_answer.stringValue = [NSString stringWithFormat:#"%.2f", ans];
}
Obviously, you need to check the validity of the valueOne and valueTwo text strings.
You really don't need a MyTextField subclass, because it's not doing anything except converting strings to doubles (and vice versa).

Notification when any property changes [duplicate]

Put simply, is there a way to receive a general notification when any property in an Objective-C class is changed? I know I can use KVO to monitor particular property changes, but I have the need to call a particular method whenever any setProperty: message is sent to my class. I want to be able to receive a generic notification without any concern about which property in particular was modified.
If it helps to clarify why I want to do this, I am making use of some fast table scrolling code found here: http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/
Part of the process of accomplishing this is that whenever a property in a table view cell is modified, [ self setNeedsDisplay ] needs to be called. I'd rather not have to override the setter methods for every property in my class just to make this call.
As Chuck notes, you can create a dependent key, or of course you can directly observe all the properties (which is less work than overloading the setters).
Using the Objective-C runtime, if you exclusively use properties, you can automate this process using class_copyPropertyList(). But I'd probably only do this if this problem comes up a bit for you. If you only have one instance of this problem, it's probably easier and safer and more maintainable just to directly observe the list of properties unless you feel like working in the ObjC runtime.
Here's an example built off of Chuck and Rob's suggestions:
DrakeObject.h
#interface DrakeObject : NSObject
#property (nonatomic, strong) NSNumber *age;
#property (nonatomic, strong) NSNumber *money;
#property (nonatomic, strong) NSString *startPosition;
#property (nonatomic, strong) NSString *currentPosition;
#property (nonatomic, strong, readonly) id propertiesChanged;
#end
DrakeObject.m
#implementation DrakeObject
- (instancetype)init {
self = [super init];
if (self) {
self.age = #25;
self.money = #25000000;
self.startPosition = #"bottom";
self.currentPosition = #"here";
}
return self;
}
- (id)propertiesChanged {
return nil;
}
+(NSSet *)keyPathsForValuesAffectingPropertiesChanged {
return [NSSet setWithObjects:#"age", #"money", #"startPosition", #"currentPosition", nil];
}
observing propertiesChanged will let us know anytime a property has changed.
[self.drakeObject addObserver:self
forKeyPath:#"propertiesChanged"
options:NSKeyValueObservingOptionNew
context:nil];
Not exactly. You can create a dependent key that depends on every property you wish to expose and then observe that. That's about as close as you'll get, I think.
Here an example of code. I have a general object and dother object. Dother object has to save his state on change each property.
#import <Foundation/Foundation.h>
#interface GeneralObject : NSObject
+ (instancetype)instanceWithDictionary:(NSDictionary *)aDictionary;
- (instancetype)initWithDictionary:(NSDictionary *)aDictionary;
- (NSDictionary *)dictionaryValue;
- (NSArray *)allPropertyNames;
#end
implementation
#import "GeneralObject.h"
#import <objc/runtime.h>
#implementation GeneralObject
#pragma mark - Public
+ (instancetype)instanceWithDictionary:(NSDictionary *)aDictionary {
return [[self alloc] initWithDictionary:aDictionary];
}
- (instancetype)initWithDictionary:(NSDictionary *)aDictionary {
aDictionary = [aDictionary clean];
for (NSString* propName in [self allPropertyNames]) {
[self setValue:aDictionary[propName] forKey:propName];
}
return self;
}
- (NSDictionary *)dictionaryValue {
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSArray *propertyNames = [self allPropertyNames];
id object;
for (NSString *key in propertyNames) {
object = [self valueForKey:key];
if (object) {
[result setObject:object forKey:key];
}
}
return result;
}
- (NSArray *)allPropertyNames {
unsigned count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
NSMutableArray *array = [NSMutableArray array];
unsigned i;
for (i = 0; i < count; i++) {
objc_property_t property = properties[i];
NSString *name = [NSString stringWithUTF8String:property_getName(property)];
[array addObject:name];
}
free(properties);
return array;
}
#end
and after all we have dother class, which should save his state on each change of any property
#import "GeneralObject.h"
extern NSString *const kUserDefaultsUserKey;
#interface DotherObject : GeneralObject
#property (strong, nonatomic) NSString *firstName;
#property (strong, nonatomic) NSString *lastName;
#property (strong, nonatomic) NSString *email;
#end
and implementation
#import "DotherObject.h"
NSString *const kUserDefaultsUserKey = #"CurrentUserKey";
#implementation DotherObject
- (instancetype)initWithDictionary:(NSDictionary *)dictionary {
if (self = [super initWithDictionary:dictionary]) {
for (NSString *key in [self allPropertyNames]) {
[self addObserver:self forKeyPath:key options:NSKeyValueObservingOptionNew context:nil];
}
}
return self;
}
- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary<NSKeyValueChangeKey, id> *)change context:(nullable void *)context {
NSDictionary *dict = [self dictionaryValue];
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:kUserDefaultsUserKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSString *)description {
return [NSString stringWithFormat:#"%#; dict:\n%#", [super description], [self dictionaryValue]];
}
#end
Happy coding!

Using a property as a count in Objective C

I am new to Objective C. I was following Stanford lectures 2011-12 fall on iOS development and in assignment 1 it asks to implement a decimal point in a calculator. This is what my implementation looks like:
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
#interface CalculatorViewController()
#property (nonatomic) BOOL userIsInTheMiddleOfEnteringNumber;
#property (nonatomic,strong) CalculatorBrain *brain;
#property (nonatomic) int userPressedDecimalPoint;
#end
#implementation CalculatorViewController
#synthesize display = _display;
#synthesize userIsInTheMiddleOfEnteringNumber = _userIsInTheMiddleOfEnteringNumber;
#synthesize userPressedDecimalPoint = _userPressedDecimalPoint;
#synthesize brain = _brain;
- (CalculatorBrain *) brain{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
- (IBAction)digitpressed:(UIButton *)sender {
NSString *digit = sender.currentTitle;
if (digit == #"."){
self.userIsInTheMiddleOfEnteringNumber = YES;
self.userPressedDecimalPoint++;
}
if (self.userIsInTheMiddleOfEnteringNumber && self.userPressedDecimalPoint< 2){
self.display.text = [self.display.text stringByAppendingString:digit];
NSLog(#"decimal pressed: %d times",self.userPressedDecimalPoint);
}
else{
self.display.text = digit;
self.userIsInTheMiddleOfEnteringNumber = YES;
self.userPressedDecimalPoint = 1;
}
}
So basically I have setup a property userPressedDecimalPoint and i try to use this property as a counter every time the decimal point is pressed. However from the NSLog i see that no matter how many times i press the decimal point it only shows 1 time pressed. Consequently, the output shows multiple decimal points if entered. Any suggestions would be appreciated. Thanks!
Full version of Corrected Code:
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
#interface CalculatorViewController()
#property (nonatomic) BOOL userIsInTheMiddleOfEnteringNumber;
#property (nonatomic,strong) CalculatorBrain *brain;
#property (nonatomic) int userPressedDecimalPoint;
#end
#implementation CalculatorViewController
#synthesize display = _display;
#synthesize userIsInTheMiddleOfEnteringNumber = _userIsInTheMiddleOfEnteringNumber;
#synthesize userPressedDecimalPoint = _userPressedDecimalPoint;
#synthesize brain = _brain;
- (CalculatorBrain *) brain{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
- (IBAction)digitpressed:(UIButton *)sender {
NSString *digit = sender.currentTitle;
if ([digit isEqualToString:#"."]){
self.userIsInTheMiddleOfEnteringNumber = YES;
self.userPressedDecimalPoint++;
}
if (self.userIsInTheMiddleOfEnteringNumber && self.userPressedDecimalPoint< 2){
self.display.text = [self.display.text stringByAppendingString:digit];
// NSLog(#"decimal pressed: %d times",self.userPressedDecimalPoint);
}
else if (![digit isEqualToString:#"."]){
self.display.text = digit;
self.userIsInTheMiddleOfEnteringNumber = YES;
}
}
- (IBAction)operationPressed:(UIButton *)sender {
if (self.userIsInTheMiddleOfEnteringNumber) [self enterPressed];
double result = [self.brain performOperation:sender.currentTitle];
NSString *resultString = [NSString stringWithFormat:#"%g", result];
self.display.text = resultString;
}
- (IBAction)enterPressed {
self.userPressedDecimalPoint = 0;
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringNumber = NO;
}
#end
Unless there is something that sets userIsInTheMiddleOfEnteringNumber to NO, any time the number is greater than one, your code will execute self.userPressedDecimalPoint = 1; and set it back to one.

random generator to obtain data from array not displaying

I know theres a better solution using arc4random (it's on my to-try-out-function list), but I wanted to try out using the rand() and stand(time(NULL)) function first. I've created a NSMutableArray and chuck it with 5 data. Testing out how many number it has was fine. But when I tried to use the button function to load the object it return me with object <sampleData: 0x9a2f0e0>
- (IBAction)generateNumber:(id)sender {
srand(time(NULL));
NSInteger number =rand()% ds.count ;
label.text = [NSString stringWithFormat:#"object %#", [ds objectAtIndex:number] ];
NSLog(#"%#",label.text);
}
While I feel the main cause is the method itself, I've paste the rest of the code below just incase i made any error somewhere.
ViewController.h
#import <UIKit/UIKit.h>
#import "sampleData.h"
#import "sampleDataDAO.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UIButton *onHitMePressed;
- (IBAction)generateNumber:(id)sender;
#property(nonatomic, strong) sampleDataDAO *daoDS;
#property(nonatomic, strong) NSMutableArray *ds;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize label;
#synthesize onHitMePressed;
#synthesize daoDS,ds;
- (void)viewDidLoad
{
[super viewDidLoad];
daoDS = [[sampleDataDAO alloc] init];
self.ds = daoDS.PopulateDataSource;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setLabel:nil];
[self setOnHitMePressed:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)generateNumber:(id)sender {
srand(time(NULL));
NSInteger number =rand()% ds.count ;
label.text = [NSString stringWithFormat:#"object %#", [ds objectAtIndex:number] ];
NSLog(#"%#",label.text);
}
#end
sampleData.h
#import <Foundation/Foundation.h>
#interface sampleData : NSObject
#property (strong,nonatomic) NSString * object;
#end
sampleData.m
#import "sampleData.h"
#implementation sampleData
#synthesize object;
#end
sampleDataDAO.h
#import <Foundation/Foundation.h>
#import "sampleData.h"
#interface sampleDataDAO : NSObject
#property(strong,nonatomic)NSMutableArray*someDataArray;
-(NSMutableArray *)PopulateDataSource;
#end
sampleDataDAO.m
#import "sampleDataDAO.h"
#implementation sampleDataDAO
#synthesize someDataArray;
-(NSMutableArray *)PopulateDataSource
{
someDataArray = [[NSMutableArray alloc]init];
sampleData * myData = [[sampleData alloc]init];
myData.object= #"object 1";
[someDataArray addObject:myData];
myData=nil;
myData = [[sampleData alloc] init];
myData.object= #"object 2";
[someDataArray addObject:myData];
myData=nil;
myData = [[sampleData alloc] init];
myData.object= #"object 3";
[someDataArray addObject:myData];
myData=nil;
myData = [[sampleData alloc] init];
myData.object= #"object 4";
[someDataArray addObject:myData];
myData=nil;
myData = [[sampleData alloc] init];
myData.object= #"object 5";
[someDataArray addObject:myData];
myData=nil;
return someDataArray;
}
#end
what i guess is going on is that nslog function cant print the data inside your sample data class because its not a standard class. not standard classes must implement the "description" method. What you get when you print out your class is the pointer to it, because nslog has no way of knowing how to print out the data in your class.
if what you want to print on that label/nslog is the nsstring inside your class "sampledata" you should access the property.
this can be done in the following way:
SampleData *instanceOfSampleData = (SampleData*)[ds objectAtIndex:number];
label.text = [NSString stringWithFormat:#"object %#", instanceOfSampleData.object];

I can not figure out how to resolve SIGABRT error in Xcode

I am working on a basic calculator app, following a tutorial for an iTunes U class. I thought I had worked it out perfectly but when I go to run the application in the simulator, I get an unexpected error. The calculator works by entering the number, pressing the enter button, entering the second number, then the enter button and then the operation. It allows me to append digits to form a multi-digit number, but as soon as I press "enter" it quits out and says "Thread 1: Program received signal: "SIGABRT."" I have double and triple checked my code and cannot seem to find anything wrong, so i thought i would post it all on here and see if you guys can figure it out. Thanks in advance!
CalculatorBrain.h
#import <Foundation/Foundation.h>
#interface CalculatorBrain : NSObject
- (void)pushOperand:(double)operand;
- (double)performOperation:(NSString *)operation;
#end
CalculatorBrain.m
#import "CalculatorBrain.h"
#interface CalculatorBrain()
#property (nonatomic, strong) NSMutableArray *operandStack;
#end
#implementation CalculatorBrain
#synthesize operandStack = _operandStack;
- (NSMutableArray *)operandStack
{
if (!_operandStack) {
_operandStack = [[NSMutableArray alloc] init];
}
return _operandStack;
}
- (void)pushOperand:(double)operand
{
NSNumber *operandObject = [NSNumber numberWithDouble:operand];
[self.operandStack addObject:operandObject];
}
- (double)popOperand
{
NSNumber *operandObject = [self.operandStack lastObject];
if (operandObject) [self.operandStack removeLastObject];
return [operandObject doubleValue];
}
- (double)performOperation:(NSString *)operation
{
double result = 0;
if ([operation isEqualToString:#"+"]){
result = [self popOperand] + [self popOperand];
} else if ([#"*" isEqualToString:operation]) {
result = [self popOperand] * [self popOperand];
} else if ([operation isEqualToString:#"-"]) {
double subtrahend = [self popOperand];
result = [self popOperand] - subtrahend;
} else if ([operation isEqualToString:#"/"]) {
double divisor = [self popOperand];
if (divisor) result = [self popOperand] / divisor;
}
[self pushOperand:result];
return result;
}
#end
CalculatorViewController.h
#import <UIKit/UIKit.h>
#interface CalculatorViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *display;
#end
CalculatorViewController.m
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
#interface CalculatorViewController()
#property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
#property (nonatomic, strong) CalculatorBrain *brain;
#end
#implementation CalculatorViewController
#synthesize display;
#synthesize userIsInTheMiddleOfEnteringANumber;
#synthesize brain = _brain;
- (CalculatorBrain *)brain
{
if(!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
- (IBAction)digitPressed:(UIButton *)sender {
NSString *digit = [sender currentTitle];
if (self.userIsInTheMiddleOfEnteringANumber) {
self.display.text = [self.display.text stringByAppendingString:digit];
} else {
self.display.text = digit;
self.userIsInTheMiddleOfEnteringANumber = YES;
}
}
- (IBAction)enterPressed
{
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringANumber = NO;
}
- (IBAction)operationPressed:(UIButton *)sender
{
if (self.userIsInTheMiddleOfEnteringANumber) {
[self enterPressed];
}
NSString *operation = [sender currentTitle];
double result = [self.brain performOperation:operation];
self.display.text = [NSString stringWithFormat:#"%g", result];
}
#end
Open CalculatorViewController.xib file in Xcode and right-click the "File's Owner".
Check the appearing pop-up if you have any linked properties listed that are not implemented in the CalculatorViewController.h file (according to the above posted code, the only properties linked from .xib to .h should be a UILabel called "display". Delete all invalid linked properties, if any is available (invalid linked properties would be marked with a yellow warning sign).
Expand your project Executable and right click on it. and click on GetInfo->Argument tag aat end of the window you see plus and minus sign button click on + sighn button and write
Name Value
NSZombieEnabled YES
then after execute your project and whenever crash your application click on run munu-> console you see there why your application crash. please try this may be this will help you.