C-style struct in Objc not importing to Swift
Asked Answered
C

0

6

The problem I'm encountering, and absolutely not understanding, is why can my Swift code not access an bridged C-style struct import from objective-c when a NSString pointer is contained, whereas the struct is visible when then NSString is replaced with a char *.

For example, when I've got the following defined in my Foo.h file:

typedef struct {
    NSString* value;
    SomeEnum unit;
} SomeMeasurement;

I get an error of Value of Type ... has no member Measurement (from the parental class) at compile time.

Whereas when my struct is the following:

typedef struct {
    char* value;
    SomeEnum unit;
} SomeMeasurement;

Things compile as expected (even if char unusable for my current needs). The fact that that small change allows for this struct to be "seen" by Swift is totally perplexing me, since it means it's (probably) not an issue with a file in bridging header or something of that sort.

Also, if I replace NSString with NSMutableData, the problem persists, and the struct cannot be seen.

Cavil answered 23/11, 2020 at 10:59 Comment(6)
did you import Foundation ?Department
Good point, but yes. Foundation is imported when I'm defining this struct in Foo.h and in my destination Bar.swiftCavil
you know that you are conflicting @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) public struct Measurement<UnitType>Department
@OlSen well spotted, but these names are all obfuscated versions of my code. I updated my question to reflect that better.Cavil
The problem you have is definitely not from NSString* value inside C struct. Structs in Swift are handled differently and are not compatible as is. The documentation talks only about similar syntax, not about about generated converting C structs to swift structs.Department
My question is "Why is one typedef visible from swift and the other one isn't".Cavil

© 2022 - 2024 — McMap. All rights reserved.