Auto Injection with typhoon + swift - ios

I'm using typhoon in a swift project, as far as i understand i have to map all injections explicitly in a TyphoonAssembly like this:
public dynamic func appDelegate() -> AnyObject {
return TyphoonDefinition.withClass(AppDelegate.self) {
(definition) in
definition.injectProperty("cityDao", with:self.coreComponents.cityDao())
definition.injectProperty("rootViewController", with:self.rootViewController())
}
}
this seems hard to manage, and very fragile (when refactoring).
I see that there is support for auto injection (matching by types) here:
https://github.com/appsquickly/Typhoon/wiki/Auto-injection-(Objective-C)
but this is for objetive c.
Does anyone know of a way i can wire up the injection without explicitly registering props with their name as string?
Thanks!

(Typhoon creator here).
Typhoon is a dynamic, introspective dependency injection container , and uses the Objective-C run-time. There are the following limitations, when it comes to Swift:
With Objective-C it avoids magic strings, allowing the use of ordinary IDE refactoring tools, however in Swift selectors are magic strings.
The Objective-C runtime only provides type information for properties, not method or initializer parameters. So only properties can support auto-wiring of any kind (macros, implicit, etc).
There is no annotation or macro system for Swift, (but it does have 1st class functions).
You can instruct Typhoon to auto-wire a property in Swift using the following:
public dynamic func appDelegate() -> AnyObject {
return TyphoonDefinition.withClass(AppDelegate.self) {
(definition) in
definition.injectProperty("cityDao")
definition.injectProperty("rootViewController")
}
}
. . and this will match by type, just as the auto-wiring Objective-C macros do. However this does not avoid specifying the name of the property to be injected. There is no other way to do this in Swift. :(
Its worth mentioning there are additional limitations when using Typhoon with Swift:
"Pure" Swift classes - not extending a Cocoa base class or declaring the #objc directive - don't support introspection, dynamic dispatch or dynamic method invocation. Typhoon only works with Cocoa classes.
Swift protocols require the #objc directive.

It's possible to use Typhoon's auto-wiring in Swift.
The auto-wiring in ObjC is like:
#property (nonatomic, strong) InjectedClass(ViewController) rootViewController;
We can survey the definition of the macro, InjectedClass
#define InjectedProtocol(aProtocol) TyphoonInjectedObject<aProtocol>*
#define InjectedClass(aClass) aClass<TyphoonInjectedProtocol>*
Thus, the auto-wiring in Swift is like
var rootViewController: (ViewController & TyphoonInjectedProtocol)?

Related

Category for obj-c class derived from swift

I am running project with lots of legacy code with objc and swift.
I've been using objc MPOldKeychainManager, which is now deprecated and swift's NewKeychainManager is to be used.
The problem is following: MPOldKeychainManager had some categories written and I don't want to rewrite them as swift extensions.
What I've done is:
naming the class derived from the NewKeychainManager (visible in "myTarget-Swift.h") to "MPOldKeychainManager"
removing the objc declaration of MPOldKeychainManager
...hoping that the categories will still work.
objc(MPOldKeychainManager)
class NewKeychainManager: KeychainManager {
}
Unfortunately, old extensions can't see the MPOldKeychainManager (derived from swift), even though I've updated the imported header to myTarget-Swift.h
#import "myTarget-Swift.h" //previously - objc "MPOldKeychainManager.h"
#interface MPOldKeychainManager (Authentication)
Question: is it possible to use categories for objc classes derived from swift?
I have already tried totally new naming
I have already tried loads of clean-builds
In case you haven't seen it, here is a useful resource for migrating from Objective-C to Swift: https://developer.apple.com/documentation/swift/migrating_your_objective_c_code_to_swift . Among other things, it states that one cannot subclass a Swift class in Objective-C. What you are trying to do is specify a different Objective-C name, MPOldKeychainManager, for the NewKeychainManager Swift class.
This will, actually, work if you add an ampersand before objc, like so:
#objc(MPOldKeychainManager)
class NewKeychainManager: KeychainManager {
}
You can then use all your existing categories in Objective-C. You will, however, have a problem using them in Swift, because to be usable in Swift they need to be available in the bridging header, and you won't be able to use the class' Objective-C name (MPOldKeychainManager) in the bridging header.
You can, however, write an Objective-C wrapper class that will have a method corresponding to each category method and also taking a NewKeychainManager pointer. A wrapper method can then delegate to the category method, which is available to Objective-C code, so you won't have to re-implement your category methods in Swift.
Let's say an Objective-C category has method authenticateUser::
#interface MPOldKeychainManager (Authentication)
-(void)authenticateUser:(int32_t)uid;
#end
The method could be wrapped as follows:
#interface OldKCMWrapper : NSObject
+(void)authenticateUser:(int32_t)uid withManager:(NewKeychainManager*)inst;
#end
This interface declaration must be available, directly or indirectly, via the bridging header. Then, somewhere in your Objective-C code, the wrapper could be implemented thus:
#implementation OldKCMWrapper
+(void)authenticateUser:(int32_t)uid withManager:(MPOldKeychainManager*)inst {
[inst authenticateUser:uid];
}
#end
The wrapper can then be used in Swift code, e.g.:
let kcm = NewKeychainManager()
OldKCMWrapper.authenticateUser(321, with: kcm)
In fact, the wrapper could be used in a Swift extension of NewKeychainManager. You would still have a Swift extension with equivalents of all the Objective-C category methods, but you would not have to re-implement their code in Swift: methods in the extension would simply delegate to the wrapper.
Hopefully this is helpful. There are other ways of implementing this idea, possibly more elegant.

When #objc and #nonobjc write before method and variable in swift?

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.

Objective-C call Swift function

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.

Are public swift functions on ObjC objects dynamically or statically dispatched?

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.”

What is the equivalent for java interfaces or objective c protocols in swift?

I've been looking in to the new Swift language trying to find what's the equivalent for an interface(in java) or a protocol(in objective-c) in Swift, after surfing on the internet and searching in the book provided by Apple, I still can't seem to find it.
Does any one know what's the name of this component in swift and what's its syntax?
Protocols in Swift are very similar to Objc, except you may use them not only on classes, but also on structs and enums.
protocol SomeProtocol {
var fullName: String { get } // You can require iVars
class func someTypeMethod() // ...or class methods
}
Conforming to a protocol is a bit different:
class myClass: NSObject, SomeProtocol // Specify protocol(s) after the class type
You can also extend a protocol with a default (overridable) function implementation:
extension SomeProtocol {
// Provide a default implementation:
class func someTypeMethod() {
print("This implementation will be added to objects that adhere to SomeProtocol, at compile time")
print("...unless the object overrides this default implementation.")
}
}
Note: default implementations must be added via extension, and not in the protocol definition itself - a protocol is not a concrete object, so it can't actually have method bodies attached. Think of a default implementation as a C-style template; essentially the compiler copies the declaration and pastes it into each object which adheres to the protocol.
swift has protocols as well, here is the relevant documentation:

Resources