I have a swift class defined like this:
#objcMembers
public class MyURL: NSObject {
func concat(_ components: String...) -> MyURL {
concat(components)
}
/// Concat the specified components one by one.
func concat(_ components: [String]) -> MyURL {
components.forEach { let _ = value?.appendingPathComponent($0) }
return self
}
// All the other methods which are available for objc.
}
There are many methods inside which are available for objective-c, so I use the #objcMembers for class prefix directly, then swift compiler starts to complaint this:
Method 'concat' with Objective-C selector 'concat:' conflicts with previous declaration with the same Objective-C selector
They are exactly have the same function signature, but the latter one is only available for swift even if exposed. Now I'm looking for some compile flags to mark the latter concat method as swift only to tell the compiler to ignore the conflict error.
#objc and #objcMembers do that explicitly, so how to do it reverse?
https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#nonobjc
“nonobjc
Apply this attribute to a method, property, subscript, or initializer declaration to suppress an implicit objc attribute. The nonobjc attribute tells the compiler to make the declaration unavailable in Objective-C code, even though it’s possible to represent it in Objective-C.
Applying this attribute to an extension has the same effect as applying it to every member of that extension that isn’t explicitly marked with the objc attribute.
You use the nonobjc attribute to resolve circularity for bridging methods in a class marked with the objc attribute, and to allow overloading of methods and initializers in a class marked with the objc attribute.
A method marked with the nonobjc attribute can’t override a method marked with the objc attribute. However, a method marked with the objc attribute can override a method marked with the nonobjc attribute. Similarly, a method marked with the nonobjc attribute can’t satisfy a protocol requirement for a method marked with the objc attribute.”
Related
When I declare static parameter in extension of class then I have to write #nonobjc before variable like:
#nonobjc static let test = "test"
and sometimes I have to write #objc before method, so what is use of #objc and #nonobjc in Swift.
Can anyone help me for this problem?
This is explained in the Apple's official documentation about Objective-C - Swift interoperability:
When you use the #objc(name) attribute on a Swift class, the class is
made available in Objective-C without any namespacing. As a result,
this attribute can also be useful when migrating an archivable
Objective-C class to Swift. Because archived objects store the name of
their class in the archive, you should use the #objc(name) attribute
to specify the same name as your Objective-C class so that older
archives can be unarchived by your new Swift class.
Conversely, Swift also provides the #nonobjc attribute, which makes a
Swift declaration unavailable in Objective-C. You can use it to
resolve circularity for bridging methods and to allow overloading of
methods for classes imported by Objective-C. If an Objective-C method
is overridden by a Swift method that cannot be represented in
Objective-C, such as by specifying a parameter to be a variable, that
method must be marked #nonobjc.
To summarize, use #objc when you want to expose a Swift attribute to Objective-C without a namespace . Use #nonobjc if you want to keep the attribute available and accessible only in Swift code.
(Addendum/additional official details to #bontoJR well summarizing answer)
From the Swift Language Reference - Attributes [emphasis mine]:
objc
Apply this attribute to any declaration that can be represented in
Objective-C — for example, non-nested classes, protocols, nongeneric
enumerations (constrained to integer raw-value types), properties and
methods (including getters and setters) of classes and protocols,
initializers, deinitializers, and subscripts. The objc attribute tells
the compiler that a declaration is available to use in Objective-C
code.
...
nonobjc
Apply this attribute to a method, property, subscript, or initializer
declaration to suppress an implicit objc attribute. The nonobjc
attribute tells the compiler to make the declaration unavailable in
Objective-C code, even though it is possible to represent it in
Objective-C.
...
Here you can find more details in this Swift Documentation : InteractingWithObjective-C
As an answer of your question, overview from attached link is as below.
#objc : You can use attribute to change the name of a class, property, method, enumeration type, or enumeration case declaration in
your interface as it’s exposed to Objective-C code.
Example : if the name of your Swift class contains a character that isn’t supported by Objective-C, you can provide an alternative name to use in Objective-C.
#nonobjc : It makes a swift declaration unavailable in Objective-C. You can use it to resolve circularity for bridging
methods and to allow overloading of methods for classes imported by
Objective-C.
Swift function defined in MySwift.swift File:
func SomeSwift()
{
}
SomeSwift() is not defined in any Swift class, it is just a pure function.
After CMD + B to build the project, open Project-Swift.h, the SomeSwift() isn't show in there.
Does the function in Swift have to be defined in some Swift class? and with #objc marked?
like the following:
#objc class SomeSwift: NSObject {
func SomeSwift()
{
}
}
Referring to Apple Documentation about Using Swift from Objective-C:
A Swift class must be a descendant of an Objective-C class to be
accessible and usable in Objective-C
Means that your class should be #objc class SomeSwift: NSObject (You're right!), but you CANNOT access the whole thing in Swift file:
When you create a Swift class that descends from an Objective-C class,
the class and its members—properties, methods, subscripts, and
initializers—that are compatible with Objective-C are automatically
available from Objective-C. This excludes Swift-only features, such as
those listed here:
Generics
Tuples
Enumerations defined in Swift without Int raw value type
Structures defined in Swift
Top-level functions defined in Swift
Global variables defined in Swift
Typealiases defined in Swift
Swift-style variadics
Nested types
Curried functions
Reference.
So, you cannot use the SomeSwift top-level function.
Even if you tried to add #objc before its declaration, the compiler will tell that:
#objc can only used when with memebers of classes, #objc protocols,
and concrete extensions of classes.
with a suggestion to remove #objc.
Here's my example, let's say I have a custom UIView with a tap gesture recognizer that responds to this function:
func handleTap(tap: UITapGestureRecognizer) {
println("Tap!")
}
I generally prefer these to be private, so I mark it as such but it doesn't work. An #objc or dynamic specifier is required, like so:
dynamic private func handleTap(tap: UITapGestureRecognizer) {
println("Tap!")
}
This makes me believe that public functions are dynamic by default when added to an objective-c object. Is this the case? Please cite references if found.
The Swift compiler will try to prove that a call to a method can only end up with a single implementation. If it can prove this then it will use static and not dynamic dispatch. Use of the "final" or "private" keyword, and whole module optimisation, will help with this.
From Using Swift with Cocoa and Objective-C:
Requiring Dynamic Dispatch
While the #objc attribute exposes your
Swift API to the Objective-C runtime, it does not guarantee dynamic
dispatch of a property, method, subscript, or initializer. The Swift
compiler may still devirtualize or inline member access to optimize
the performance of your code, bypassing the Objective-C runtime. When
you mark a member declaration with the dynamic modifier, access to
that member is always dynamically dispatched. Because declarations
marked with the dynamic modifier are dispatched using the Objective-C
runtime, they’re implicitly marked with the #objc attribute.”
I need to create an instance of a principal class from a loadable bundle in swift. All I know about the class is that it conforms to a specific protocol (NOT #objc protocol) and it is NOT a subclass of NSObject. Is this possible at all?
It was so simple in Obj-C with NSObject, because really, obj-c doesn't care about types, but this swift, oh man, the principalClass property of NSBundle returns AnyClass?, how is it possible on Earth to create an instance of that AnyClass? type and tell it that now it conforms to a swift protocol?
thanks in advance
Non-#objc protocols do not carry runtime type information: this means that you cannot use the as operator to convert from a class type to your protocol type if the relationship is not known at compile-time. #objc protocols, on the other hand, do carry runtime type information.
If your protocol was marked #objc and declared at least one initializer, you should be able to do this:
if let bundle = NSBundle(...) {
if let meta = bundle.principalClass as? MyProtocol.Type {
let obj: MyProtocol = meta(initParam: a, initParam2: b, ...)
}
}
... except that the current Swift compiler will crash if you try to convert AnyType to a protocol type like that. It works if you can use a class type instead of a protocol type, though.
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[]()
}
For the following code, i get no errors:
class MyClass {
#lazy var arr : String[] = String[]()
}
Thank you your help!
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.