Swift - which types to use? NSString or String

后端 未结 9 1024
死守一世寂寞
死守一世寂寞 2020-12-23 00:06

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, N

9条回答
  •  鱼传尺愫
    2020-12-23 00:54

    Swift Strings are quite elegant and easy to use, unless you need to parse them. The whole concept of indexing into Swift strings is just plain crazy. Whenever I need to parse through a typical unicode string, I convert it to NSString. Swift makes this a bit tricky though in that the common "character" integer expressions like ' ' or 'A' or '0' don't work in Swift. You have to use 32, 65, 48. Unfortunately, I'm not kidding! Because of this, I've put most of my string parsing code into an NSString extension written in Objective-C.

    Yes I do know WHY Swift's designers made String indexing so crazy: They wanted to be able to express many-byte characters like emoji as single "Characters". My choice would have been to let this rare use case be expressed as multiple UTF16 characters, but what the heck.

提交回复
热议问题