当函数使用 getter 时 Objective C 中的 EXC_BAD_EXCESS

发布于 2025-01-04 12:26:28 字数 872 浏览 2 评论 0原文

- (NSDictionary *)myPropertyList   //Calling getter of myPropertyList
{
    if(!_myPropertyList){
        NSArray *colorsUsed = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor blueColor], nil];
        _myPropertyList=[[NSDictionary alloc] initWithObjectsAndKeys: colorsUsed, @"colorUsed",[NSNumber numberWithDouble:MAX_RADIUS],@"maximumRadius",[NSNumber numberWithDouble:MIN_RADIUS],@"minimumRadius", [NSNumber numberWithInt:20],@"circleNumbers", nil];;
    }
    return _myPropertyList;
}

- (NSArray *)generateCircleList
{
    int minRadius=[[self.myPropertyList objectForKey:@"minimumRadius"] intValue];
    int maxRadius=[[self.myPropertyList objectForKey:@"maximumRadius"] intValue];
    ...
    //code continues

我的模型中有这段代码。当调用使用 myPropertyList 的函数generateCircleLlist 时,它会因 EXC_BAD_ACCESS 错误而崩溃。我确实环顾四周,但找不到解决方案。有什么帮助吗?

- (NSDictionary *)myPropertyList   //Calling getter of myPropertyList
{
    if(!_myPropertyList){
        NSArray *colorsUsed = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor blueColor], nil];
        _myPropertyList=[[NSDictionary alloc] initWithObjectsAndKeys: colorsUsed, @"colorUsed",[NSNumber numberWithDouble:MAX_RADIUS],@"maximumRadius",[NSNumber numberWithDouble:MIN_RADIUS],@"minimumRadius", [NSNumber numberWithInt:20],@"circleNumbers", nil];;
    }
    return _myPropertyList;
}

- (NSArray *)generateCircleList
{
    int minRadius=[[self.myPropertyList objectForKey:@"minimumRadius"] intValue];
    int maxRadius=[[self.myPropertyList objectForKey:@"maximumRadius"] intValue];
    ...
    //code continues

I have this piece of code in my model. When the function generateCircleLlist is called that uses the myPropertyList, it is crashing with a EXC_BAD_ACCESS error. I really looked around but couldn't find a solution to this. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2025-01-11 12:26:28

您正在创建 colorUsed 但使用 colorUsed,还要添加 [colorsUsed release];用于内存管理。

if(!_myPropertyList){

    NSArray *colorsUsed = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor blueColor], nil];
    _myPropertyList=[[NSDictionary alloc] initWithObjectsAndKeys: colorsUsed, @"colorUsed",[NSNumber numberWithDouble:MAX_RADIUS],@"maximumRadius",[NSNumber numberWithDouble:MIN_RADIUS],@"minimumRadius", [NSNumber numberWithInt:20],@"circleNumbers", nil];

    [colorsUsed release];
}

You are creating colorsUsed but using colorUsed, also add [colorsUsed release]; for memory management.

if(!_myPropertyList){

    NSArray *colorsUsed = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor blueColor], nil];
    _myPropertyList=[[NSDictionary alloc] initWithObjectsAndKeys: colorsUsed, @"colorUsed",[NSNumber numberWithDouble:MAX_RADIUS],@"maximumRadius",[NSNumber numberWithDouble:MIN_RADIUS],@"minimumRadius", [NSNumber numberWithInt:20],@"circleNumbers", nil];

    [colorsUsed release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文