2016-07-08 9 views
0

私はユーレカフォームから正確な値を抽出するのに苦労しています。以下私のソースコードは、これまでである:以下のように何も入力が提供されていないときに返さユーレカフォームから解析された値を抽出するには?

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 

ユーザーが選択した値のみを返す方法はありますか?返される結果は解析が難しいためです。 例次の結果が返されます。

Comments 
    New Jersey 
    Email 
    2016-07-08 10:00:23 +0000 
    2016-07-11 07:00:16 +0000 

答えて

0

値を取得できるようです。あなたがしなければならないのは、フィールドに値を持っているかどうかを確認してoptionalsをアンラップです:

if let feedbackType = valuesDictionary["feedbackType"] as? String 
{  
    print(feedbackType) //should print only "Comments" 
} 

それが条件を満たしているとIFブロック内のコードをスキップしません値nilがある場合。

基本的にオプションを印刷すると、解析する必要がある複雑な文字列のように見えますが、実際の値にアクセスするにはアンラップする必要があります。

オプションの詳細については、Appleのマニュアルhere

関連する問題