2015-09-28 50 views
2

在我更新至XCode 7後,我現在收到此錯誤「UITextField?不能轉換爲'UITextField'。我爲什麼不能轉換一下子糊塗了。有什麼想法?UITextField?在XCode 7更新後不能轉換爲'UITextField'

 let saveAction = UIAlertAction(title: "Search", 
     style: UIAlertActionStyle.Default, 
     handler: {(action) in 
      let query = 
      (alertController.textFields?[0] as UITextField).text ### errors here 
      let tag = query 
      if !query.isEmpty { 
       if self.model.queryForTag(tag) != nil { 
       } 
       else if isNew { 
        self.model.saveQuery(
         query, forTag: tag, syncToCloud: true) 
        let indexPath = 
        NSIndexPath(forRow: 0, inSection: 0) 
        self.tableView.insertRowsAtIndexPaths([indexPath], 
         withRowAnimation: .Automatic) 
        self.forceSearch(indexPath) 
        self.tableView.reloadData() 
       } 
      } 
    }) 

回答

2

展開您的自選正常。這將解決大部分的錯誤。無論是textfieldsquery會後可用後衛聲明

let saveAction = UIAlertAction(title: "Search", 
    style: UIAlertActionStyle.Default, 
    handler: {(action) in 

     guard let textFields = alertController.textFields, let first = textFields.first, let query = first.text else { 
      return 
     } 

}) 

PS後衛報表也將幫助你成長金字塔

+0

感謝輸入這似乎解決我的問題太多:(alertController.textFields [0]的UITextField?)!文字 –

+2

請不要使用鱈魚像那樣。每當你做獨角獸死亡! –

+0

@R Menke - 會 - 謝謝你的幫助 –