代码之家  ›  专栏  ›  技术社区  ›  fuzzygoat

通过Setter排列?

  •  0
  • fuzzygoat  · 技术社区  · 16 年前

    // INTERFACE
    @interface CarBody : NSObject {
     NSMutableArray *tires;
    }
    // Should this be (id *) it works but I was convinced it would be pointer?
    - (void) addTire:(id)newTire;
    @end
    
    @interface TireSnow : NSObject {
    }
    @end
    
    // IMPLEMENTATION
    @implementation CarBody
    - (void) addTire:(id)newTire {
     [tires addObject:newTire];
        // ** Release here or in main()?
    
    }
    - (id) init {
     [super init];
     tires = [[NSMutableArray alloc] init];
     NSLog(@"_init: %@", NSStringFromClass([self class]));
     return self;
    }
    - (void) dealloc {
     NSLog(@"_deal: %@", NSStringFromClass([self class]));
     [tires release];
     [super dealloc];
    }
    @end
    

    1. 在addTire方法中,(id)是否正确,我以为它会是(id*)
    2. 释放我添加到数组中的项,我应该在调用它后在setter或main()中执行吗?

    加里

    编辑:

    // MAIN
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        CarBody *newCarBody_001;
        TireSnow *newSnowTire_001;
    
        newCarBody_001 = [[CarBody alloc] init];
        newSnowTire_001 = [[TireSnow alloc] init];
    
        [newCarBody_001 addTire:newSnowTire_001];
    
        // Clean up
        [newCarBody_001 release];
        [newSnowTire_001 release];
        [pool drain];
        return 0;
    }
    

    EDIT_002:

    刚刚添加了代码来生成所有4个轮胎,并在调用设置器后将轮胎释放器移入循环。

    // CREATE TIRES
    for(int loopCounter=0; loopCounter<4; loopCounter++) {
        newSnowTire_001 = [[TireSnow alloc] init];
        [newCarBody_001 addTire:newSnowTire_001];
        [newSnowTire_001 release];
    }
    

    我刚刚检查了这个,它是正确的。..

    1. 新雪地轮胎_001(添加轮胎)保留计数=2
    2. NewSnowTire_001最终通过dealloc方法发布。
    3 回复  |  直到 16 年前
        1
  •  2
  •   Georg Schölly Crazy Developer    16 年前
    1. (id) (TireSnow*) id 已经是一个指针,所以你不需要 *

    2. main

    3. 我觉得还可以。

    4. 你可以使用 [[NSMutableArray alloc] initWithCapacity:4] 。这只是对数组的提示,如果插入更多项,它将自动展开。检查 [tires length] addTire


    -init

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

    nil

        2
  •  1
  •   PeyloW    16 年前
    1. 你应该使用 id id* ) java.lang.Object NSObject NSProxy . 不幸的是 ,以及 Class '*'

    2. addTire: 方法就是一个非常好的例子,永远不要释放作为参数交给你的对象。因此,只释放交给您的对象 (即便如此,只有从 alloc , new copy 方法) .

    3. tires init 检查超级类结果,就像这样 (永远不要相信super总是工作,甚至返回相同的实例)

    - (id) init {
        self = [super init];
        if (self) {
          tires = [[NSMutableArray alloc] init];
          NSLog(@"_init: %@", NSStringFromClass([self class]));
        }
        return self;
      }
    
    1. 你可以使用 NSArray 如果你从一开始就可以使用所有四个轮胎。最好的办法可能是要求轮胎在 方法。如果这不可能,那么使用 不可变数组 NSMutableArray .
        3
  •  0
  •   Chuck    16 年前

    id 定义如下(在objc.h中):

    typedef struct objc_object {
        Class isa;
    } *id;
    

    所以 id* 将是指向指针的指针。

    这在 the Objective-C memory management rules