目标C:内存管理,释放具有多个引用的对象

时间:2021-10-02 20:11:11

I would like to release a Car object present in the dealer. I would like to know what is the right way of doing it. NSMutableArray Inventory stores the cars for a particular dealer. So, now I would like to present the user with a delete functionality, so, user could select the car using the Vin Number and delete it. But if I try to find the car and release the reference that doesn't works. Would I need to remove the object from the Array and then, release the reference? I am fairly new to objective c and in the initial learning phase. Thank you.

我想发布经销商中的Car对象。我想知道这样做的正确方法是什么。 NSMutableArray Inventory为特定经销商存储汽车。所以,现在我想向用户呈现删除功能,因此,用户可以使用Vin编号选择汽车并将其删除。但是,如果我试图找到汽车并发布不起作用的参考。我是否需要从Array中删除该对象然后释放引用?我对目标c和初始学习阶段都很新。谢谢。

#import "Dealer.h"
#import "Address.h"
#import "PhoneNumber.h"

@implementation Dealer

static NSInteger dealerIdAllocater = 0;

-(id) init{
    self = [super init];
    self.dealerId = ++dealerIdAllocater;
    self.addressList = [[NSMutableArray alloc] init];
    self.inventory = [[NSMutableArray alloc] init];
    return self;
}

@synthesize dealerId, name, addressList, inventory;

-(void)addCarInInventory:(Car*)car{
    [self.inventory addObject: car];
}

-(void)displayAddresses{
    for(Address *address in self.addressList){
        NSLog(@"Street Address: %@", address.streetAddress);
        NSLog(@"City: %@", address.city);
        NSLog(@"State: %@", address.state);
        NSLog(@"Country: %@", address.country);
        for(int i=0; i<[address.phoneNumber count]; i++){
            PhoneNumber *phoneNumber = [address.phoneNumber objectAtIndex:i];
            NSLog(@"Phone Number %i, %@", i, phoneNumber.phoneNumber);
        }
        NSLog(@"--------------");
    }
}

-(void)displayInventory{
    for(Car *car in self.inventory){
        [car displayInformation];
    }
    NSLog(@"--------------");
}

-(Car *)findCarById:(NSString *)vinNumber{
    for(Car *car in self.inventory){
        if ([vinNumber isEqualToString:car.vinNumber]) {
            return car;
        }
    }
    return nil;
}

@end

1 个解决方案

#1


1  

Would I need to remove the object from the Array and then, release the reference?

我是否需要从Array中删除该对象然后释放引用?

Yes, containers such as NSMutableArrays increment the retain count of objects by 1 when added to them. This is to make sure the container will always hold a valid reference to an object. When you remove an object from the container, the retain count will be decremented by 1.

是的,NSMutableArrays等容器在添加到对象时会将对象的保留计数增加1。这是为了确保容器始终保持对对象的有效引用。从容器中删除对象时,保留计数将减1。

#1


1  

Would I need to remove the object from the Array and then, release the reference?

我是否需要从Array中删除该对象然后释放引用?

Yes, containers such as NSMutableArrays increment the retain count of objects by 1 when added to them. This is to make sure the container will always hold a valid reference to an object. When you remove an object from the container, the retain count will be decremented by 1.

是的,NSMutableArrays等容器在添加到对象时会将对象的保留计数增加1。这是为了确保容器始终保持对对象的有效引用。从容器中删除对象时,保留计数将减1。