2015-11-05 64 views
0

我想通過他與到達的消息被放在迅速正確的API文本‘可選’這是我的代碼:文本字段接收可選(「值」),而不是‘價值’

let accountValues:NSArray = account!.accountNumber.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "-")) 
self.accountTextField.text = accountValues.objectAtIndex(0) as? String 
self.accountDigitTextField.text = accountValues.objectAtIndex(1) as? String 
+0

所以是用雙引號的問題嗎? –

+0

雙引號?我相信他們不會干涉! –

回答

0

當檢索值時,在值後面使用!downcast應該強制解開任何可選項,任何設置爲?的東西都可以使用!強制解包。 例如,

let intString = 7 as? String 

print(intString!) 

這應打印 「7」,而不是可選( 「7」)

0

您正在使用可選值「As? 。字符串」因此,它會返回一個可選( '值')

試試這個:

let accountValues:NSArray = account!.accountNumber.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "-")) 
self.accountTextField.text = "\(accountValues.objectAtIndex(0))" 
self.accountDigitTextField.text = "\(accountValues.objectAtIndex(1))" 
+0

這返回一個警告和問題繼續 –

+0

現在檢查我的答案。 –

+0

也沒有工作,我相信componentsSeparatedByCharactersInSet這已經返回值到這個可選的消息 –

0

請更換這些線路:

self.accountTextField.text = accountValues.objectAtIndex(0) as? String 
self.accountDigitTextField.text = accountValues.objectAtIndex(1) as? String 

由:

self.accountTextField.text = (accountValues.objectAtIndex(0) as! String) 
self.accountDigitTextField.text = (accountValues.objectAtIndex(1) as! String) 
+0

謝謝,但問題仍然存在 –

+0

你可以聲明accountNumber在帳戶類爲非可選? var accountNumber:String! =「」 –

+0

是的,我試試這個! –

相關問題