This is my first time using this site and I am quite new to Objective-c. I’m sure this is a simple question but for some reason I am having a lot of issues. The app is designed to have the user enter a string via textfield, then it will pick the rest of the sentence and display it. The issue appears to be that my *name will be retained after the keyboard method and work once in the changelabel method. Then if i press the button again, invoking the changelabel method, the name appears to have been released and crashes the app.
#import
#import “Array.h”
@interface RandomBoredViewController : UIViewController {
UILabel *label;
UIButton *button;
UITextField *textField;
Array *array;
NSString *name;
NSString *description;
NSMutableString *whole;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) IBOutlet UIButton *button;
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) Array *array;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSMutableString *whole;
-(IBAction) keyBoard;
-(IBAction) changeLabel;
@end
and my .m
#import "RandomBoredViewController.h"
@implementation RandomBoredViewController
@synthesize label;
@synthesize checker;
@synthesize button;
@synthesize textField;
@synthesize array;
@synthesize name;
@synthesize description;
@synthesize whole;
-(IBAction) changeLabel {
NSLog(@"Button being pushed");
description = [array getString];
NSLog(@"%@",description);
NSLog(@"%@",name);
name = [NSString stringWithString:name];
whole = [NSMutableString stringWithString:name];
NSLog(@"%@",name);
NSLog(@"%@",whole);
[whole appendString:description];
NSLog(@"%@",name);
NSLog(@"%@",whole);
label.text = whole;
NSLog(@"%@",name);
}
-(IBAction) keyBoard {
name = [NSString stringWithString:textField.text];
NSLog(@"%@",name);
label.text = [NSString stringWithString: name];
[textField resignFirstResponder];
}
- (void)viewDidLoad {
[super viewDidLoad];
array = [[Array alloc]init];
[array createArray];
NSLog(@"%i",[array arrayCount]);
whole = [[NSMutableString alloc]init];
name = [[NSString alloc]init];
}
- (void)dealloc {
[super dealloc];
[label release];
[button release];
[textField release];
[array release];
//[name release];
[description release];
}
@end
Taking one thing in microcosm, the code you’ve posted creates two things named
name— an instance variable and a property.Instance variables are directly accessed storage. They have no behaviour.
Properties are named attributes accessed via getters and setters. So they may have arbitrary behaviour. They may report the values of instance variables or values calculated from instance variables or values calculated or obtained by any other means. Relevantly, the setters may
retain,assignor act in any other way.Instance variables may be accessed only by the instance of a class they belong to. Properties are usually intended to be accessed by anyone.
Since retaining is a behaviour and you’ve ascribed it to your
nameproperty, setting something to it would result in aretain. Instance variables can’t have behaviour, so setting a value to it doesn’t result in aretainor anything else.As a result, this line:
Creates a new string and returns a non-owning reference. Which means it’ll definitely last for the duration of this autorelease pool (ie, you explicitly may pass it as an argument or return it safely, assuming you haven’t taken manual control of your autorelease pools).
You store that reference to your instance variable. Instance variables don’t have behaviour so the reference is stored but you still don’t own that object. It’s still only safe to use for the duration of that autorelease pool.
So when you access it in that method it’s safe. When you access it later it’s unsafe.
If instead you’d gone with:
Then you’d have set that string to be the new value of the property. Because your property has the
retainbehaviour, you’d subsequently have completely safe access to the string object, until you say otherwise.Because you’ve got a property with exactly the same name as an instance variable, you could subsequently access it either as just
nameor asself.name. Similarly you could have stored directly to the instance variable rather than via the property if you’d ensured you had an owning reference manually.As suggested above, use of ARC is a way to get the compiler to figure all this stuff out for you.
That issue is what causes your code to crash — you end up trying to access a reference that has ceased to be valid. If you’d taken ownership of it then it would have continued to exist at least as long as you kept ownership.