我读到,如果一个对象是使用init创建的,那么它必须被释放,但是如果它类似于这个elementFormula = [[NSMutableString stringWithString:@""],它是自动释放的。然而,内存管理的其他一些方面让我感到困惑。
例如,像[[NSMutableString stringWithString:@""] retain]这样的东西--如果我保留了一个自动释放的对象--我必须自己释放它,还是它仍然是自动释放的?另外,如果我声明一个指向.h中的对象的指针,并在.m中为它赋值,就像这样的completeFormula = [self synthesizeFormula];,而不经过init过程,那么我必须释放completeFormula吗?
这些都是我关心的主要问题,但如果需要的话,我已经在下面发布了整个代码。
@interface EquationTextField : UIView <KeyInput> {
FormulaKeyboard *keyboard;
int consecutiveElementCount;
int subscriptLength;
int chargeIndex;
NSMutableString *elementFormula;
NSString *lastElementPressed;
NSString *charge;
NSString *state;
NSString *completeFormula;
}
init {
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"FormulaKeyboard" owner:self options:nil];
for (id object in bundle) {
if ([object isKindOfClass:[FormulaKeyboard class]])
keyboard = (FormulaKeyboard *)object;
}
self.inputView = keyboard;
keyboard.delegate = self;
elementFormula = [[NSMutableString stringWithString:@""] retain];
charge = [[NSString alloc] initWithString:@""];
state = [[NSString alloc] initWithString:@""];
}
- (void)addElement:(NSString *)currentElement {
if (![currentElement isEqualToString:lastElementPressed]) {
// when new element is pressed
[elementFormula appendString:currentElement];
lastElementPressed = currentElement;
consecutiveElementCount = 1;
}
else if ([currentElement isEqualToString:lastElementPressed]) {
// when element is pressed consecutively
if (consecutiveElementCount == 1) {
// since there is no '1' subscript, nothing needs to be deleted
[elementFormula appendString:@"₂"];
subscriptLength = 1;
}
if (consecutiveElementCount > 1) {
// deletes last subscript and replaces it with new subscript
NSArray *subscriptList = [[[NSArray alloc] initWithObjects:@"₂", @"₃", @"₄", @"₅", @"₆", @"₇", @"₈", @"₉", @"₁₀", @"₁₁", @"₁₂", nil] autorelease];
NSRange range = NSMakeRange (([elementFormula length] - subscriptLength), subscriptLength);
[elementFormula deleteCharactersInRange:range];
NSString *tempSubscript = [subscriptList objectAtIndex:consecutiveElementCount - 1];
[elementFormula appendString:tempSubscript];
subscriptLength = [tempSubscript length];
}
if (consecutiveElementCount < 11)
consecutiveElementCount ++;
}
completeFormula = [self synthesizeFormula];
}
- (void)changeCharge:(NSString *)chargeIncrease {
NSArray *chargeList = [[[NSArray alloc] initWithObjects:@"⁴⁻", @"³⁻", @"²⁻", @"⁻", @"", @"⁺", @"²⁺", @"³⁺", @"⁴⁺", nil] autorelease];
if ([chargeIncrease isEqualToString:@"+"]) {
chargeIndex += 1;
charge = [chargeList objectAtIndex:chargeIndex];
}
else if ([chargeIncrease isEqualToString:@"-"]) {
chargeIndex -= 1;
charge = [chargeList objectAtIndex:chargeIndex];
}
completeFormula = [self synthesizeFormula];
}
- (void) changeState:(NSString *)stateName {
state = stateName;
completeFormula = [self synthesizeFormula];
}
- (NSString *)synthesizeFormula {
NSMutableString *synthesizedFormula = [[[NSMutableString alloc] initWithString:elementFormula] autorelease];
[synthesizedFormula appendString:charge];
[synthesizedFormula appendString:state];
return synthesizedFormula;
}
- (void)dealloc
{
[charge release];
[state release];
self.inputView = nil;
keyboard.delegate = nil;
[keyboard release];
[super dealloc];
}发布于 2012-01-04 16:04:43
它并不是导致你不得不负责释放内存的原因。如果您调用alloc、retain或new (这在最近很少使用),那么您将负责自己释放内存。
因此,在有一个自动释放对象并保留它的情况下。然后,是的,然后你将负责随后释放它。
最常见的影响是,您可以负责释放一个没有显式调用alloc或retain的对象,这是一个具有retain属性的属性。
@property (retain) NSString *string;
如果您有以下代码的话
self.string = [NSString stringWithString:@"This is an autoreleased string typically"];调用self.string片段将使其保留您要设置的值。因此,您需要确保,并有以下的清理,可能在您的dealloc。
self.string = nil;https://codereview.stackexchange.com/questions/7437
复制相似问题