Pure Swift project. Which Apple APIs am I allowed to use? - ios

Every since Apple started open-sourcing Swift last year, I have wondering about which frameworks are going to be distributed along with its standard library.
I'm working on a pure Swift project at the moment and need to use some common objects without compromising compatibility with other (POSIX) systems. Are these objects shipped in Swift's STL?
(De)serialization objects: NSCoder, NSKeyedArchiver, NSCoding, NSXMLParser
Concurrency objects: NSOperationQueue, dispatch_async(), dispatch_sync()
File manipulation objects: NSFileManager, NSURL, NSData
And if not, are there any pure Swift alternatives?

Related

Rest, Json, Alamofire - Swift

I have really huge problem with these 3. I can't them distinct. I know that are libraries and cooperate with themselves . It seems to me that all three make the same , I mean retrieve data from Internet. Could you explain me what's going on with these three?
REST(Representational state transfer): It is a framework which provides you a way of communicating between computers using internet. Typically, over an API call. It consists of an Architecture with 6 Constraints (5 Compulsory & 1 Optional). Read more about it here.
JSON(JavaScript Object Notation): This is a standard representation of data exchange. There are other representations like XML which were used for the same. JSON consists of basic data structures in order to pass data over network. It uses String, Number, Array, another JSON Object, null and a Boolean to efficiently represent data.
The above 2 concepts are relevant in any stream or language of computer science.
Alamofire(HTTP Networking library for Swift): This library IS ONLY USED IN iOS APPS WITH SWIFT. It doesn't hold relevance outside this subset. Sure, there is a method of making network calls without using Alamofire in swift. You can read about NSURL, NSURLSession etc. to learn the classic method. The problem with normal NSURL calls is that it is very elaborate to write those calls and can get messy in no time. Thankfully, there is a way of mitigating that mess. Alamofire handles those async calls efficiently and also lets you do cool stuff with the response easily.
NOTE: These 3 are not at all same. REST calls can be made using JSON, XML, URL Encoding etc. JSON can be used in normal JavaScript and not necessarily needs to be passed over a network. and Alamofire exists to just ease the pain of making network calls in iOS.
Hope this helps!

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

iOS PDF native search

I need to present and open pdf documents in my app. I would like to avoid third part libraries, because of update reasons (and I couldn´t find anyone created in swift).
I have been looking at QLPreviewController, UIDocumentInteractionController and presenting the pdf in an UIWebView. All these alternatives works fine for just presenting the pdf but I can´t find any built in search. I want functionality like the iBooks app.
Any advice is appreciated!
You'll likely wont find any 3rd-party frameworks written in Swift yet, simply because as of Swift 2.2 it's not binary compatible yet, and any binary framework written in Swift would be very fragile to break with even a minor update of Xcode (and updates to the compiler, that is).
I'm working on the commercial available PSPDFKit SDK for both iOS and Android. We're actually using a lot of C++ internally since raw performance is very important and Objective-C (and for many things, also Swift) are not yet fast enough for certain tasks.
We did invest a lot of time in adopting the latest Objective-C features such as nullability and generics next to declarations such as noescape for block-based API to make our SDK great to consume from within Swift.
While a separate Swift-wrapper could offer additional convenience, you'll find it very simple to use, and we're always working to adopt more features that improve bridging as they come available - there are a few interesting things in the Swift 3 proposals.
If you do not want to go the framework route, you can use CGPDFScanner to base a custom text extraction engine on. You will need to read up on Character Map Parsing - Page 446ff and many other sections - extracting text from a PDF document is surprisingly difficult, and after much work you'll be left with individual glyph positions and need to approximate where words are and if the document uses spaces or if you need to synthesize your own to correctly extract text. It's something that just takes a lot of experimentation and approximation to get right.

Why do we still use NSDictionary and NSArrays in swift ios applications?

I see a lot of code for swift ios tutorials on the internet still using the old objective-c data types like NSDictionary and NSArrays instead of using the modern Array and Dictionary data types that come with Swift.
Most often I see a lot of type casting going on using as? etc. I have seen this mostly in examples involving json parsing from a webservice.
So why is this? Is this because swift isn't fully compatible withe cocoa touch apis yet? Or is there another reason like performance?
Even though you used the NSArray and NSDictionary, swift will automatically bridges to their native swift equivalent Array and Dictionary.
By using the Swift native, the performance will be great.
Please refer the documentation
Because you are calling code in the Cocoa library that uses NSDictionary and NSArray and Apple isn't going to rewrite all these libraries.
Your question whether Swift is not fully compatible with Cocoa APIs is nonsensical. It is compatible, or you couldn't call Cocoa. But the Cocoa APIs are not changing. Cocoa is a library; it uses and supports the classes that it uses and supports, and using a different programming language isn't going to change that.

Support iOS 8 - Swift

Do I need to learn and convert my entire codebase to the new Swift language if I want to support ios 8?
No. The APIs available from Swift are exactly the same as the APIs available from Objective-C; you can code against any iOS 8 APIs from either language.
To start writing in Swift, there's an option called Migration. It will convert your existing code into swift code
Migration provides an opportunity to revisit an existing Objective-C
app and improve its architecture, logic, and performance by replacing
pieces of it in Swift. For a straightforward, incremental migration of
an app, you’ll be using the tools learned earlier—mix and match plus
interoperability. Mix-and-match functionality makes it easy to choose
which features and functionality to implement in Swift, and which to
leave in Objective-C.
NO.
Your Swift code can be run alongside your Objective-C code because Swift is built with the same compiler, ARC management and runtime as Objective-C.

Resources