Swift Eureka Forms - 如何为字段选项赋值?
Swift Eureka Forms - How to assign values to field options?
如果我像这样创建一个分段行字段:
<<< SegmentedRow<String>(){ row in
row.title = "Sex"
row.tag = "sex"
row.selectorTitle = "Select your sex"
row.options = ["Male","Female"]
}
如何将选项与特定值匹配?例如,如果用户 select 男性,那么有没有办法在代码中获取 M
而不是 Male
,但仍然向用户显示 Male
?
或者如果我有一个国家列表,例如:
<<< PushRow<String>(){ row in
row.title = "Passport Issuing Country"
row.tag = "passportIssuingCountry"
row.selectorTitle = "Passport Issuing Country"
row.options = ["AUSTRALIA","AUSTRIA","BELGIUM"]
}
我可以将每个国家/地区名称分配给国家/地区代码吗,例如 AUSTRALIA
将 return AU
、AUSTRIA
将 return AT
等
目前,无法为选项保留内部值 - 但您可以这样做:
1.创建一个包含所有可用选项的枚举
enum Sex: Int {
case NotKnown = 0
case Male = 1
case Female = 2
case NotApplicable = 9
static let all = [NotKnown, Male, Female, NotApplicable]
static let strings = ["Unknown", "Male", "Female", "Not Applicable"]
func string() -> String {
let index = Sex.all.index(of: self) ?? 0
return Sex.strings[index]
}
static func fromString(string: String) -> Sex {
if let index = Sex.strings.index(of: string) {
return Sex.all[index]
}
return Sex.NotKnown
}
}
2。使用您想要公开的所有选项创建您的行
<<< SegmentedRow<String>(){ row in
row.title = "Sex"
row.tag = "sex"
row.selectorTitle = "Select your sex"
row.options = [Sex.Female.string(), Sex.Male.string()]
}
3。读取值
let row: SegmentedRow<String>? = self.form.rowBy(tag: "sex")
if let value = row?.value {
// Returns "Female"
print(value)
// Returns 2
print(Sex.fromString(string: value).rawValue)
}
用户看到并选择了 Strings
,但是您得到了一个 enum
值,例如您可以将其作为整数保存在数据库中(参见:ISO/IEC 5218)。
如果我像这样创建一个分段行字段:
<<< SegmentedRow<String>(){ row in
row.title = "Sex"
row.tag = "sex"
row.selectorTitle = "Select your sex"
row.options = ["Male","Female"]
}
如何将选项与特定值匹配?例如,如果用户 select 男性,那么有没有办法在代码中获取 M
而不是 Male
,但仍然向用户显示 Male
?
或者如果我有一个国家列表,例如:
<<< PushRow<String>(){ row in
row.title = "Passport Issuing Country"
row.tag = "passportIssuingCountry"
row.selectorTitle = "Passport Issuing Country"
row.options = ["AUSTRALIA","AUSTRIA","BELGIUM"]
}
我可以将每个国家/地区名称分配给国家/地区代码吗,例如 AUSTRALIA
将 return AU
、AUSTRIA
将 return AT
等
目前,无法为选项保留内部值 - 但您可以这样做:
1.创建一个包含所有可用选项的枚举
enum Sex: Int {
case NotKnown = 0
case Male = 1
case Female = 2
case NotApplicable = 9
static let all = [NotKnown, Male, Female, NotApplicable]
static let strings = ["Unknown", "Male", "Female", "Not Applicable"]
func string() -> String {
let index = Sex.all.index(of: self) ?? 0
return Sex.strings[index]
}
static func fromString(string: String) -> Sex {
if let index = Sex.strings.index(of: string) {
return Sex.all[index]
}
return Sex.NotKnown
}
}
2。使用您想要公开的所有选项创建您的行
<<< SegmentedRow<String>(){ row in
row.title = "Sex"
row.tag = "sex"
row.selectorTitle = "Select your sex"
row.options = [Sex.Female.string(), Sex.Male.string()]
}
3。读取值
let row: SegmentedRow<String>? = self.form.rowBy(tag: "sex")
if let value = row?.value {
// Returns "Female"
print(value)
// Returns 2
print(Sex.fromString(string: value).rawValue)
}
用户看到并选择了 Strings
,但是您得到了一个 enum
值,例如您可以将其作为整数保存在数据库中(参见:ISO/IEC 5218)。