Re: ARC Extending Lifetime of Objects
Re: ARC Extending Lifetime of Objects
- Subject: Re: ARC Extending Lifetime of Objects
- From: James Montgomerie <email@hidden>
- Date: Mon, 23 Jul 2012 17:01:21 +0100
I think this does what you want. It works by [arguably ab-] using the CF bridging casts:
NSUInteger count = [dictionary count];
// Are you sure you don't want to malloc and free these? Might blow the
// stack if they're large.
id __unsafe_unretained values[count];
id __unsafe_unretained keys[count];
[dictionary getObjects:values andKeys:keys];
for (NSUInteger i = 0; i < count; ++i) {
keys[i] = (__bridge id)CFBridgingRetain([keys[i] copy]);
values[i] = (__bridge id)CFBridgingRetain([values[i] copy]);
}
NSDictionary *other = [NSDictionary dictionaryWithObjects:values
forKeys:keys
count:count];
for (NSUInteger i = 0; i < count; ++i) {
CFBridgingRelease((__bridge CFTypeRef)keys[i]);
CFBridgingRelease((__bridge CFTypeRef)values[i]);
}
I second the thoughts already expressed here though that doing:
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject: dictionary]]
would be more readable and more maintainable (and less likely to be wrong), so I'd say do that if performance does not prove to be an issue.
Jamie.
On 23 Jul 2012, at 09:00, Andreas Grosam wrote:
> Suppose, I have C-arrays defined as follows:
>
> int count = [dictionary count];
> id __unsafe_unretained values[count];
> id __unsafe_unretained keys[count];
>
>
> I fill these arrays with:
>
> [dictionary getObjects:values andKeys:keys];
>
>
>
> Now, I would like to "reuse" these arrays through holding objects maintained by ARC (if possible):
>
> for (int i = 0; i < count; ++i) {
> keys[i] = [keys[i] copy];
> values[i] = [values[i]] copy];
> }
> NSDictionary *other = [NSDictionary dictionaryWithObjects:values forKeys:keys count:count];
>
>
>
> This won't work in ARC, since the arrays are declared __unsafe_unretained and this would cause the newly created object immediately be deallocated once it is assigned to the array's element.
>
> What I would like to avoid is to use separate arrays (appropriately declared), and also avoid to disable ARC and maintain the release counts manually.
>
> Is there a way to extend the lifetime of the created objects, or is there a way to "re-declare" the arrays so that ARC can maintain the references - or is there any other simple "trick"?
>
>
> Thanks for suggestions! ;)
>
> Andreas
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden