1

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?!

Koushik Ravikumar
  • 664
  • 11
  • 26

2 Answers2

0

As the method definition implying: parameter AutoreleasingUnsafeMutablePointer? won't accept address of nil NSArray object. So you should use the following code to call the method:

var sectionChanges = NSArray()
var rowChanges = NSArray()

databaseView.getSectionChanges(&sectionChanges, rowChanges: &rowChanges, for: notifications, with: mappings)
Zhou Jason
  • 406
  • 4
  • 6
0

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)
Koushik Ravikumar
  • 664
  • 11
  • 26