NSURL getResourceValue in swift

前端 未结 5 861
梦如初夏
梦如初夏 2021-02-13 11:54

Having trouble figuring out how to make the following call in swift:

var anyError: NSError? = nil
var rsrc: NSNumber? = nil
var success = url.getResourceValue(&a         


        
5条回答
  •  一生所求
    2021-02-13 12:09

    You need to make rsrc an optional AnyObject and pass it by reference like so:

    var anyError: NSError?
    var rsrc: AnyObject?
    var success = url.getResourceValue(&rsrc, forKey:NSURLIsUbiquitousItemKey, error:&anyError)
    

    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:

    if let number = rsrc as? NSNumber {
        // use number
    }
    

提交回复
热议问题