Re: Get list of possible applications
Re: Get list of possible applications
- Subject: Re: Get list of possible applications
- From: Chris Hanson <email@hidden>
- Date: Sat, 19 Apr 2008 21:33:23 -0700
On Apr 19, 2008, at 9:55 AM, Gerd Knops wrote:
This returns an array with URLs of applicable applications for a
file of a given URL:
- (NSArray *)applicationsForURL:(NSURL *)url {
return (NSArray
*)LSCopyApplicationURLsForURL((CFURLRef)url,kLSRolesAll);
}
It also leaks -- even if you're running under Objective-C garbage
collection -- because LSCopyApplicationURLsForURL has "copy" in its
name and therefore follows the Core Foundation "copy" rule.
The proper form for the above method, if you aren't using Objective-C
garbage collection, is:
- (NSArray *)applicationsForURL:(NSURL *)url {
return [(NSArray *)LSCopyApplicationURLsForURL((CFURLRef)url,
kLSRolesAll) autorelease];
}
This ensures that the returned NSArray will be autoreleased, instead
of leaked.
If you're using -- or might use -- Objective-C garbage collection,
you'll want to replace the cast to (NSArray *) with NSMakeColectable
like this:
- (NSArray *)applicationsForURL:(NSURL *)url {
return
[NSMakeCollectable(LSCopyApplicationURLsForURL((CFURLRef)url,
kLSRolesAll)) autorelease];
}
The NSMakeCollectable ensures that the CFArrayRef returned by
LSCopyApplicationURLsForURL will have its lifetime managed by the
collector if GC is on, since -autorelease has no effect when GC is
on. This is also correct for non-GC, since NSMakeCollectable when GC
is off is effectively just a cast from (CFTypeRef) to (id).
-- Chris
_______________________________________________
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