Having trouble figuring out how to make the following call in swift:
var anyError: NSError? = nil
var rsrc: NSNumber? = nil
var success = url.getResourceValue(&rsrc, forKey:NSURLIsUbiquitousItemKey, error:&anyError)
The above does not compile:
Cannot convert the expression's type 'Bool' to type 'inout Bool'
So I tried this:
var anyError: NSError? = nil
var rsrc: AutoreleasingUnsafePointer<AnyObject?> = nil
var success = url.getResourceValue(rsrc, forKey:NSURLIsUbiquitousItemKey, error:&anyError)
but this generates EXC_BAD_ACCESS
runtime error.
How do I pass in the expected first arg as AutoreleasingUnsafePointer<AnyObject?>
(which should point to a boolean NSNumber according to doc), and then be able to check its expected Bool value ?
Here's a concrete example as an extension of the URL type:
For convenience, if the call to
getResourceValue()
fails, it returns the value.distantPast
by default. Using thetry?
form allows discarding the error which isn't always needed.If the getResourceValue(:forKey:) result is ultimately a logical value, you can cast the returned pointer value directly to a Bool value:
or simply test it as a Bool value without assigning it:
or do both:
You need to make
rsrc
an optional AnyObject and pass it by reference like so:Note: You do not need to initialize Optionals to nil, they are set to nil by default.
If you then want to check if the value is an NSNumber you can then do a conversion:
in swift 3 previous calls are not available/deprecated:
so use:
Here is drewag's code updated for Swift 2.0