Working with C strings in Swift, or: How to convert UnsafePointer to CString

后端 未结 2 791
夕颜
夕颜 2020-12-15 16:24

While playing with Standard C Library functions in Swift, I came across problems when passing C strings around. As a simple example (just to demonstrate the problem), the St

2条回答
  •  孤城傲影
    2020-12-15 17:29

    Swift 1.1 (or perhaps earlier) has even better C string bridging:

    let haystack = "This is a simple string"
    let needle = "simple"
    let result = String.fromCString(strstr(haystack, needle))
    

    The CString type is gone completely.

提交回复
热议问题