Swift print() printing name of the type instead of the enum associated value
I'm printing a couple of enums in delegate methods for WebRTC. Comparing them or using switch statements works fine, but one of the enums just prints its type name instead of associated value:
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCPeerConnectionState) {
// I don't know why this only prints the type name and not the associated value
print ("peerConnectionState (newState)")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) {
print("ICEConnectionState: (newState)")
}
This prints
peerConnectionState: RTCPeerConnectionState
ICEConnectionState: connected
The RTCPeerConnectionState never prints its associated value, even though if I check its rawValue, it's a valid member of the enum. Both of them are defined in PeerConnectionState.h as below:
/** Represents the ice connection state of the peer connection. */
typedef NS_ENUM(NSInteger, RTCIceConnectionState) {
RTCIceConnectionStateNew,
RTCIceConnectionStateChecking,
RTCIceConnectionStateConnected,
RTCIceConnectionStateCompleted,
RTCIceConnectionStateFailed,
RTCIceConnectionStateDisconnected,
RTCIceConnectionStateClosed,
RTCIceConnectionStateCount,
};
/** Represents the combined ice+dtls connection state of the peer connection. */
typedef NS_ENUM(NSInteger, RTCPeerConnectionState) {
RTCPeerConnectionStateNew,
RTCPeerConnectionStateConnecting,
RTCPeerConnectionStateConnected,
RTCPeerConnectionStateDisconnected,
RTCPeerConnectionStateFailed,
RTCPeerConnectionStateClosed,
};
I don't know a whole lot about ObjC-Swift bridging, it all seems to "just work". What's the difference between the two enums that only seem to affect print()?
objective-c swift enums webrtc
add a comment |
I'm printing a couple of enums in delegate methods for WebRTC. Comparing them or using switch statements works fine, but one of the enums just prints its type name instead of associated value:
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCPeerConnectionState) {
// I don't know why this only prints the type name and not the associated value
print ("peerConnectionState (newState)")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) {
print("ICEConnectionState: (newState)")
}
This prints
peerConnectionState: RTCPeerConnectionState
ICEConnectionState: connected
The RTCPeerConnectionState never prints its associated value, even though if I check its rawValue, it's a valid member of the enum. Both of them are defined in PeerConnectionState.h as below:
/** Represents the ice connection state of the peer connection. */
typedef NS_ENUM(NSInteger, RTCIceConnectionState) {
RTCIceConnectionStateNew,
RTCIceConnectionStateChecking,
RTCIceConnectionStateConnected,
RTCIceConnectionStateCompleted,
RTCIceConnectionStateFailed,
RTCIceConnectionStateDisconnected,
RTCIceConnectionStateClosed,
RTCIceConnectionStateCount,
};
/** Represents the combined ice+dtls connection state of the peer connection. */
typedef NS_ENUM(NSInteger, RTCPeerConnectionState) {
RTCPeerConnectionStateNew,
RTCPeerConnectionStateConnecting,
RTCPeerConnectionStateConnected,
RTCPeerConnectionStateDisconnected,
RTCPeerConnectionStateFailed,
RTCPeerConnectionStateClosed,
};
I don't know a whole lot about ObjC-Swift bridging, it all seems to "just work". What's the difference between the two enums that only seem to affect print()?
objective-c swift enums webrtc
1
I don't think the ObjC code keeps this meta data. I think you have to manually extend these enums with conformance toCustomStringDescribableadd a computed propertydescription, which uses a switch on the enum value to return the correct (hard coded string) case name.
– Alexander
Nov 23 '18 at 18:22
Apparently, if you define your enums in Objective C with NS_ENUM and name them just right, LLVM makes them available in Swift 'for free' stackoverflow.com/questions/24872475/…. Both enums above are working in this manner, Xcode even auto generates switch blocks with all the enum values, the problem seems confined to print()
– p10ben
Nov 25 '18 at 16:02
Whoops - you were right Alexander, there was a file in the sample code I'd started with that has exactly what you describe, and it was missing the class in question. I was mistaking Swift bridging the enum labels for description strings.
– p10ben
Dec 9 '18 at 3:41
add a comment |
I'm printing a couple of enums in delegate methods for WebRTC. Comparing them or using switch statements works fine, but one of the enums just prints its type name instead of associated value:
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCPeerConnectionState) {
// I don't know why this only prints the type name and not the associated value
print ("peerConnectionState (newState)")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) {
print("ICEConnectionState: (newState)")
}
This prints
peerConnectionState: RTCPeerConnectionState
ICEConnectionState: connected
The RTCPeerConnectionState never prints its associated value, even though if I check its rawValue, it's a valid member of the enum. Both of them are defined in PeerConnectionState.h as below:
/** Represents the ice connection state of the peer connection. */
typedef NS_ENUM(NSInteger, RTCIceConnectionState) {
RTCIceConnectionStateNew,
RTCIceConnectionStateChecking,
RTCIceConnectionStateConnected,
RTCIceConnectionStateCompleted,
RTCIceConnectionStateFailed,
RTCIceConnectionStateDisconnected,
RTCIceConnectionStateClosed,
RTCIceConnectionStateCount,
};
/** Represents the combined ice+dtls connection state of the peer connection. */
typedef NS_ENUM(NSInteger, RTCPeerConnectionState) {
RTCPeerConnectionStateNew,
RTCPeerConnectionStateConnecting,
RTCPeerConnectionStateConnected,
RTCPeerConnectionStateDisconnected,
RTCPeerConnectionStateFailed,
RTCPeerConnectionStateClosed,
};
I don't know a whole lot about ObjC-Swift bridging, it all seems to "just work". What's the difference between the two enums that only seem to affect print()?
objective-c swift enums webrtc
I'm printing a couple of enums in delegate methods for WebRTC. Comparing them or using switch statements works fine, but one of the enums just prints its type name instead of associated value:
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCPeerConnectionState) {
// I don't know why this only prints the type name and not the associated value
print ("peerConnectionState (newState)")
}
func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) {
print("ICEConnectionState: (newState)")
}
This prints
peerConnectionState: RTCPeerConnectionState
ICEConnectionState: connected
The RTCPeerConnectionState never prints its associated value, even though if I check its rawValue, it's a valid member of the enum. Both of them are defined in PeerConnectionState.h as below:
/** Represents the ice connection state of the peer connection. */
typedef NS_ENUM(NSInteger, RTCIceConnectionState) {
RTCIceConnectionStateNew,
RTCIceConnectionStateChecking,
RTCIceConnectionStateConnected,
RTCIceConnectionStateCompleted,
RTCIceConnectionStateFailed,
RTCIceConnectionStateDisconnected,
RTCIceConnectionStateClosed,
RTCIceConnectionStateCount,
};
/** Represents the combined ice+dtls connection state of the peer connection. */
typedef NS_ENUM(NSInteger, RTCPeerConnectionState) {
RTCPeerConnectionStateNew,
RTCPeerConnectionStateConnecting,
RTCPeerConnectionStateConnected,
RTCPeerConnectionStateDisconnected,
RTCPeerConnectionStateFailed,
RTCPeerConnectionStateClosed,
};
I don't know a whole lot about ObjC-Swift bridging, it all seems to "just work". What's the difference between the two enums that only seem to affect print()?
objective-c swift enums webrtc
objective-c swift enums webrtc
asked Nov 23 '18 at 18:12
p10benp10ben
1841314
1841314
1
I don't think the ObjC code keeps this meta data. I think you have to manually extend these enums with conformance toCustomStringDescribableadd a computed propertydescription, which uses a switch on the enum value to return the correct (hard coded string) case name.
– Alexander
Nov 23 '18 at 18:22
Apparently, if you define your enums in Objective C with NS_ENUM and name them just right, LLVM makes them available in Swift 'for free' stackoverflow.com/questions/24872475/…. Both enums above are working in this manner, Xcode even auto generates switch blocks with all the enum values, the problem seems confined to print()
– p10ben
Nov 25 '18 at 16:02
Whoops - you were right Alexander, there was a file in the sample code I'd started with that has exactly what you describe, and it was missing the class in question. I was mistaking Swift bridging the enum labels for description strings.
– p10ben
Dec 9 '18 at 3:41
add a comment |
1
I don't think the ObjC code keeps this meta data. I think you have to manually extend these enums with conformance toCustomStringDescribableadd a computed propertydescription, which uses a switch on the enum value to return the correct (hard coded string) case name.
– Alexander
Nov 23 '18 at 18:22
Apparently, if you define your enums in Objective C with NS_ENUM and name them just right, LLVM makes them available in Swift 'for free' stackoverflow.com/questions/24872475/…. Both enums above are working in this manner, Xcode even auto generates switch blocks with all the enum values, the problem seems confined to print()
– p10ben
Nov 25 '18 at 16:02
Whoops - you were right Alexander, there was a file in the sample code I'd started with that has exactly what you describe, and it was missing the class in question. I was mistaking Swift bridging the enum labels for description strings.
– p10ben
Dec 9 '18 at 3:41
1
1
I don't think the ObjC code keeps this meta data. I think you have to manually extend these enums with conformance to
CustomStringDescribable add a computed property description, which uses a switch on the enum value to return the correct (hard coded string) case name.– Alexander
Nov 23 '18 at 18:22
I don't think the ObjC code keeps this meta data. I think you have to manually extend these enums with conformance to
CustomStringDescribable add a computed property description, which uses a switch on the enum value to return the correct (hard coded string) case name.– Alexander
Nov 23 '18 at 18:22
Apparently, if you define your enums in Objective C with NS_ENUM and name them just right, LLVM makes them available in Swift 'for free' stackoverflow.com/questions/24872475/…. Both enums above are working in this manner, Xcode even auto generates switch blocks with all the enum values, the problem seems confined to print()
– p10ben
Nov 25 '18 at 16:02
Apparently, if you define your enums in Objective C with NS_ENUM and name them just right, LLVM makes them available in Swift 'for free' stackoverflow.com/questions/24872475/…. Both enums above are working in this manner, Xcode even auto generates switch blocks with all the enum values, the problem seems confined to print()
– p10ben
Nov 25 '18 at 16:02
Whoops - you were right Alexander, there was a file in the sample code I'd started with that has exactly what you describe, and it was missing the class in question. I was mistaking Swift bridging the enum labels for description strings.
– p10ben
Dec 9 '18 at 3:41
Whoops - you were right Alexander, there was a file in the sample code I'd started with that has exactly what you describe, and it was missing the class in question. I was mistaking Swift bridging the enum labels for description strings.
– p10ben
Dec 9 '18 at 3:41
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451313%2fswift-print-printing-name-of-the-type-instead-of-the-enum-associated-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451313%2fswift-print-printing-name-of-the-type-instead-of-the-enum-associated-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
I don't think the ObjC code keeps this meta data. I think you have to manually extend these enums with conformance to
CustomStringDescribableadd a computed propertydescription, which uses a switch on the enum value to return the correct (hard coded string) case name.– Alexander
Nov 23 '18 at 18:22
Apparently, if you define your enums in Objective C with NS_ENUM and name them just right, LLVM makes them available in Swift 'for free' stackoverflow.com/questions/24872475/…. Both enums above are working in this manner, Xcode even auto generates switch blocks with all the enum values, the problem seems confined to print()
– p10ben
Nov 25 '18 at 16:02
Whoops - you were right Alexander, there was a file in the sample code I'd started with that has exactly what you describe, and it was missing the class in question. I was mistaking Swift bridging the enum labels for description strings.
– p10ben
Dec 9 '18 at 3:41