所以我正在开发一个应用程序,并且我没有使用自动引用计数,所以我必须自己进行内存管理。我有这段代码,它在第一种方法中为 UIView 的图层设置了一个值。然后在第二种方法中,我检索它。array1 在我的viewDidLoad
这是我的第一个方法
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGFloat endLocation = [touch locationInView:self];
CGRect rect = CGRectMake(startLocation.x, startLocation.y, endLocation.x, endLocation.y);
UIView *rectstring = [[UIView alloc]initWithFrame:rect];
NSString *string = [NSString stringWithFormat:@"%d",pencilbool];
[[rectstring layer] setValue:string forKey:@"color"];
NSString *string1 = [NSString stringWithFormat:@"%f",stepper.value];
[[rectstring layer] setValue:string1 forKey:@"size234"];
[array1 addObject:rectstring];
[rectstring release];
[string release];
[string1 release];
}
第二种方法:
-(IBAction)secondmethod {
for (UIView *string1 in array1) {
CGContextRef gc=UIGraphicGetCurrentContext();
CGFloat width =[[string1.layer valueForKey:@"size234"] floatValue];
CGContextSetLineWidth (gc, width);
CGRect rect = [string1 frame];
CGContextMoveToPoint(gc, rect.origin.x, rect.origin.y);
CGContextAddLineToPoint(gc, rect.size.width, rect.size.height);
CGContextStrokePath(gc);
}
}
这会导致CGFloat width =[[string1.layer valueForKey:@"size234"] floatValue];
. 但是,如果我删除第一种方法中的发布调用:
[string release];
[string1 release];
它运行良好。
为什么这会导致错误?有任何想法吗?