D. Spears
D. Spears

Reputation: 31

First @objc after public causes an error in Swift 4

I ran into this during my Swift 4 conversion and have a simple test case that I am not sure if it is my error or a bug in Swift 4. If I have an @objc identifier as the first property after "public", I get a compile error

import Foundation

class ErrorClass: NSObject
{
    public

    @objc let myVal = 0

    let myOtherVal = 1


}

This will result in an "Expected declaration" error at the @objc identifier. However, if you reverse the declaration of the two properties like this, everything works fine.

import Foundation

class ErrorClass: NSObject
{
    public

    let myOtherVal = 1

    @objc let myVal = 0



}

I may be misunderstanding the scope of public. In swift, does it only apply to the declaration after the identifier, or does it scope everything after it like C++? If the former, that would explain my issue as the newline after public would be ignored in the second case making just myOtherVal be affected by the public identifier.

Upvotes: 0

Views: 72

Answers (1)

Connor Neville
Connor Neville

Reputation: 7361

It has nothing to do with being the first variable in the class. The @objc just needs to precede the access control (public). I'm not sure why you have public on the previous line; that seems confusing and I haven't seen that before.

class MyClass {
    @objc public let myVar = 0 // compiles
}

Upvotes: 1

Related Questions