Swift - which types to use? NSString or String

后端 未结 9 1040
死守一世寂寞
死守一世寂寞 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:56

    String and NSString are interchangeable, so it doesn't really matter which one you use. You can always cast between the two, using

    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.

提交回复
热议问题