ios Swift - 使用哪些类型?NSString 或字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24038629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift - which types to use? NSString or String
提问by Alec
With the introduction of Swift I've been trying to get my head round the new language
随着 Swift 的引入,我一直在努力了解新语言
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
我是一名 iOS 开发人员,会NSString, NSInteger, NSDictionary
在应用程序中使用类型。我注意到在 Apple 的“The Swift Programming Language”电子书中,他们使用 Swift 类型String, Int, Dictionary
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
.
我注意到 Swift 类型没有(或名称不同)一些 Foundation 类型具有的功能。例如NSString
有一个length
属性。但是我一直没能为 Swift 找到一个类似的String
。
I'm wondering, for an iOS application should I still be using the Foundation types?
我想知道,对于 iOS 应用程序,我还应该使用 Foundation 类型吗?
采纳答案by Cezar
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.
你应该尽可能使用 Swift 原生类型。该语言经过优化以使用它们,并且大部分功能都在本机类型和Foundation
类型之间桥接。
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 answerfor a discussion on how to get the a String's length and this answerfor a discussion on using containsString()
to check for substrings. (Disclaimer: I'm the author for both of these answers)
虽然String
和NSString
大部分是可以互换的,即您可以将String
变量传递给带NSString
参数的方法,反之亦然,但截至目前,某些方法似乎无法自动桥接。见这个答案就如何得到一个字符串的长度的讨论,这个答案的讨论使用containsString()
来检查子。(免责声明:我是这两个答案的作者)
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
我还没有完全探索其他数据类型,但我认为上述内容的某些版本也适用于Array
/ NSArray
、Dictionary
/NSDictionary
以及 Swift 和NSNumber
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
.
每当您需要使用 Foundation 类型之一时,您可以使用它们显式键入变量/常量,例如 invar str: NSString = "An NSString"
或使用bridgeToObjectiveC()
Swift 类型的现有变量/常量,str.bridgeToObjectiveC().length
例如。您也可以施放String
到NSString
使用str as 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.
但是,这些技术明确使用 Foundation 类型或至少其中一些类型的必要性在将来可能会过时,因为根据语言参考中的说明,例如String
/NSString
桥应该是完全无缝的。
For a thorough discussion on the subject, refer to Using Swift with Cocoa and Objective-C: Working with Cocoa Data Types
有关该主题的深入讨论,请参阅将Swift 与 Cocoa 和 Objective-C 结合使用:使用 Cocoa 数据类型
回答by Sandy Rawat
NSString : Creates objects that resides in heap and always passed by reference.
NSString :创建驻留在堆中并始终通过引用传递的对象。
String: Its a value type whenever we pass it , its passed by value. like Struct and Enum, String itself a Struct in Swift.
字符串:每当我们传递它时,它都是一种值类型,它是按值传递的。就像 Struct 和 Enum 一样,String 本身就是 Swift 中的 Struct。
public struct String {
// string implementation
}
But copy is not created when you pass. It creates copy when you first mutate it.
但是当你通过时不会创建副本。当您第一次对其进行变异时,它会创建副本。
String is automatically bridged to Objective-C as NSString. If the Swift Standard Library does not have, you need import the Foundation framework to get access to methods defined by NSString.
String 作为 NSString 自动桥接到 Objective-C。如果 Swift 标准库没有,则需要导入 Foundation 框架才能访问 NSString 定义的方法。
Swift String is very powerful it has plethora of inbuilt functions.
Swift String 非常强大,它有大量的内置函数。
Initialisation on String:
字符串初始化:
var emptyString = "" // Empty (Mutable)
let anotherString = String() // empty String immutable
let a = String(false) // from boolean: "false"
let d = String(5.999) // " Double "5.99"
let e = String(555) // " Int "555"
// New in Swift 4.2
let hexString = String(278, radix: 18, uppercase: true) // "F8"
create String from repeating values:
从重复值创建字符串:
let repeatingString = String(repeating:"123", count:2) // "123123"
In Swift 4 -> Strings Are Collection Of Characters:
在 Swift 4 -> 字符串是字符的集合:
Now String is capable of performing all operations which anyone can perform on Collection type.
现在 String 能够执行任何人都可以对 Collection 类型执行的所有操作。
For more information please refer apple documents.
更多信息请参考苹果文档。
回答by Emanuel
Your best bet is to use Swift native types and classes, as some others have noted NSString has toll free translation to String, however, they're not the same a 100%, take for example the following
你最好的选择是使用 Swift 原生类型和类,因为其他一些人已经注意到 NSString 可以免费转换为 String,但是,它们不是 100%,例如以下
var nsstring: NSString = "\U0001F496"
var string: String = "\U0001F496"
nsstring.length
count(string)
you need to use the method count() to count the characters in string, also note that nsstring.length returns 2, because it counts its length based on UTF16.
需要使用count()方法计算string中的字符数,还要注意nsstring.length返回2,因为它是基于UTF16计算长度的。
Similar, YES The same, NO
相似,是 相同,否
回答by Gabriele Petronella
String
and NSString
are interchangeable, so it doesn't really matter which one you use. You can always cast between the two, using
String
并且NSString
是可以互换的,所以你使用哪一个并不重要。您始终可以在两者之间进行转换,使用
let s = "hello" as NSString
or even
甚至
let s: NSString = "hello"
NSInteger
is just an alias for an int
or a long
(depending on the architecture), so I'd just use Int
.
NSInteger
只是 anint
或 a的别名long
(取决于架构),所以我只使用Int
.
NSDictionary
is a different matter, since Dictionary
is a completely separate implementation.
NSDictionary
是另一回事,因为它Dictionary
是一个完全独立的实现。
In general I'd stick to swift types whenever possibile and you can always convert between the two at need, using the bridgeToObjectiveC()
method provided by swift classes.
一般来说,我会在任何可能的情况下坚持使用 swift 类型,并且您可以随时在需要时使用bridgeToObjectiveC()
swift 类提供的方法在两者之间进行转换。
回答by Jiaaro
Since the objective C types are still dynamically dispatched they're probably going to be slower. I'd say you're best served using the Swift native types unless you need to interact with objective-c APIs
由于目标 C 类型仍然是动态调度的,因此它们可能会变慢。我会说你最好使用 Swift 原生类型,除非你需要与 Objective-c API 交互
回答by Nate Cook
Use the Swift native types whenever you can. In the case of String, however, you have "seamless" access to all the NSString
methods like this:
尽可能使用 Swift 原生类型。但是,在 String 的情况下,您可以“无缝”访问所有NSString
像这样的方法:
var greeting = "Hello!"
var len = (greeting as NSString).length
回答by Fangming
Swift 4 update
斯威夫特 4 更新
String gets revisions in swift 4. Now you can directly call count on it and it consider grapheme clusters as 1 piece, like an emoji. NSString is not updated and is counting it in another way.
String 在 swift 4 中得到修订。现在您可以直接调用 count 并将字素簇视为 1 个,就像表情符号一样。NSString 未更新,并以另一种方式对其进行计数。
var nsstring: NSString = "???"
var string: String = "???"
print(nsstring.length) // 11
print(string.count) // 1
回答by kashish makkar
String is a struct
字符串是一个结构体
// in Swift Module
// 在 Swift 模块中
public struct String
公共结构字符串
{
{
}
}
NSString is a class
NSString 是一个类
// in Foundation Module
// 在基础模块中
open class NSString : NSObject
打开类 NSString : NSObject
{
{
}
}