精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究
With the introduction of Swift I've been trying to get my head round the new language
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
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.
I'm wondering, for an iOS application should I still be using the Foundation types?
随着Swift的推出,我一直试图理顺新语言
我是iOS开发人员,会在应用程序中使用NSString, NSInteger, NSDictionary类型。我注意到在Apple的“The Swift Programming Language”电子书中,他们使用了Swift类型String, Int, Dictionary
我注意到Swift类型没有(或者命名不同)Foundation类型的一些功能。例如NSString有一个length属性。但我无法为Swift找到类似的String。
我想知道,对于iOS应用程序,我还应该使用基础类型吗?
Prefer String. However, String functionality is still a bit limited in the alpha release, so NSString will be more used for now. We hope they will fix the API until GA
喜欢String。但是,String alpha版本的功能仍然有限,因此NSString现在将更多地使用。我们希望他们能在GA之前修复API
Also, you can call countElements(str), to answer that specific part of your question此外,您可以调用countElements(str),回答问题的具体部分
Better, you can call "foo".length" directly in Swift. An implicit cast to NSString` is added for you by the compiler更好,您可以直接调用"foo".length". 通过编译器为您添加对NSString的隐含转换!
.length may not work correctly for special characters. Such as Emoji or Chinese characters that take up 2 or 3 unicode chars. countElements is the correct method to use。 .length可能无法正常使用特殊字符。如表情符号或中文字符占用2或3个unicode字符。countElements是正确的使用方法
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.
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)
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
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.
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.
For a thorough discussion on the subject, refer to Using Swift with Cocoa and Objective-C: Working with Cocoa Data Types
您应该尽可能使用Swift本机类型。该语言已经过优化以使用它们,并且大多数功能都在本机类型和Foundation类型之间进行桥接。
虽然String并且NSString大多数是可互换的,即,您可以将String变量传递给接受NSString参数的方法,反之亦然,但是到目前为止,某些方法似乎不会自动桥接。有关如何获取字符串长度的讨论,请参阅,以及有关使用检查子字符串的讨论的containsString()。(免责声明:我是这两个答案的作者)
我还没有完全探索其他数据类型,但我认为上面所述的某些版本也适用于Array/ NSArray,Dictionary/ NSDictionary和Swift中的各种数字类型NSNumber
每当您需要使用其中一种Foundation类型时,您可以使用它们显式地键入变量/常量,比如var str: NSString = "An NSString",或在Swift类型的现有变量/常量上使用bridgeToObjectiveC(),类似str.bridgeToObjectiveC().length,例如。你也可以通过把String转换为NSStringstr as NSString
但是,这些明确使用Foundation类型技术,或至少其中一些类型的必要性在将来可能已经过时,因为从语言参考中所述,例如String/ NSString bridge应该是完全无缝的。
I notice in the Swift 3 manual that the Foundation types are passed by reference, while the native types are passed by value (or something like that I don't fully understand at this point). It would be great if you could update your answer discussing the implications of that我在Swift 3手册中注意到Foundation类型是通过引用传递的,而本机类型是通过值传递的(或者类似的东西,我现在还不完全理解)。如果您可以更新您的答案,讨论其含义,那就太棒了
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.String:每当我们传递它时它是一个值类型,它通过值传递。像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:从重复值创建String:
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.
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有免费的字符串转换,但是,它们不是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.
Similar, YES The same, NO
你需要使用方法count()来计算字符串中的字符,还要注意nsstring.length返回2,因为它根据UTF16计算它的长度。
相似,是的,同样,不
String and NSString are interchangeable, so it doesn't really matter which one you use. You can always cast between the two, usingString并且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.
NSDictionary is a different matter, since Dictionary is a completely separate implementation.
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
NSInteger只是一个int或一个别名long(取决于架构),所以我只是使用Int。
NSDictionary是另一回事,因为它Dictionary是一个完全独立的实现。
一般来说,只要有可能,我就会坚持使用swift类型,并且你总是可以使用bridgeToObjectiveC()swift类提供的方法在需要时进行转换。