How to pass an error pointer in the Swift language?

后端 未结 2 826
刺人心
刺人心 2021-02-06 20:14

I am attempting to pass an error pointer in swift and am unable to do so. The compiler complains that \"NSError is not convertible to \'NSErrorPointer\'\".

var e         


        
2条回答
  •  感动是毒
    2021-02-06 21:19

    You just pass a reference like so:

    var error: NSError?
    var results = context.executeFetchRequest(request, error: &error)
    
    if error != nil {
        println("Error executing request for entity \(entity)")
    }
    

    Two important points here:

    1. NSError? is an optional (and initialized to nil)
    2. you pass by reference using the & operator (e.g., &error)

    See: Using swift with cocoa and objective-c

提交回复
热议问题