How to use RxSwift in project with Objective-C code? - swift

I am working in a old project that mixes Objective-C and Swift, there is a lot of legacy code in Objective-C, so migrating everything to Swift is not realistic. New code is written in Swift but Objective-C needs to be mantained with small updates.
And I have been working with RxSwift recently and I want to start using in this old project too.
The problem is the Objective-C <-> Swift interoperability.
e.g. I can't write this:
#objc let int = BehaviorSubject<Int>(value: 42)
Because I get Property cannot be marked #objc because its type cannot be represented in Objective-C
How can I deal with this situation? Is anyone using RxSwift in a mixed objc/swift project?

The core of RxSwift is the Event enum with associated values. This enum cannot be ported into Objective-C.
The answer to your question is... You can't use RxSwift in Objective-C code.
An alternative would be use ReactiveObjC in your Objective-C code and map from RxSwift Observables to ReactiveObjC Signals.

Related

Where can I find the implementation of "class DispatchQueue : DispatchObject"?

I am interested to learn more about the implementation details of class DispatchQueue : DispatchObject.
I have looked here, and here but I can't find the matching implementation.
You probably won't find this in the Swift code because it isn't Swift. This is just a front end for Cocoa's OS_dispatch_queue etc., which are not open source. It's not part of the Swift library; it's an iOS thing, not a language thing.

Attribute weak extern in Swift

I'm converting the obj-c framework to the Swift one.
I need to convert this line:
__attribute__((weak)) extern NSString *sdkVersion = 0;
to Swift similar one. Spend a lot of time on google, but didn't find the answer.
Predicting your questions, what I'm trying to achieve... in short words, this global variable is defined in an optional framework. My Swift function should print this version in case if our SDK is linked with that framework.
I know, I can create a wrapper (another objc framework), add necessary getters there, and then link it with the main framework. But may be there is a more simple solution?
Thanks in advance.

FMDatabaseQueue (FMDB) using Swift

I've been trying to find Swift examples for FMDatabaseQueue.
All I have been able to find is Objective-C here: https://ccgus.github.io/fmdb/html/Classes/FMDatabaseQueue.html
Im not familiar with Objective-C and I'm new to Swift so I'm not sure how to port the code.
I've found what I was looking for here https://github.com/ccgus/fmdb/issues/291
There exists Swift wrappers for SQLite that may be a better fit that fmdb which can run in Swift but does not use Swift features such as type safety or error handling. See for example my GRDB.swift https://github.com/groue/GRDB.swift which was heavily influenced by ccgus/fmdb.

Realm Swift iOS7 documentation

I was just wondering if there is any written documentation on realm swift for iOS 7, since it is combination of objective c and swift.
Just to be clear, I'm not looking for official documentation, just some guidelines so I have a clearer view of what to use.
Thank you for any help.
You should refer to Realm Objective-C's documentation guide (https://realm.io/docs/objc/latest) and API documentation (https://realm.io/docs/objc/latest/api), which is applicable even when used from Swift.
As our installation instructions recommend (https://realm.io/docs/objc/latest/#installation), you should also be using RLMSupport.swift which adds some useful helpers to use Realm Objective-C from Swift. That file's source is its best documentation, it's pretty small and should be fairly readable.
Finally, to determine the exact Swift syntax when calling Objective-C methods, I recommend you either use autocomplete, or command-click a Realm.framework token from Swift, which should display Xcode's auto-generated Swift interface for the Realm module.

When I import Swift, where does it come from?

I am beginning to work my way through Neuburg's iOS Programming with Swift book, and have a real newbie question: Newburg says:
"your code always implicitly imports Swift itself." (p. 14)
But where does it import it from? Do I need to purchase this "Swift" thing in order to use the Swift language?
Is means that it imports the Swift module/framework by default.