Search code examples
iosswiftnsarrayswift3xcode8

Swift 3 - sending the pointer of an optionally bound variable - iOS


I am currently converting my swift files to swift 3 and there is a runtime exception that involves pointers. Here is the code:

var sectionChanges:NSArray? = nil
var rowChanges:NSArray? = nil

databaseView.getSectionChanges(&sectionChanges!, rowChanges: &rowChanges!, for: notifications, with: mappings)

fatal error: unexpectedly found nil while unwrapping an Optional value

function signature specialization ) -> () to @callee_owned (@unowned Swift.UnsafeBufferPointer) -> (@out ()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer) -> ()]> of generic specialization of Swift.StaticString.withUTF8Buffer ((Swift.UnsafeBufferPointer) -> A) -> A

EDIT: Definition of getSectionChanges():

open func getSectionChanges(_ sectionChangesPtr: AutoreleasingUnsafeMutablePointer<NSArray>?, rowChanges rowChangesPtr: AutoreleasingUnsafeMutablePointer<NSArray>?, for notifications: [NSNotification], with mappings: YapDatabaseViewMappings)

Please help?!


Solution

  • Initialising the optional variable to something other than nil seems to have helped. Answering this question myself, in case it could be useful to somebody else.

    var sectionChanges:NSArray? = NSArray()
    var rowChanges:NSArray? = NSArray()
    
    databaseView.getSectionChanges(&(sectionChanges)!, rowChanges: &(rowChanges)!, for: notifications, with: mappings)