简体   繁体   中英

IOS - Create new object instances through a loop with ARC

Basically, my problem is that I'm trying to create 3 instances of a UIView through a loop. I'm using ARC and I don't really know if what I want to do is possible with it. Here's the code I currently have :

NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"RoomView"
                                                  owner:self
                                                options:nil];
NSMutableArray *roomViews = [[NSMutableArray alloc] initWithCapacity:[gtb.rooms count]];

for (i = 0; i < [gtb.rooms count]; i++)
{
    RoomView *rcv = [[RoomView alloc] init];
    NSDictionary *room = [gtb.rooms objectAtIndex:i];
    rcv = [nibViews objectAtIndex:0];
    NSLog(@"Start rcv = %@", rcv);
    rcv.roomNumber.text = [NSString stringWithFormat:@"Chambre %d", i + 1];
    rcv.roomType.text = [room objectForKey:@"roomType"];
    [rcv setFrame:CGRectMake(0, sizeOfContent, rcv.frame.size.width, rcv.frame.size.height)];
    sizeOfContent += rcv.frame.size.height;
    [roomViews addObject:rcv];
    NSLog(@"End rcv = %@", rcv);
}

for (i = 0; i < [gtb.rooms count]; i++)
    NSLog(@"Room #%i : %@", i, [roomViews objectAtIndex:i]);

And here's what I have in the logs :

2012-09-20 10:15:00.287 AppName[2792:707] Start rcv = <RoomView: 0x148570; frame = (0 0; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.289 AppName[2792:707] End rcv = <RoomView: 0x148570; frame = (0 420; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.312 AppName[2792:707] Start rcv = <RoomView: 0x148570; frame = (0 420; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.314 AppName[2792:707] End rcv = <RoomView: 0x148570; frame = (0 527; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.316 AppName[2792:707] Start rcv = <RoomView: 0x148570; frame = (0 527; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.317 AppName[2792:707] End rcv = <RoomView: 0x148570; frame = (0 634; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.319 AppName[2792:707] Room #0 : <RoomView: 0x148570; frame = (0 634; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.323 AppName[2792:707] Room #1 : <RoomView: 0x148570; frame = (0 634; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.325 AppName[2792:707] Room #2 : <RoomView: 0x148570; frame = (0 634; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>

I know if I wasn't using ARC, I should have placed something like [rcv autoRelease] at the end of the loop, but with ARC, I can't.

Is there any solution to solve it, or must I disable ARC for this file ?

Thanks for your help !

With ARC, autorelease is being called for you. You don't need to worry about it.

You need to replace this line:

rcv = [nibViews objectAtIndex:0];

With this one:

rcv = [[nibViews objectAtIndex:0] copy];

Because if not, you just are accessing the same view object, not a new one copied from it (strong reference not copied).

And as a side not, there is no influence of ARC in that matter. You would have same problem without ARC. With ARC you are just not writing release/autorelease etc.

By the rule of memory management, you need to release only the objects you alloc-init or copy. Here you are not doing both with RoomView *rcv. So even if your code is not ARC enabled you need not release or autorelease it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM