Paul Simon Bracha
Paul Simon Bracha

Reputation: 183

Lazy init to variables in an @objc class in Swift

I am trying to use lazy initialization for instance for an Array in a class in Swift. When i am using @objc declaration for the class, in order to use it in objective-c i got a compilation error. When i just use the class without @objc, i can compile it without any issues.

I get errors for this:

@objc class MyClass {
    @lazy var arr : String[] = String[]()
}

Error generated

For the following code, i get no errors:

class MyClass {
    @lazy var arr : String[] = String[]()
}

Thank you your help!

Upvotes: 1

Views: 1081

Answers (1)

Yatheesha
Yatheesha

Reputation: 10432

In the first case I think its a bug in bridge code generator Developer forum.

If anything which is not objective-C compatible compiler won't generate it's objective-C equivalent (it won't give Error), For example if you have a Generics or tuple that are not supported in objective-C compiler is not going to generate objective-C equivalent for this also it won't give error.

In the second case you are not including @objc , From apple doc :

A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C.If your Swift class is a descendant of an Objective-C class, the compiler automatically adds the @objc attribute for you.

Hence this class is not included(not accessible) , so its not giving error.

Upvotes: 3

Related Questions