Answers for "enum to string swift"

1

enum string swift example

enum Alert: CustomStringConvertible {
    case DuplicateUserNameMessage(userName: String)
    case NetworkFailureMessage(url: String)
    case OverheatingMessage(degrees: Float)
    var description: String {
        switch self {
        case .DuplicateUserNameMessage(let userName): 
          return "The username \(userName) already exists"
        case .NetworkFailureMessage(let url): 
          return "Unable to connect to \(url)"
        case .OverheatingMessage(let degrees):
          return String(format: "Warning: The water is %.1f degrees", degrees)
        }
    }
}

let userName = "Mike"
let url = "www.google.com"
let degrees: Float = 103.4587254

print(Alert.DuplicateUserNameMessage(userName: userName).description)
print(Alert.NetworkFailureMessage(url: url).description)
print(Alert.OverheatingMessage(degrees: degrees).description)
Posted by: Guest on May-11-2021
0

Converting Enum to String and Strint to Enum

private enum Status
{
    Awesome = 1,
    Cool = 2
};

public string ConvertEnumToString(Status status)
{
   return status.ToString();
}

public Status ConvertStringToEnum(string status)
{
    return (Status) Enum.Parse(typeof (Status), status);
}

//serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

//Fast enum
public enum HumanStates
{
	Idle,
    Working,
    Sleeping,
    Eating,
    Dead
     
}

public string FastToString(HumanStates state)
{
	switch(state)
    {
      case HumanStates.Idle:
        	return nameof(HumanStates.Idle);
      case HumanStates.Working:
        	return nameof(HumanStates.Working);
      case HumanStates.Sleeping:
        	return nameof(HumanStates.Sleeping);
      case HumanStates.Eating:
        	return nameof(HumanStates.Eating);
      case HumanStates.Dead:
        	return nameof(HumanStates.Dead);
      default:
        throw new ArgumentOutOfRangeException(nameof(state), state, null);        
    }
}
Posted by: Guest on August-24-2021

Code answers related to "Swift"

Browse Popular Code Answers by Language