How to define a char array in Swift

Here is my C code in Objective-C method

char addressBuffer [100];

But how to define this char in Swift?

I try something like this, but this does not work:

var addressBuffer: CChar (100)

Here is the documentation https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/InteractingWithCAPIs.html

+4
source share
3 answers

this is the way to get a pretty array of Unicode characters in Swfit:

var charArray: Array<Character> = Array(count: 100, repeatedValue: "?")

if it first fills your array with 100 question marks.

Update

with CCharfor example:

var charArray: Array<CChar> = Array(count: 100, repeatedValue: 32) // 32 is the ascii space
+10
source

Swift, , - C char 100 ( C):

var addressBuffer = [Int8](count: 100, repeatedValue: 0)

// test it
addressBuffer[0] = 65 // 'A'
addressBuffer.withUnsafePointerToElements() { (addrBuffPtr : UnsafePointer<CChar>) -> () in
      // use pointer to memory
      var x0 = addrBuffPtr.memory
      println("The ASCII value at index 0 is \(x0)")

      var myCOpaquePointer = COpaquePointer(addrBuffPtr)
      // use C pointer for interoperation calls
}
+6

hotpaw2 Swift 4

var addressBuffer = [Int8](repeating:0, count:100)
+2

Source: https://habr.com/ru/post/1545399/


All Articles