2016-11-16 11 views
0
func textField(_ textField: UITextField, 
       shouldChangeCharactersIn range: NSRange, 
       replacementString string: String) -> Bool{ 
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
     let currentLocale = NSLocale.current 
     let decimalSeparator = currentLocale.objectForKey(NSLocaleDecimalSeparator) as! String 
     let existingTextHasDecimalSeparator = textField.text?.rangeOfString(decimalSeparator) 
     let replacementTextHasDecimalSeparator = string.rangeOfString(decimalSeparator) 
     if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil { 
      return false 
     } else { 
      return true 
     } 

可能有人幫我找出解決的讓小數點分隔符行沒有成員「objectForKey」「類型‘區域設置’價值」?由於如何解決'Locale'沒有成員'objectForKey'?獲取decimalseparator在我的代碼工作

回答

0

也許嘗試將其更改爲NSLocale.init(localeIdentifier: NSLocale.current.identifier).object(forKey: NSLocale.Key.decimalSeparator)

+0

這行代碼行得通,但現在我遇到了它下面兩行的問題。 – Shane

0

直接使用currentLocale

let currentLocale = NSLocale.current 
let decimalSeparator:String = currentLocale.decimalSeparator! 

這是爲我工作的性質。

let currentLocale = NSLocale.current 
let decimalSeparator:String = currentLocale.decimalSeparator! 
let existingTextHasDecimalSeparator = "text1.".range(of: decimalSeparator) 
let replacementTextHasDecimalSeparator = "text2.".range(of:decimalSeparator) 
if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil { 
    print(true) 
} else { 
    print(false) 
} 
+0

它擺脫了那個特定的錯誤代碼,但它下面的兩條線得到了相同的錯誤。 – Shane

0

LocaleSwiftifiedFoundation類)沒有調用,所以成員函數,但NSLocale一樣。

let local = Locale.current as NSLocale 
local.object(forKey: NSLocale.Key.someKey) 

這將工作至少直到幷包括Xcode9.0。我猜想對NSLocale的支持會在某個時候下降,所以當這一天到來時我們不應該使用它。

相關問題