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.
Related
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 :))
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've recently moved to Swift from regular objc. And yes, I know how to use Any and AnyObject.
But I'm curious why Apple decided on AnyObject and Any. Why is it good? What problem is it solving? How much developer time is it saving?
For backward compatibility with existing Cocoa libraries. Classes like NSDictionary, NSArray, etc. do not enforce type on all objects contained. Also in Obj-C there's id which is a pointer to any Obj-C object.
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.
There are couple of JSON frameworks. JSONKit , SBJSON , Native JSON and other. Which one is the best to use for Iphone app OR ios App and why ?
I have used SBJson for two years, and many Object-C API use SBJson too.
It is quite hard to compare different json apis, however SBJson is easy to use and widely used.
if you want to target just iOS5 and above you could try NSJSONSerialization
Basically iOS 5 has a new class named NSJSONSerialization. It has a
static method called JSONObjectWithData:options:error that takes an
NSData and gives you back a Foundation object – usually an
NSDictionary or an NSArray depending what do you have at the top of
your JSON file hierarchy.
an excellent example
from Ray Wenderlich