2015-11-05 46 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

是的,我试试这个! –

相关问题