Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 接收可选(“值”)而不是“值”的文本字段_Ios_Swift - Fatal编程技术网

Ios 接收可选(“值”)而不是“值”的文本字段

Ios 接收可选(“值”)而不是“值”的文本字段,ios,swift,Ios,Swift,我正在尝试传递一个正确的文本,api放在swift中,他带着可选消息到达。这是我的代码: let accountValues:NSArray = account!.accountNumber.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "-")) self.accountTextField.text = accountValues.objectAtIndex(0) as? String self.

我正在尝试传递一个正确的文本,api放在swift中,他带着可选消息到达。这是我的代码:

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

您正在使用可选值作为?字符串。因此,它将返回可选的“值”

试试这个:

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

请更换这些线路:

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


检索值时,在值后使用!键。向下转换应强制展开任何选项。任何设置为?的项都可以使用!强制展开!。 比如说,

let intString = 7 as? String

print(intString!)

这应该打印7,而不是可选的7。因此,双引号是否有问题?双引号?我相信它们不会干扰!这会返回一个警告,问题仍然存在。因此,我相信组件由字符分隔将此已返回值设置为此可选消息您可以在account类中声明accountNumber吗作为非可选?var accountNumber:String!=Optionalvalue似乎来自用于填充accountNumber属性的值。
let intString = 7 as? String

print(intString!)