我努力从Eureka表格中提取确切的价值。以下是我的源代码:
class CommunityReportViewController: FormViewController, MFMailComposeViewControllerDelegate{
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
var userTitleChoice: String = ""
var userCategoryChoice : String = ""
var userDescriptionChoice: String = ""
@IBOutlet weak var submitReport: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
form +++ Section("Feedback Type")
<<< SegmentedRow<String>("feedbackType"){
$0.options = ["Comments", "Bug Reports", "Questions"]
$0.value = "Comments"
}.onChange{ row in
let userDefault = NSUserDefaults.standardUserDefaults()
userDefault.setValue(row.value, forKey: "feedbackType")
}
+++ Section("Follow up?")
<<< ActionSheetRow<String>("officeLocation"){
$0.title = "Where are you?"
$0.selectorTitle = "Please select the office you are located in"
$0.options = ["Singapore", "Prague", "New Jersey"]
}
+++ Section("Best time to contact you?")
<<< PopoverSelectorRow<String>("preferredMethod"){
$0.title = "Preferred Method"
$0.options = ["Phone", "Email", "Do Not Contact"]
}
<<< DateRow("preferredDate"){
$0.title = "Preferred date"
}
<<< TimeInlineRow("preferredTime"){
$0.title = "Preferred time"
$0.noValueDisplayText = "-:--"
}
<<< ButtonRow() {
$0.title = "Submit Feedback!"
$0.onCellSelection(self.buttonTapped)
}
}
func buttonTapped(cell: ButtonCellOf<String>, row: ButtonRow) {
let valuesDictionary = form.values(includeHidden:true)
print (valuesDictionary["feedbackType"], "Feedback")
print (valuesDictionary["officeLocation"], "Office")
print (valuesDictionary["preferredTime"], "Time")
print (valuesDictionary["preferredMethod"], "Method")
print (valuesDictionary["preferredDate"], "Date")
}
}
当没有提供输入时,valuesDictionary返回的结果如下所示:
Optional(Optional("Comments")) Feedback
Optional(nil) Office
Optional(nil) Time
Optional(nil) Method
Optional(nil) Date
如果提供了正确的输入,则返回以下结果:
Optional(Optional("Comments")) Feedback
Optional(Optional("New Jersey")) Office
Optional(Optional(2016-07-08 10:00:23 +0000)) Time
Optional(Optional("Email")) Method
Optional(Optional(2016-07-11 07:00:16 +0000)) Date
有没有办法只返回用户选择的值?因为返回的结果很难解析。 示例I只希望返回以下结果
Comments
New Jersey
Email
2016-07-08 10:00:23 +0000
2016-07-11 07:00:16 +0000
答案 0 :(得分:1)
看起来您已经可以获取值了。您所要做的就是检查字段是否有值并打开选项:
if let feedbackType = valuesDictionary["feedbackType"] as? String
{
print(feedbackType) //should print only "Comments"
}
如果值为nil,则不满足条件并跳过if块内的代码。
所以基本上当你打印出可选项时,它看起来像一个需要解析的复杂字符串,但它们只需要解包来访问实际值。
您可以在Apple文档here
中找到有关选项的更多信息答案 1 :(得分:0)
必须扩展以处理更多类型(int,...)
let fvalues = form.values().mapValues { value -> String in
return value as? String ?? ""
}