简体   繁体   English

为什么我们仍然在swift ios应用程序中使用NSDictionary和NSArrays?

[英]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. 我在互联网上看到了很多关于swift ios教程的代码,仍然使用旧的objective-c数据类型,如NSDictionaryNSArrays而不是使用Swift附带的现代ArrayDictionary数据类型。

Most often I see a lot of type casting going on using as? 我经常看到很多类型的铸件正在使用as? etc. I have seen this mostly in examples involving json parsing from a webservice. 我已经看到这主要是在涉及从Web服务进行json解析的示例中。

So why is this? 那么为什么呢? Is this because swift isn't fully compatible withe cocoa touch apis yet? 这是因为swift与可可触摸apis不完全兼容吗? 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. 即使你使用了NSArray和NSDictionary,swift也会自动连接到他们的原生swift等效数组和字典。

By using the Swift native, the performance will be great. 通过使用Swift原生,性能将是巨大的。

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. 因为您在使用NSDictionary和NSArray的Cocoa库中调用代码,Apple不会重写所有这些库。

Your question whether Swift is not fully compatible with Cocoa APIs is nonsensical. 你的问题Swift是否与Cocoa API不完全兼容是荒谬的。 It is compatible, or you couldn't call Cocoa. 它是兼容的,或者你不能调用Cocoa。 But the Cocoa APIs are not changing. 但是Cocoa API并没有改变。 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. 它使用并支持它使用和支持的类,并且使用不同的编程语言不会改变它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM