gpt4 book ai didi

objective-c - 主函数上的 Exc_Bad_Access 无法通过类传递实例变量

转载 作者:行者123 更新时间:2023-12-03 17:35:38 24 4
gpt4 key购买 nike

更新帖子

现在,我在主设备上有 7 次 EXC_BAD_ACCESS,我不知道为什么当 PumpViewHeight 的结果为 607 时,来自 PumpCustomView 类的 heightOfPumpView 结果为 0。

PumpViewController.m

#import "PumpViewController.h"
#import "PumpModel.h"
#import "PumpCustomView.h"

@implementation PumpViewController

@synthesize labels;
@synthesize heightOfPumpView;
- (id)init
{

if (self = [super init])
{
labels = [[PumpModel alloc]init];

PumpCustomView* pumpView = [PumpCustomView alloc];
heightOfPumpView = [pumpView pumpViewHeight];
[labels pumpCreateLabel:heightOfPumpView];
labelsArray = [[NSMutableArray alloc]initWithArray:[labels labelsGroup]];

[labels release];

if (labelsArray!=nil)
{
[pumpView addSubview:[labelsArray objectAtIndex:2]];
}



[labelsArray release];
[pumpView release];
}

return self;
}


-(void) dealloc
{
[super dealloc];

}

@end

PumpModel.m

#import "PumpModel.h"
#import "PumpViewController.h"
#import "PumpCustomView.h"

@implementation PumpModel
@synthesize labelsGroup;

-(id)init
{
self = [super init];
return self;
}

-(void)pumpCreateLabel:(float)pumpViewHeight
{
theNumberOfPump = 8;
PumpViewController* pumpViewControllerAlloc = [PumpViewController alloc];
labelsGroup = [[NSMutableArray alloc]init];

for (int i = 0;i < theNumberOfPump; i++)
{
int pumpViewHeight = [pumpViewControllerAlloc heightOfPumpView];
int pumpViewWidthA = 259;

int resultHeight = pumpViewHeight/theNumberOfPump;
CGFloat resultWidth = pumpViewWidthA/2;
positionChart[i] = resultHeight * i;

newLabel[i] = [[NSTextField alloc] init] ;

[newLabel[i] setIntValue:i];

newLabel[i].frame = CGRectMake(resultWidth, positionChart[i], 300, 100);
newLabel[i].font= [NSFont fontWithName:@"Arial" size:12];
newLabel[i].textColor= [NSColor blackColor];
newLabel[i].backgroundColor= [NSColor whiteColor];

[labelsGroup addObject:newLabel[i]];
[newLabel[i] release];

NSLog(@"%@ %d",[[labelsGroup objectAtIndex:i] stringValue],positionChart[i]);
}
[pumpViewControllerAlloc release];

}

-(void) dealloc
{
[labelsGroup release];
[super dealloc];
}

最佳答案

您不应该在[super init]之前向对象发送消息,例如:

- (id)init
{
if (self = [super init])
{
[self setNumberOfPump:8];
}
return self;
}

这也适用于:

-(id)initWithNumberOfPump:(int)numberOfPump
{
if (self = [super init]) {
theNumberOfPump = numberOfPump;
[self pumpCreateLabel];
}
return self ;
}

关于objective-c - 主函数上的 Exc_Bad_Access 无法通过类传递实例变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10180934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com