类型字符串不符合协议序列类型 [英] type String does not conform to protocol sequencetype
本文介绍了类型字符串不符合协议序列类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是代码,我正在尝试制作一个简单的计算器:
Here is the code, I'm trying to make a simple calculator :
@IBAction func btnOperatorInput(sender: UIButton) {
let op = ("+", "-", "*", "/")
let nr1 = sender.currentTitle!
var nr2 = ""
if sender.currentTitle == nr1{
display.text! = self.display.text! + nr1
nrTaped = true
} else if sender.currentTitle == String(op) {
nrTaped = false
nr2 = display.text!
display.text! = String(String(nr1) + String(op))
}
for _ in op.0{
self.calc.add(Int(nr1)!, secondNumber: Int(nr2)!)
}
}
我在 for 循环中遇到错误.当我按 + 将数字加在一起时,我不知道如何使用我创建的字符串中的+"(例如)!
I get the error at the for loop. I don't know how to use the "+" (for example) from the String I created, when I press the + to add the numbers together!
推荐答案
错误很容易解决,需要访问String
的CharacterView
:
The error is easily solved, you need to access the String
's CharacterView
:
for _ in op.0.characters {
self.calc.add(Int(nr1)!, secondNumber: Int(nr2)!)
}
注意:正如上面的另一位评论者所查询的那样,我不确定您为什么要迭代始终是单个且可预测的字符,但我想这可能只是示例代码.
Note: As another commenter has queried above, I'm not sure why you'd want to iterate over what will always be a single and predictable character but I guess this might just be example code.
这篇关于类型字符串不符合协议序列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文