Swift - 使用哪种类型? NSString或String [英] Swift - which types to use? NSString or String

查看:137
本文介绍了Swift - 使用哪种类型? NSString或String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着Swift的推出,我一直试图让我的头脑绕过新语言

With the introduction of Swift I've been trying to get my head round the new language

我是一名iOS开发人员,会使用应用程序中的NSString,NSInteger,NSDictionary 。我注意到在Apple的The Swift Programming Language电子书中,他们使用Swift类型 String,Int,Dictionary

I'm an iOS developer and would use types such as NSString, NSInteger, NSDictionary in an application. I've noticed that in the "The Swift Programming Language" ebook by Apple, they use the Swift types String, Int, Dictionary

我注意到Swift类型没有(或者命名不同)Foundation类型的一些功能。例如 NSString 具有长度属性。但是我找不到类似的Swift String

I've noticed the Swift types don't have (or are differently named) some of the functions that the Foundation types do. For example NSString has a length property. But I've not been able to find a similar one for the Swift String.

我想知道,对于iOS应用程序,我是否还应该使用Foundation类型?

I'm wondering, for an iOS application should I still be using the Foundation types?

推荐答案

您应该尽可能使用Swift本机类型。该语言已经过优化以使用它们,并且大多数功能都在本机类型和 Foundation 类型之间进行桥接。

You should use the Swift native types whenever possible. The language is optimized to use them, and most of the functionality is bridged between the native types and the Foundation types.

虽然字符串 NSString 主要是可以互换的,即你可以传递 String 变量到采用 NSString 参数的方法中,反之亦然,有些方法似乎不会自动桥接到目前为止。有关如何获取字符串长度和此答案。 com / a / 24037522/1371070>这个答案,讨论使用 containsString()来检查子字符串。 (免责声明:我是这两个答案的作者)

While String and NSString are mostly interchangeable, i.e, you can pass String variables into methods that take NSString parameters and vice versa, some methods seem to not be automatically bridged as of this moment. See this answer for a discussion on how to get the a String's length and this answer for a discussion on using containsString() to check for substrings. (Disclaimer: I'm the author for both of these answers)

我还没有完全探索其他数据类型,但我假设上面所述的某些版本将会也适用于数组 / NSArray 字典 / NSDictionary ,以及Swift中的各种数字类型和 NSNumber

I haven't fully explored other data types, but I assume some version of what was stated above will also hold true for Array/NSArray, Dictionary/NSDictionary, and the various number types in Swift and NSNumber

每当您需要使用其中一种Foundation类型时,您可以使用它们显式地键入变量/常量,如 var str:NSString =An NSString或使用 bridgeToObjectiveC()关于Swift类型的现有变量/常量,如 str.bridgeToObjectiveC()。length for例。您还可以使用 str作为NSString String 强制转换为 NSString $ c>。

Whenever you need to use one of the Foundation types, you can either use them to type variables/constants explicitly, as in var str: NSString = "An NSString" or use bridgeToObjectiveC() on an existing variable/constant of a Swift type, as in str.bridgeToObjectiveC().length for example. You can also cast a String to an NSString by using str as NSString.

然而,这些技术明确使用基础类型或至少其中一些的必要性,将来可能会过时,因为从在语言参考中声明, String / NSString 桥,例如,应该是完全无缝的。

However, the necessity for these techniques to explicitly use the Foundation types, or at least some of them, may be obsolete in the future, since from what is stated in the language reference, the String/NSString bridge, for example, should be completely seamless.

有关该主题的详细讨论,请参阅将Swift与Cocoa和Objective-C一起使用:使用Cocoa数据类型

For a thorough discussion on the subject, refer to Using Swift with Cocoa and Objective-C: Working with Cocoa Data Types

这篇关于Swift - 使用哪种类型? NSString或String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆