将临时对象添加到 NSMutableArray

发布于 2025-01-07 02:44:30 字数 918 浏览 5 评论 0原文

我有一个自定义类 MDRect,我试图添加一个 NSMutableArray

数组是一个属性:

@property (retain) NSMutableArray* array; 

它在 NSView 子类的 initMethod 中初始化:

-(id)init {
array =  [NSMutableArray new];
return [super init];
 }

然后我尝试在此处的数组中添加一个对象:

-(void)mouseUp:(NSEvent *)theEvent
{
NSPoint mouseLoc = [NSEvent mouseLocation];    
mouseLoc = [self mouse:mouseLoc inRect:[self frame]];

CGSize temp;
NSLog(@"%f",mouseLoc.y - mouseLocation.y);
NSLog(@"%f",mouseLoc.x - mouseLocation.x);
temp.height = mouseLoc.y - mouseLocation.y;
temp.width = mouseLoc.x - mouseLocation.x;

tempRect.size = temp;
MDRect * rect = [[MDRect alloc] initWithColor:[NSColor orangeColor] andRect:tempRect];
[array addObject:rect];    




int i =     (int)array.count;
NSLog(@"%i",i);
[self setNeedsDisplay:YES];
}

但该对象不是被添加到数组中。它在 NSLog 函数中从不返回除 0 之外的任何值。我做错了什么?

I have a custom class MDRect that i am trying to add an NSMutableArray

the array is a property:

@property (retain) NSMutableArray* array; 

it is initialized in the initMethod of the NSView subclass:

-(id)init {
array =  [NSMutableArray new];
return [super init];
 }

then i am trying to add an object in the array here:

-(void)mouseUp:(NSEvent *)theEvent
{
NSPoint mouseLoc = [NSEvent mouseLocation];    
mouseLoc = [self mouse:mouseLoc inRect:[self frame]];

CGSize temp;
NSLog(@"%f",mouseLoc.y - mouseLocation.y);
NSLog(@"%f",mouseLoc.x - mouseLocation.x);
temp.height = mouseLoc.y - mouseLocation.y;
temp.width = mouseLoc.x - mouseLocation.x;

tempRect.size = temp;
MDRect * rect = [[MDRect alloc] initWithColor:[NSColor orangeColor] andRect:tempRect];
[array addObject:rect];    




int i =     (int)array.count;
NSLog(@"%i",i);
[self setNeedsDisplay:YES];
}

But the object being is not being added to the array. it never returns any value other than 0 in the NSLog function. What am I doing wrong?

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

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

发布评论

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

评论(1

心安伴我暖 2025-01-14 02:44:30

你的问题是你的 init 方法。你完全错了,它应该看起来像这样:

- (id)init {
    if (self = [super init]) {
        array = [NSMutableArray new];
    }
    return self;
}

你的问题是你没有调用 super 的 init 并将其分配给 self然后设置数组。您在获得正确的对象之前就分配了 array ,然后您甚至没有返回 array 已设置的任何内容,而是返回了以下结果超类的 init 方法。然后,当您记录 array.count 时,arraynil,因此 i 变为 0< /code> (因为在这种情况下消息 nil 返回 0)。

Your problem is your init method. You've got it quite wrong and it should look like this:

- (id)init {
    if (self = [super init]) {
        array = [NSMutableArray new];
    }
    return self;
}

Your problem is that you're not calling super's init and assigning that to self and then setting up array. You're assigning array before you've even got a proper object and then you're not even returning anything that array has been set on, but rather returning the result of the super-class's init method. Then when you go to log array.count, array is nil and hence i becomes 0 (because messaging nil returns 0 in this circumstance).

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