Is it possible to spawn a thread with a Swift entry point from within a C function in iOS? For example, using gcd or the POSIX threading API (not sure if this can be used within an iOS app or not) and specifying a function signature for a Swift function?
I am not trying to solve a specific problem here, but am curious about practical limitations of Swift & C interoperability within iOS. It seems that Apple provide seamless cross-compatibility bridging Swift to C, but I haven't found much about going the opposite direction.
You can access POSIX threading mechanisms in Swift. I have some examples of working with C in this project and this other hobby project. Hopefully they are useful to you.
When working with things like pthread_create you will want to pass in either a Swift function or closure as the function pointer argument. I attempted going from Swift => C and having C start the thread with a Swift function via #_cdecl and wasn't able to get it working. It might be possible but the context of how I had things setup I couldn't get it in this case.
The other thing to keep in mind is that once you are in C land you will need a way to get back. This is typically done through void* as your context pointer. From Swift you will likely want to pass in an UnsafePointer you are managing or use Unmanaged<T> to directly pass a class instance you have into C and back into Swift.
Related
I have an old library in objective C, developped by several people following (one stop and the next continue the project...).
This library is used in several applications, most of them developped in objective C.
I want to add new features in this app, but the code is not maintainable actually (no tests, no comment, etc...).
My main idea was to rebuild a framework or library in Swift. I have the business requirement but I'm not comfortable with objective c.
Am I going to have compatibility problems if I want to use my new swift library in "old" objective C application, or It could be great If I don't change the API?
Could it be transparent for the client who have his own application and have to use the new library now?
As long as you keep the interface the same and mark all previously public methods/properties as #objc, there should be no breaking changes to the API of the library and hence all consumers should be able to continue using it without any changes on their side.
However, be aware that having to mark all methods as #objc will limit your ability to use the full extent of Swift, since some features of Swift are not available in Obj-C and hence if you want your types/functions to be available in Obj-C, you cannot use such features (i.e. you cannot use tuples in function signatures or add methods to your enums).
As an experienced Objective-C developer who is now learning Swift , I'm really missing some of the reflection and dynamic features of Objective-C.
For eg: I had written a JSON serializer which automatically mapped keys and values using KVO and Objective C introspection , and there are open source libraries like Mantle which do this.
I could declare my object as an NSObject subclass and proceed but I feel that this is not the Swift way of doing things.
Is there any other way to accomplish the same tasks , while avoiding boilerplate , using what Swift provides ?
EDIT: (2016) this answer is auto-dated. Some of the advice may still be relevant but now that Swift is open-source, I would look into other possible answers.
There is no native KVO reflection like what you described built into Swift. See:
https://stackoverflow.com/a/24092370/798682
And based on what we do know about how the Swift compiler optimizes method execution at compile time (vs the pure runtime implementation of ObjC) it doesn’t seem likely to be added anytime soon. See https://stackoverflow.com/a/25438299/798682 and
http://blog.untitledkingdom.co.uk/obj-c-vs-swift/
for more info on that.
With all that being said, here is a blog post on some KVO alternatives in Swift:
http://blog.scottlogic.com/2015/02/11/swift-kvo-alternatives.html
and another that details some of the reflection capabilities that are in Swift:
http://freecake.angelodipaolo.org/simple-reflection-in-swift/.
I'm writing a program in Swift, which has scrolling in it and I decided to use this code, which works pretty well, but it is in Objective C:
https://gist.github.com/mayoff/4187399
I've translated 95% of it except one method, namely:
- (void)createPathPoints
I incorporate Objective C with Swift, using UIBezierPath+forEachElement.m
and UIBezierPath+forEachElement.h and the bridge to Swift.
I'm having difficulties with this method because it uses a lot of pointer arithmetic, which is not present in Swift. So I wonder can somebody help me to translate this one method, or suggest another way how one can load points of the BezierPath into an array.
The github link also provides link to original question of Stack Overflow, if somebody needs it.
Thank You!
I'm trying to translate an Objective-C app into Swift and I don't know how to implement a malloc.
Is it possible to use it in Swift?
Thank you
You need to implement a bridging header when you use ObjC or C from Swift. The functions exported by your bridging header are then available in your Swift app/module. See here, for the overview.
If you just need to "call some code" on the C-side, then the functions exported from C are basically just wrappers for Swift. However, if you need to interact with the data returned from those functions -- especially if malloc'd and not a simple primitive -- Swift has a number of C related types ready for your use (see here for specifics).
Furthermore, if you're trying to wrap or interact with C++ code, you can't directly do so from Swift. You have to setup an initial interface with ObjC or C for the C++ code, and then bridge that to Swift. Not really fun at all, but thankfully it's not as common a use case as bridging ObjC (primarily) or C.
... and for what it's worth, unless you need low level Core Audio for some reason (granted, like porting an app you already have), AVAudioEngine (iOS8+) is so much simpler for any applicable use case than Core Audio, and is readily available in Swift.
This might be really generic and rather about the framework in general than a programming question. But, in the light of Swift, and the tedious and sometimes impossible tasks you have interacting with C APIs, that question is very relevant.
When building a new app for iOS, I discovered that you can really have a hard time working with address book framework. First, there is the uncomfortable pointer passing that you have to do for many CoreFoundation Methods. Secondly, the functions mostly return that ugly Unmanaged objects, where you have to figure out if they are retained or not (ARC is several years old now!). Accessing the properties through their identifiers is terribly cumbersome and far from typesafe. And lastly, since there is no C Function Pointer Support yet, you can´t even call ABAddressBookRegisterExternalChangeCallback(addressBook: ABAddressBook!, callback: ABExternalChangeCallback, context: UnsafeMutablePointer<Void>) because the ABExternalChangeCallback is so far only defined in Objective-C!
Then I found that there is some nice Objective-C Api in the Mac OS Version of AddressBookFramework. How unfair! Isn´t the iOS Frameworks younger? Why do you think Apple did this? And when will they change this in your opinion? Did I miss something, and is there an Objective-C Api for iOS, too?
Any suggestions for how to tackle above problems in the most convenient and beautiful way are welcome, too! For my part, I´m writing a complete wrapper to obscure all the nasty pointer- C-Function- and global constants uglyness. As soon as it´s ready I´ll commit it to StackExchange and maybe Github to let others benefit and discuss my solution.
EDIT: finally managed to upload my wrapper to GitHub. See
https://github.com/SocialbitGmbH/SwiftAddressBook
I agree with you about what iOS provides to access to the address book.
I've posted an answer explaining how I handled the problem, using some functional aspects of swift, and how I dealt with extracting unmanaged objects.
Briefly:
I defined a custom operator to allow me chaining function calls to transform some input data
I implemented 2 generic functions to extract unmanaged objects
posted some code to show how I access to the address book, loop through all contacts, and retrieve some data for each one