Suggest how to translate Objective C pointer arithmetic into Swift - ios

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!

Related

Swift / Objective C: Is there a way to find all objects of a certain type?

I understand this might be bad software designing but I am mostly curious if this is possible without actually using it in a live app.
In Swift / Objective C: Is there a quick performant way to find all objects of a certain type? Lets say I want all objects which are currently referenced of type UIViewController. Is this possible?

Spawn thread with Swift entry point from within C function in iOS

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.

Making a Swift OSS library compatible with Objective-C

I am building a library in Swift, and it has to support Objective-C.
I already checked this answer which recommends to write the library in Objective-C but the requirements that were given to me are to write the library in Swift. I am delivering the library in source form, so the argument there (against writing the library in Swift) about unstable ABI should not apply in my case.
So I've heard that in order to make this Swift library work for Objective-C, I will have to avoid using the advanced features in Swift that are not available in Objective-C. Examples of these are:
Generics
Structs
All Swift classes must derive from NSObject
So my 2 questions are:
Where Can I find an exhaustive list for those constraints?
How can I quickly test that my library is compatible with Objective-C? I am not familiar at all in the interoperability topic of Swift and Objective-C. Not a lot articles that I could find online. Is the official Apple docs sufficient? and which parts can help?
I appreciate all the help here.
The most comprehensive list of Swift features not available from Objective-C is in the Swift Type Compatibility section of Apple's Using Swift with Cocoa and Objective-C guide.
Quoting from there, the list of exclusions are as follows:
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
The whole guide is worth reading, but I'd pay particular attention to the Mix and Match section which describes calling Swift from Objective-C and vise-versa, including external frameworks.
I would definitely recommend doing as #Mike Taverne suggests: make a suite of unit tests in Objective-C which exercise the APIs you've developed in Swift. That's the best way to make sure it all works as expected.
There are more things good to know beforehand.
You cannot extend swift class by objective c class.
You cannot use #objc on generic swift class or method (you can use generics in #objc swift class on methods though)
So the api will be more limited than if written in objective c as it support generics (though people like to say not real generics etc...) and not to be able to extend any class you provide in your api can be quite limitation too, but still it's doable.
You just have to put #objc everywhere and compiler will say you very quickly what is not supported. Also casting can be quite tricky sometimes... I ended up to cast like this because I was reciving objective c generic class from swift in swift code
as! CSResponse<AnyObject> as! (CSResponse<AnyObject> & CSListData)
This is working code, but direct cast is imposible.
Best is to write pure swift for swift and use objective c libs as needed otherwise you will and up fighting to write one line of code. (like me :))

How to deal with the lack of reflection in Swift?

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

Why is there no Objective C AddressBook Framework for iOS, and how to best tackle the problems with C 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

Resources