4

In Swift 2.2, I was able to pass in nil as a valid parameter to a function which requires an UnsafePointer<UInt8>. In Swift 3, I can no longer do that:

func myFuncThatTakesAPointer(buffer: UnsafePointer<UInt8>, length: Int) { /** **/ }

myFuncThatTakesAPointer(buffer: nil, length: 0)
Playground execution failed: error: Xcode8Playground-iOS.playground:62:33: error: nil is not compatible with expected argument type 'UnsafePointer<UInt8>'
myFuncThatTakesAPointer(buffer: nil, length: 0)
                                ^

Am I required to specify the pointer declaration in my function as optional now?

matt
  • 515,959
  • 87
  • 875
  • 1,141
JAL
  • 41,701
  • 23
  • 172
  • 300
  • 4
    Please check [this](https://github.com/apple/swift-evolution/blob/master/proposals/0055-optional-unsafe-pointers.md). – OOPer Oct 11 '16 at 14:32

1 Answers1

6

Am I required to specify the pointer declaration in my function as optional now?

In a word, yes. From the release notes:

The types UnsafePointer, UnsafeMutablePointer, AutoreleasingUnsafeMutablePointer, OpaquePointer, Selector, and NSZone now represent non-nullable pointers—that is, pointers that are never nil. A nullable pointer is now represented using Optional, for example, UnsafePointer<Int>?.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks matt, congrats on 200k! – JAL Oct 11 '16 at 14:35
  • Thanks, but you don't have to put me over the top if you don't want to, I'll get there. :) I realize that my answer is kind of annoying. :))) – matt Oct 11 '16 at 14:36
  • It's all good, I clearly need to spend more time reading the language proposals. – JAL Oct 11 '16 at 14:37
  • 1
    Actually the release notes doc I pointed you to is better. The language proposals can be really confusing; just because they accepted something does not mean it is implemented in a public release, or that they implemented it the way the the proposal says. – matt Oct 11 '16 at 14:38
  • 1
    @matt, better check both docs, 'cos proposal contains motivation for change. – user28434'mstep Oct 11 '16 at 14:57