I’m working with a NSMutableString and having some difficulties. The app quits whenever i try to access it and it does not say that anything is amiss in the NSLog.
I set up the string in the view did load method so that my string will be able to retain its information rather than being reset, but i don’t believe my knowledge on this is correct. Any help would be great. Thanks!
#import <UIKit/UIKit.h>
@class rootViewController;
@interface Numpad : UIViewController {
rootViewController *viewController;
UIButton *one;
UIButton *two;
UIButton *three;
NSInteger *loggedNumbers;
NSMutableString *numberString;
}
-(IBAction)buttonClicked:(id)sender;
@property (nonatomic, retain)IBOutlet UIButton *one;
@property (nonatomic, retain)IBOutlet UIButton *two;
@property (nonatomic, retain)IBOutlet UIButton *three;
@property (nonatomic, retain) rootViewController *viewController;
@end
and the .m
#import "Numpad.h"
@implementation Numpad
@synthesize viewController, one, two, three, four, five, six, seven, eight, nine, zero;
- (void)initString{
}
- (IBAction)buttonClicked:(id)sender{
int stringLength = [numberString length];
//NSLog(@"Number String before Switch: %@", numberString);
switch (((UIButton*)sender).tag){
case 1:
{
[numberString insertString: @"1" atIndex: stringLength];
}
break;
case 2:
{
[numberString insertString: @"2" atIndex: stringLength];
}
break;
case 3:
{
[numberString insertString: @"3" atIndex: stringLength];
}
break;
default:
break;
}
double myDouble = [numberString doubleValue];
int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5));
NSLog(@"Number String: %@", numberString);
NSLog(@"After int applied: %i", myInt);
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
numberString = [[NSMutableString stringWithString: @""];
[super viewDidLoad];
}
The string do not retain by itself, as you are assigning value to numberString by convenience method so you should retain it yourself. So viewDidLoad should look like this.
or you should allocate it