To get a string description of the enum value, I can usually just print it out as shown in this answer.
enum CustomEnum {
case normal
case special
}
let customEnum = CustomEnum.normal
print("Custom enum is: (customEnum)")
Result:
But, this doesn't work for system enums, for example UIView.ContentMode
. It just prints out the name of the enum itself.
let imageView = UIImageView()
let contentMode = imageView.contentMode
print("ContentMode is: (contentMode)")
Result:
I tried making a String
explicitly like this:
let description = String(describing: contentMode)
print("Description is: (description)")
Which doesn't have any effect:
Is it only possible to print the value by switching over all the possible values?
switch contentMode {
case .scaleToFill:
print("mode is scaleToFill")
case .scaleAspectFit:
print("mode is scaleAspectFit")
case .scaleAspectFill:
print("mode is scaleAspectFill")
case .redraw:
print("mode is redraw")
case .center:
print("mode is center")
case .top:
print("mode is top")
case .bottom:
print("mode is bottom")
case .left:
print("mode is left")
case .right:
print("mode is right")
case .topLeft:
print("mode is topLeft")
case .topRight:
print("mode is topRight")
case .bottomLeft:
print("mode is bottomLeft")
case .bottomRight:
print("mode is bottomRight")
@unknown default:
print("default")
}
This finally gives me the result I want:
But this is really tedious, and I don't want to do this for every system enum if I can avoid it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…