0

I have an array of Strings in Swift declared like this:

var DataStreamBuffer : [String] = {return []}()

Appending a new element in Swift, it's easy. I just do:

DataStreamBuffer.append(new_string)

However, I need to append a new element to DataStreamBuffer from a C file. What should be the right procedure in my C code? And what should I do in Swift so DataStreamBuffer is visible from C (do I have to declare anything in the Bridging-Header.h?)

Duarte
  • 127
  • 4
  • 14

1 Answers1

0

If you by any chance mean Objective-C, then this might help:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID122

I haven't played with it myself yet, so I'm not sure if any Objective-C code can call Swift. If that is the case, then any C code in your project can call your Swift code, because any C code is valid Objective-C (i.e. Objective-C is a superset of C). The reverse is not true, however.

Another approach is to have a C function that returns a string that you want to append to the Swift string array. The C function is called from Swift code, which gets the string and actually appends it to the Swift array. You would use a bridging header to make the C function visible to Swift.

A variation of this approach would be to declare a callback function in your C code, import the declaration into Swift using a bridging header, and implement the callback in Swift. This is a more difficult approach, but would come in handy if this part of the app logic is controlled from the C code.

If you provide a more detailed context of what you are doing, people might give you more specific advice or even some examples.

Anatoli P
  • 4,791
  • 1
  • 18
  • 22