Re: Getting resource for key (Swift)
Re: Getting resource for key (Swift)
- Subject: Re: Getting resource for key (Swift)
- From: Charles Srstka <email@hidden>
- Date: Mon, 20 Jul 2015 14:37:58 -0500
> On Jul 20, 2015, at 2:19 PM, Jan E. Schotsman <email@hidden> wrote:
>
> Still making baby steps in Swift.
>
> What am I doing wrong this time?
>
> var fileObject:NSURL = ...
> var err:NSError?
> var value:Bool?
>
> var gotValue = fileObject.getResourceValue( &value, forKey: NSURLIsAliasFileKey, error: &err ); // cannot call getResourceValue with this parameter list
> if gotValue
> {
> // use value!
> }
> else { if let error = err { println("file object inspection failed: \(error.localizedDescription)") }}
>
> TIA,
>
> Jan E.
It’s definitely awkward, and this is certainly one of those times where it gets obvious that the API wasn’t designed to be used in Swift.
This is, in a nutshell, how I do it:
let url = ...
do {
var isAliasAny: AnyObject? = nil
try url.getResourceValue(&isAliasAny, forKey: NSURLIsAliasFileKey)
if let isAlias = isAliasAny as? NSNumber {
print("is alias: \(isAlias.boolValue)")
} else {
throw NSCocoaError.FileReadUnknownError
}
} catch {
print("file object inspection failed: \(error)")
}
Charles
_______________________________________________
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