9

Here is my C code in an Objective-C method

char addressBuffer[100];

But how to define this char in Swift language?

I try something like this but this doesn't work:

var addressBuffer : CChar(100)

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

11
  • 3
    use String instead. If you need a char[100], you should use the C language instead. I don't see a reason for Swift-Code to use a char[100] type Commented Jun 20, 2014 at 15:54
  • Recall: "Objective-C with out the C" Commented Jun 20, 2014 at 15:56
  • 1
    I use netServiceDidResolveAddress delegate and in this method I use sockets which are implemented in C language. socket->C language Commented Jun 20, 2014 at 15:56
  • 1
    @Zaph I hope Objective-C will not go away. I like it much more than Swift. Maybe I should become a Cobol programmer instead, or write Android apps... No, seriously, I don't like Swift... I like private methods, a non-managed language and I like to know how exactly my code is translated to assembler. It would be much nicer if ObjC were just extended with value types and generics. No need to create a new language. Commented Jun 20, 2014 at 16:10
  • 1
    @Michael I see I am loosing much with Swift and that there are to many optional shortcuts that are definitely affect readability. Every devs are going to come up with a bewildering variety of coding styles, a "Code of Babble". I am already seeing operator over-loadings that just shouldn't happen, the worst of C++ has arrived. Commented Jun 20, 2014 at 16:36

3 Answers 3

12

that is the way to get a nice unicode character array in Swfit:

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

if fills your array with 100 question marks, initially.

update

with CChar, for instance:

var charArray: Array<CChar> = Array(count: 100, repeatedValue: 32) // 32 is the ascii space
Sign up to request clarification or add additional context in comments.

3 Comments

Okey maybe it will be good but how to define a char array (100) with CChar type?
what's the difference between them?
@AlexanderSupertramp, the CChar is literally an alias for Int8 in Swift.
7

This Swift code seems to allocate something usable as a C char array of size 100 (for C interoperation needs):

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
}

3 Comments

Did you find some problem with the documentation?
C signed char variables (if you need them for interoperation with some existing C library using type char) aren't for use with unicode data.
6

hotpaw2's answer in Swift 4+

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.