[Swift-user] Treatment of booleans in Swift

Mihael Hategan hategan at mcs.anl.gov
Mon May 19 18:29:17 CDT 2014


On Mon, 2014-05-19 at 23:22 +0000, Bronevetsky, Greg wrote:
> I've been working with Booleans recently and I've run into a couple of issues with them that might be a problem for others:
> 
> -          tracef doesn't have a format option for Booleans, forcing
> me to convert them to strings. Running @toInt on a Boolean produces:
> "java.lang.NumberFormatException: For input string: "false"".

I think the documentation is lying (by omission) about that. Try "%b".

> 
> -          It is not clear how to format files to be read by
> readData() if the data in question is a Boolean. Based on
> experimentation I discovered that the correct strings are
> "True"/"False" rather than "true"/"false" or "0"/"1". It would be
> useful to document this in the description of readData().

All of "True" and "true" and "TRUE" and "tRuE" (and all other case
combinations) should work. The implementation uses Java's
Boolean.valueOf(String), which calls this:
    private static boolean toBoolean(String name) {
        return ((name != null) && name.equalsIgnoreCase("true"));
    }

Mihael




More information about the Swift-user mailing list