Swift wrong compile error: default label can only appear in switch statement

1.2k Views Asked by At

NSFileManager has been renamed to FileManager in Swift 2.3 onwards. So do the defaultManager class method which now just default However when I use.

FileManager.default()

Compiler mis-icntepret and throw error.

default label can only appear in switch statement

Swift 2.3 and Xcode 8 beta 3

2

There are 2 best solutions below

0
On

This problem is fixed in Swift 3, and the backticks are no longer necessary. Note that the default manager is now a class property rather than a class method. So the correct syntax is:

 FileManager.default
1
On

I don't know about Swift 2.3, but I think the syntax for escaping a keyword hasn't changed. After all, it's not a major update.

You can use the ` character to escape a keyword so as to use the keyword as an identifier for a variable, method or something else. So you can do this do create a variable named var:

var `var` = 0

So if you just write:

FileManager.`default`()

it should work.

btw I just looked up NSFileManager in the docs. And I found that there is no default() method. Is the docs outdated or something?

enter image description here