I'm trying to convert an input text, for example: "São Paulo" into an acronym "SP" within Zapier. I'm using Code by Zapier - Run Javascript.
The Code I'm using is:
var ConverterEstados = function(inputData) {
var data;
switch (inputData) {
/* Estados */
case "Acre" : data = "AC"; break;
case "Alagoas" : data = "AL"; break;
case "Amazonas" : data = "AM"; break;
case "Amapá" : data = "AP"; break;
case "Bahia" : data = "BA"; break;
case "Ceará" : data = "CE"; break;
case "Distrito Federal" : data = "DF"; break;
case "Espírito Santo" : data = "ES"; break;
case "Goiás" : data = "GO"; break;
case "Maranhão" : data = "MA"; break;
case "Minas Gerais" : data = "MG"; break;
case "Mato Grosso Do Sul" : data = "MS"; break;
case "Mato Grosso" : data = "MT"; break;
case "Pará" : data = "PA"; break;
case "Paraíba" : data = "PB"; break;
case "Pernambuco" : data = "PE"; break;
case "Piauí" : data = "PI"; break;
case "Paraná" : data = "PR"; break;
case "Rio De Janeiro" : data = "RJ"; break;
case "Rio Grande Do Norte" : data = "RN"; break;
case "Rondônia" : data = "RO"; break;
case "Roraima" : data = "RR"; break;
case "Rio Grande Do Sul" : data = "RS"; break;
case "Santa Catarina" : data = "SC"; break;
case "Sergipe" : data = "SE"; break;
case "São Paulo" : data = "SP"; break;
case "Tocatíns" : data = "TO"; break;
}
return data;
};
I need this code to return two letters for example "SP", it must be returned inside the output variable, which is an object.
Zapier gives an example of output
output = {id: 1, hello: "world"};
Could anyone help?
To fix your existing code, you need only return an object (aka wrapped in {} from the whole text box:
// your function here
// ...
// ...
return {twoLetterCode: ConverterEstados(inputData)}
It's also worth noting you can simplify your function by using an object instead:
const ConverterEstados = {
"Acre": "AC",
"Alagoas": "AL",
// ...
}
return {twoLetterCode: ConverterEstados[inputData.state]}
In that last part, i'm assuming your input has mapped in a variable called state, but it can be named whatever you want.
Related
If you look at this nice list of preinstalled fonts that Apple compiled, you'll notice 2 things missing.
iOS and macOS System Fonts
Visual representation of the font. (would be nice, Apple)
More importantly, the actual font name that you need to use.
As an example, in Xcode/Swift if I want to use Apple SD Gothic Neo Heavy I can't use:
contactNameLabel.font = UIFont(name: "Apple SD Gothic Neo Heavy", size: 13)
I would have to use:
contactNameLabel.font = UIFont(name: "AppleSDGothicNeo-Heavy", size: 13)
Just wanted to see if anyone has a link that shows these names, or otherwise, what the best practice is for easily finding them.
I apologize that my question is not strictly code related. This is my first Stack Overflow question, and I wish it could be more technical, but I think the answer to this could be helpful to people, including myself.
Thanks all.
You can print out all fonts and families available using this code:
for family in UIFont.familyNames {
print("family:", family)
for font in UIFont.fontNames(forFamilyName: family) {
print("font:", font)
}
}
By modifying the print statements using the for loops in the other answers, I made an extension of UIFont that allows you to access the font name string using the rawValue of an enum.
let myNewFont = UIFont(name: UIFont.nameOf.Thonburi_Light.rawValue, size: 30)!
extension UIFont {
public enum nameOf: String {
case Copperplate_Light = "Copperplate-Light"
case Copperplate = "Copperplate"
case Copperplate_Bold = "Copperplate-Bold"
case AppleSDGothicNeo_Thin = "AppleSDGothicNeo-Thin"
case AppleSDGothicNeo_Light = "AppleSDGothicNeo-Light"
case AppleSDGothicNeo_Regular = "AppleSDGothicNeo-Regular"
case AppleSDGothicNeo_Bold = "AppleSDGothicNeo-Bold"
case AppleSDGothicNeo_SemiBold = "AppleSDGothicNeo-SemiBold"
case AppleSDGothicNeo_UltraLight = "AppleSDGothicNeo-UltraLight"
case AppleSDGothicNeo_Medium = "AppleSDGothicNeo-Medium"
case Thonburi = "Thonburi"
case Thonburi_Light = "Thonburi-Light"
case Thonburi_Bold = "Thonburi-Bold"
case GillSans_Italic = "GillSans-Italic"
case GillSans_SemiBold = "GillSans-SemiBold"
case GillSans_UltraBold = "GillSans-UltraBold"
case GillSans_Light = "GillSans-Light"
case GillSans_Bold = "GillSans-Bold"
case GillSans = "GillSans"
case GillSans_SemiBoldItalic = "GillSans-SemiBoldItalic"
case GillSans_BoldItalic = "GillSans-BoldItalic"
case GillSans_LightItalic = "GillSans-LightItalic"
case MarkerFelt_Thin = "MarkerFelt-Thin"
case MarkerFelt_Wide = "MarkerFelt-Wide"
case HiraMaruProN_W4 = "HiraMaruProN-W4"
case CourierNewPS_ItalicMT = "CourierNewPS-ItalicMT"
case CourierNewPSMT = "CourierNewPSMT"
case CourierNewPS_BoldItalicMT = "CourierNewPS-BoldItalicMT"
case CourierNewPS_BoldMT = "CourierNewPS-BoldMT"
case KohinoorTelugu_Regular = "KohinoorTelugu-Regular"
case KohinoorTelugu_Medium = "KohinoorTelugu-Medium"
case KohinoorTelugu_Light = "KohinoorTelugu-Light"
case AvenirNextCondensed_Heavy = "AvenirNextCondensed-Heavy"
case AvenirNextCondensed_MediumItalic = "AvenirNextCondensed-MediumItalic"
case AvenirNextCondensed_Regular = "AvenirNextCondensed-Regular"
case AvenirNextCondensed_UltraLightItalic = "AvenirNextCondensed-UltraLightItalic"
case AvenirNextCondensed_Medium = "AvenirNextCondensed-Medium"
case AvenirNextCondensed_HeavyItalic = "AvenirNextCondensed-HeavyItalic"
case AvenirNextCondensed_DemiBoldItalic = "AvenirNextCondensed-DemiBoldItalic"
case AvenirNextCondensed_Bold = "AvenirNextCondensed-Bold"
case AvenirNextCondensed_DemiBold = "AvenirNextCondensed-DemiBold"
case AvenirNextCondensed_BoldItalic = "AvenirNextCondensed-BoldItalic"
case AvenirNextCondensed_Italic = "AvenirNextCondensed-Italic"
case AvenirNextCondensed_UltraLight = "AvenirNextCondensed-UltraLight"
case TamilSangamMN = "TamilSangamMN"
case TamilSangamMN_Bold = "TamilSangamMN-Bold"
case HelveticaNeue_UltraLightItalic = "HelveticaNeue-UltraLightItalic"
case HelveticaNeue_Medium = "HelveticaNeue-Medium"
case HelveticaNeue_MediumItalic = "HelveticaNeue-MediumItalic"
case HelveticaNeue_UltraLight = "HelveticaNeue-UltraLight"
case HelveticaNeue_Italic = "HelveticaNeue-Italic"
case HelveticaNeue_Light = "HelveticaNeue-Light"
case HelveticaNeue_ThinItalic = "HelveticaNeue-ThinItalic"
case HelveticaNeue_LightItalic = "HelveticaNeue-LightItalic"
case HelveticaNeue_Bold = "HelveticaNeue-Bold"
case HelveticaNeue_Thin = "HelveticaNeue-Thin"
case HelveticaNeue_CondensedBlack = "HelveticaNeue-CondensedBlack"
case HelveticaNeue = "HelveticaNeue"
case HelveticaNeue_CondensedBold = "HelveticaNeue-CondensedBold"
case HelveticaNeue_BoldItalic = "HelveticaNeue-BoldItalic"
case TimesNewRomanPS_ItalicMT = "TimesNewRomanPS-ItalicMT"
case TimesNewRomanPS_BoldItalicMT = "TimesNewRomanPS-BoldItalicMT"
case TimesNewRomanPS_BoldMT = "TimesNewRomanPS-BoldMT"
case TimesNewRomanPSMT = "TimesNewRomanPSMT"
case Georgia_BoldItalic = "Georgia-BoldItalic"
case Georgia_Italic = "Georgia-Italic"
case Georgia = "Georgia"
case Georgia_Bold = "Georgia-Bold"
case SinhalaSangamMN_Bold = "SinhalaSangamMN-Bold"
case SinhalaSangamMN = "SinhalaSangamMN"
case ArialRoundedMTBold = "ArialRoundedMTBold"
case Kailasa_Bold = "Kailasa-Bold"
case Kailasa = "Kailasa"
case KohinoorDevanagari_Regular = "KohinoorDevanagari-Regular"
case KohinoorDevanagari_Light = "KohinoorDevanagari-Light"
case KohinoorDevanagari_Semibold = "KohinoorDevanagari-Semibold"
case KohinoorBangla_Regular = "KohinoorBangla-Regular"
case KohinoorBangla_Semibold = "KohinoorBangla-Semibold"
case KohinoorBangla_Light = "KohinoorBangla-Light"
case NotoSansOriya_Bold = "NotoSansOriya-Bold"
case NotoSansOriya = "NotoSansOriya"
case ChalkboardSE_Bold = "ChalkboardSE-Bold"
case ChalkboardSE_Light = "ChalkboardSE-Light"
case ChalkboardSE_Regular = "ChalkboardSE-Regular"
case NotoSansKannada_Bold = "NotoSansKannada-Bold"
case NotoSansKannada_Light = "NotoSansKannada-Light"
case NotoSansKannada_Regular = "NotoSansKannada-Regular"
case AppleColorEmoji = "AppleColorEmoji"
case PingFangTC_Regular = "PingFangTC-Regular"
case PingFangTC_Thin = "PingFangTC-Thin"
case PingFangTC_Medium = "PingFangTC-Medium"
case PingFangTC_Semibold = "PingFangTC-Semibold"
case PingFangTC_Light = "PingFangTC-Light"
case PingFangTC_Ultralight = "PingFangTC-Ultralight"
case GeezaPro_Bold = "GeezaPro-Bold"
case GeezaPro = "GeezaPro"
case DamascusBold = "DamascusBold"
case DamascusLight = "DamascusLight"
case Damascus = "Damascus"
case DamascusMedium = "DamascusMedium"
case DamascusSemiBold = "DamascusSemiBold"
case Noteworthy_Bold = "Noteworthy-Bold"
case Noteworthy_Light = "Noteworthy-Light"
case Avenir_Oblique = "Avenir-Oblique"
case Avenir_HeavyOblique = "Avenir-HeavyOblique"
case Avenir_Heavy = "Avenir-Heavy"
case Avenir_BlackOblique = "Avenir-BlackOblique"
case Avenir_BookOblique = "Avenir-BookOblique"
case Avenir_Roman = "Avenir-Roman"
case Avenir_Medium = "Avenir-Medium"
case Avenir_Black = "Avenir-Black"
case Avenir_Light = "Avenir-Light"
case Avenir_MediumOblique = "Avenir-MediumOblique"
case Avenir_Book = "Avenir-Book"
case Avenir_LightOblique = "Avenir-LightOblique"
case KohinoorGujarati_Light = "KohinoorGujarati-Light"
case KohinoorGujarati_Bold = "KohinoorGujarati-Bold"
case KohinoorGujarati_Regular = "KohinoorGujarati-Regular"
case DiwanMishafi = "DiwanMishafi"
case AcademyEngravedLetPlain = "AcademyEngravedLetPlain"
case PartyLetPlain = "PartyLetPlain"
case Futura_CondensedExtraBold = "Futura-CondensedExtraBold"
case Futura_Medium = "Futura-Medium"
case Futura_Bold = "Futura-Bold"
case Futura_CondensedMedium = "Futura-CondensedMedium"
case Futura_MediumItalic = "Futura-MediumItalic"
case ArialHebrew_Bold = "ArialHebrew-Bold"
case ArialHebrew_Light = "ArialHebrew-Light"
case ArialHebrew = "ArialHebrew"
case Farah = "Farah"
case MuktaMahee_Light = "MuktaMahee-Light"
case MuktaMahee_Bold = "MuktaMahee-Bold"
case MuktaMahee_Regular = "MuktaMahee-Regular"
case NotoSansMyanmar_Regular = "NotoSansMyanmar-Regular"
case NotoSansMyanmar_Bold = "NotoSansMyanmar-Bold"
case NotoSansMyanmar_Light = "NotoSansMyanmar-Light"
case Arial_BoldMT = "Arial-BoldMT"
case Arial_BoldItalicMT = "Arial-BoldItalicMT"
case Arial_ItalicMT = "Arial-ItalicMT"
case ArialMT = "ArialMT"
case Chalkduster = "Chalkduster"
case Kefa_Regular = "Kefa-Regular"
case HoeflerText_Italic = "HoeflerText-Italic"
case HoeflerText_Black = "HoeflerText-Black"
case HoeflerText_Regular = "HoeflerText-Regular"
case HoeflerText_BlackItalic = "HoeflerText-BlackItalic"
case Optima_ExtraBlack = "Optima-ExtraBlack"
case Optima_BoldItalic = "Optima-BoldItalic"
case Optima_Italic = "Optima-Italic"
case Optima_Regular = "Optima-Regular"
case Optima_Bold = "Optima-Bold"
case Galvji_Bold = "Galvji-Bold"
case Galvji = "Galvji"
case Palatino_Italic = "Palatino-Italic"
case Palatino_Roman = "Palatino-Roman"
case Palatino_BoldItalic = "Palatino-BoldItalic"
case Palatino_Bold = "Palatino-Bold"
case MalayalamSangamMN_Bold = "MalayalamSangamMN-Bold"
case MalayalamSangamMN = "MalayalamSangamMN"
case AlNile = "AlNile"
case AlNile_Bold = "AlNile-Bold"
case LaoSangamMN = "LaoSangamMN"
case BradleyHandITCTT_Bold = "BradleyHandITCTT-Bold"
case HiraMinProN_W3 = "HiraMinProN-W3"
case HiraMinProN_W6 = "HiraMinProN-W6"
case PingFangHK_Medium = "PingFangHK-Medium"
case PingFangHK_Thin = "PingFangHK-Thin"
case PingFangHK_Regular = "PingFangHK-Regular"
case PingFangHK_Ultralight = "PingFangHK-Ultralight"
case PingFangHK_Semibold = "PingFangHK-Semibold"
case PingFangHK_Light = "PingFangHK-Light"
case Helvetica_Oblique = "Helvetica-Oblique"
case Helvetica_BoldOblique = "Helvetica-BoldOblique"
case Helvetica = "Helvetica"
case Helvetica_Light = "Helvetica-Light"
case Helvetica_Bold = "Helvetica-Bold"
case Helvetica_LightOblique = "Helvetica-LightOblique"
case Courier_BoldOblique = "Courier-BoldOblique"
case Courier_Oblique = "Courier-Oblique"
case Courier = "Courier"
case Courier_Bold = "Courier-Bold"
case Cochin_Italic = "Cochin-Italic"
case Cochin_Bold = "Cochin-Bold"
case Cochin = "Cochin"
case Cochin_BoldItalic = "Cochin-BoldItalic"
case TrebuchetMS_Bold = "TrebuchetMS-Bold"
case TrebuchetMS_Italic = "TrebuchetMS-Italic"
case Trebuchet_BoldItalic = "Trebuchet-BoldItalic"
case TrebuchetMS = "TrebuchetMS"
case DevanagariSangamMN = "DevanagariSangamMN"
case DevanagariSangamMN_Bold = "DevanagariSangamMN-Bold"
case Rockwell_Italic = "Rockwell-Italic"
case Rockwell_Regular = "Rockwell-Regular"
case Rockwell_Bold = "Rockwell-Bold"
case Rockwell_BoldItalic = "Rockwell-BoldItalic"
case SnellRoundhand = "SnellRoundhand"
case SnellRoundhand_Bold = "SnellRoundhand-Bold"
case SnellRoundhand_Black = "SnellRoundhand-Black"
case ZapfDingbatsITC = "ZapfDingbatsITC"
case BodoniSvtyTwoITCTT_Bold = "BodoniSvtyTwoITCTT-Bold"
case BodoniSvtyTwoITCTT_BookIta = "BodoniSvtyTwoITCTT-BookIta"
case BodoniSvtyTwoITCTT_Book = "BodoniSvtyTwoITCTT-Book"
case Verdana_Italic = "Verdana-Italic"
case Verdana = "Verdana"
case Verdana_Bold = "Verdana-Bold"
case Verdana_BoldItalic = "Verdana-BoldItalic"
case AmericanTypewriter_CondensedBold = "AmericanTypewriter-CondensedBold"
case AmericanTypewriter_Condensed = "AmericanTypewriter-Condensed"
case AmericanTypewriter_CondensedLight = "AmericanTypewriter-CondensedLight"
case AmericanTypewriter = "AmericanTypewriter"
case AmericanTypewriter_Bold = "AmericanTypewriter-Bold"
case AmericanTypewriter_Semibold = "AmericanTypewriter-Semibold"
case AmericanTypewriter_Light = "AmericanTypewriter-Light"
case AvenirNext_Medium = "AvenirNext-Medium"
case AvenirNext_DemiBoldItalic = "AvenirNext-DemiBoldItalic"
case AvenirNext_DemiBold = "AvenirNext-DemiBold"
case AvenirNext_HeavyItalic = "AvenirNext-HeavyItalic"
case AvenirNext_Regular = "AvenirNext-Regular"
case AvenirNext_Italic = "AvenirNext-Italic"
case AvenirNext_MediumItalic = "AvenirNext-MediumItalic"
case AvenirNext_UltraLightItalic = "AvenirNext-UltraLightItalic"
case AvenirNext_BoldItalic = "AvenirNext-BoldItalic"
case AvenirNext_Heavy = "AvenirNext-Heavy"
case AvenirNext_Bold = "AvenirNext-Bold"
case AvenirNext_UltraLight = "AvenirNext-UltraLight"
case Baskerville_SemiBoldItalic = "Baskerville-SemiBoldItalic"
case Baskerville_SemiBold = "Baskerville-SemiBold"
case Baskerville_BoldItalic = "Baskerville-BoldItalic"
case Baskerville = "Baskerville"
case Baskerville_Bold = "Baskerville-Bold"
case Baskerville_Italic = "Baskerville-Italic"
case KhmerSangamMN = "KhmerSangamMN"
case Didot_Bold = "Didot-Bold"
case Didot = "Didot"
case Didot_Italic = "Didot-Italic"
case SavoyeLetPlain = "SavoyeLetPlain"
case BodoniOrnamentsITCTT = "BodoniOrnamentsITCTT"
case Symbol = "Symbol"
case Charter_BlackItalic = "Charter-BlackItalic"
case Charter_Bold = "Charter-Bold"
case Charter_Roman = "Charter-Roman"
case Charter_Black = "Charter-Black"
case Charter_BoldItalic = "Charter-BoldItalic"
case Charter_Italic = "Charter-Italic"
case Menlo_BoldItalic = "Menlo-BoldItalic"
case Menlo_Bold = "Menlo-Bold"
case Menlo_Italic = "Menlo-Italic"
case Menlo_Regular = "Menlo-Regular"
case NotoNastaliqUrdu = "NotoNastaliqUrdu"
case NotoNastaliqUrdu_Bold = "NotoNastaliqUrdu-Bold"
case BodoniSvtyTwoSCITCTT_Book = "BodoniSvtyTwoSCITCTT-Book"
case DINAlternate_Bold = "DINAlternate-Bold"
case Papyrus_Condensed = "Papyrus-Condensed"
case Papyrus = "Papyrus"
case HiraginoSans_W3 = "HiraginoSans-W3"
case HiraginoSans_W6 = "HiraginoSans-W6"
case HiraginoSans_W7 = "HiraginoSans-W7"
case PingFangSC_Medium = "PingFangSC-Medium"
case PingFangSC_Semibold = "PingFangSC-Semibold"
case PingFangSC_Light = "PingFangSC-Light"
case PingFangSC_Ultralight = "PingFangSC-Ultralight"
case PingFangSC_Regular = "PingFangSC-Regular"
case PingFangSC_Thin = "PingFangSC-Thin"
case MyanmarSangamMN = "MyanmarSangamMN"
case MyanmarSangamMN_Bold = "MyanmarSangamMN-Bold"
case AppleSymbols = "AppleSymbols"
case Zapfino = "Zapfino"
case BodoniSvtyTwoOSITCTT_BookIt = "BodoniSvtyTwoOSITCTT-BookIt"
case BodoniSvtyTwoOSITCTT_Book = "BodoniSvtyTwoOSITCTT-Book"
case BodoniSvtyTwoOSITCTT_Bold = "BodoniSvtyTwoOSITCTT-Bold"
case EuphemiaUCAS = "EuphemiaUCAS"
case EuphemiaUCAS_Italic = "EuphemiaUCAS-Italic"
case EuphemiaUCAS_Bold = "EuphemiaUCAS-Bold"
case DINCondensed_Bold = "DINCondensed-Bold"
}
}
Open Font Book application, find your font, select and switch to font info preview mode ( i button on topbar) and use the PostScript name of the font.
I did a Google search and found a list here. It seems to be what you are looking for, it has the visual representation, and should work with Xcode.
Try this to get all available iOS fonts
let familyNames = UIFont.familyNames
for family in familyNames {
print("Family name " + family)
let fontNames = UIFont.fontNames(forFamilyName: family)
for font in fontNames {
print(" Font name: " + font)
}
}
Using Xcode 9.4.1 and Swift 4.1
Having an enumeration with multiple cases from type Int, how can I print the case name by its rawValue?
public enum TestEnum : UInt16{
case ONE = 0x6E71
case TWO = 0x0002
case THREE = 0x0000
}
I am accessing the Enum by the rawValue:
print("\nCommand Type = 0x" + String(format:"%02X", someObject.getTestEnum.rawValue))
/*this prints: Command Type = 0x6E71
if the given Integer value from someObject.TestEnum is 28273*/
Now I additionally want to print "ONE" after the HEX value.
I am aware of the Question: How to get the name of enumeration value in Swift?
but this is something different because I want to determine the case name by the cases raw value instead of the enumeration value by itself.
Desired Output:
Command Type = 0x6E71, ONE
You can create an enum value from its rawValue and get its case String using String.init(describing:).
public enum TestEnum : UInt16 {
case ONE = 0x6E71
case TWO = 0x0002
case THREE = 0x0000
}
let enumRawValue: UInt16 = 0x6E71
if let enumValue = TestEnum(rawValue: enumRawValue) {
print(String(describing: enumValue)) //-> ONE
} else {
print("---")
}
You can't get the case name as as String as the enum's type isn't String, so you'll need to add a method to return it yourself…
public enum TestEnum: UInt16, CustomStringConvertible {
case ONE = 0x6E71
case TWO = 0x0002
case THREE = 0x0000
public var description: String {
let value = String(format:"%02X", rawValue)
return "Command Type = 0x" + value + ", \(name)"
}
private var name: String {
switch self {
case .ONE: return "ONE"
case .TWO: return "TWO"
case .THREE: return "THREE"
}
}
}
print(TestEnum.ONE)
// Command Type = 0x6E71, ONE
How we can typecast a variable of one enum type to another. I have two Int Enum. And when i create a variable of type First enum and assign some value from second it doesn't allow me. How we can achieve that?
Below is the code
enum Sections: Int {
case initiateZero = 0
case Air
case Weight
}
enum SubSections: Int {
case MaxPath = 0
case Weight
case FullWeight
}
For getting them.
var section = Sections.initiateZero
if isLongForm {
section = Sections(rawValue: indexPath.section)!
} else {
section = SubSections(rawValue: indexPath.section)
}
Not able to assign them. Please suggest.
I can not think of valid usecase, but you might want to use its rawValues:
enum Enum1: Int {
case one = 0
case two = 1
}
enum Enum2: Int {
case one = 0
case two = 1
}
let enum1 = Enum1.one
let enum2 = Enum2(rawValue: enum1.rawValue)
print(enum2)
prints:
Optional(Enum2.one)
I have already authorized HealthKit, and I am getting BiologicalSex from HealthKitStore like this:
let healthKitStore:HKHealthStore = HKHealthStore()
var biologicalSexObject: HKBiologicalSexObject?
var biologicalSex: HKBiologicalSex?
do {
biologicalSexObject = try healthKitStore.biologicalSex()
biologicalSex = biologicalSexObject!.biologicalSex
} catch _ as NSError {
biologicalSex = nil
print("error reading biological sex")
}
However, when I try to print biologicalSex it returns HKBiologicalSex instead of .Male or .Female.
I have seen more or less this exact code in several tutorials, so I'm wondering if there have been any syntax changes I should be aware of in Swift 2. (The error handling has changed, so I'm curious if anything else of note has.)
The rawValue of biologicalSex = biologicalSexObject!.biologicalSex is required to do this. The enum for BiologicalSex looks like this:
typedef enum : NSInteger {
HKBiologicalSexNotSet = 0,
HKBiologicalSexFemale,
HKBiologicalSexMale,
HKBiologicalSexOther,
} HKBiologicalSex;
Using this information it is easy to design a switch statement to cover all of the possible values:
switch biologicalSex.rawValue{
case 0:
biologicalSex = nil
case 1:
biologicalSex = "Female"
case 2:
biologicalSex = "Male"
case 3:
biologicalSex = "Other"
default:
biologicalSex = nil
}
I am trying to have a switch statement inside of an if statement checking integers
if variable <= 3 {
// code
switch variable {
case 0:
println("0 case")
case 1:
println("1 case")
case 2:
println("2 case")
case 3:
println("3 case")
default:
println("error")
}
}
But I am getting an error for each case
Binary operator '~=' cannot be applied to operands of type 'Int' and 'Int?'
I do not understand why this wouldn't work.
3 is an Int but variable is an Int? (an optional Int). You have to unwrap it.
For example, you can check if it's nil in the if statement. If it's not, then it's safe to force-unwrap (with !) inside the scope of that conditional:
One of many possible approaches:
if variable <= 3 && variable != nil {
// code
switch variable! {
case 0:
println("0 case")
case 1:
println("1 case")
case 2:
println("2 case")
case 3:
println("3 case")
default:
println("error")
}
}
var variable : Int? will not work
var variable = 2
//var variable : Int? // will not work
if variable <= 3 {
// code
switch variable {
case 0:
println("0 case")
case 1:
println("1 case")
case 2:
println("2 case")
case 3:
println("3 case")
default:
println("error")
}
}
If your variable is an Optional, be aware that if it's nil then in your initial if statement the expression nil <= 3 returns true.
So, the most comprehensive form your can choose (IMHO) is:
var variable : Int? = ...
switch variable {
case .Some(0):
print("1 case")
case .Some(1):
print("2 case")
case .Some(3):
print("3 case")
case .Some(let x):
print("We got \(x)")
case .None:
print("variable is nil")
}
Note:
print is Swift 2.0. Use println in Swift 1.2