- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我收到错误:
“Cannot subscript a value of type ‘[Double]’ with an index of type ‘(Any) -> Int’” on the following line of code:
tipPer = tipPercentages[index]
这是我的其余代码(包括错误行):((对于糟糕的格式/语法感到抱歉,我是 Swift 的新手!))
import UIKit
class ViewController: UIViewController {
var tipPer: Int = 0
let defaults = UserDefaults.standard
@IBOutlet weak var tipLabel: UILabel!
@IBOutlet weak var totalLabel: UILabel!
@IBOutlet weak var perPersonLabel: UILabel!
@IBOutlet weak var billField: UITextField!
@IBOutlet weak var peopleField: UITextField!
@IBOutlet weak var tipControl: UISegmentedControl!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// This is a good place to retrieve the default tip percentage from UserDefaults
// and use it to update the tip amount
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func onTap(_ sender: AnyObject) {
view.endEditing(true)
}
@IBAction func calculateTip(_ sender: AnyObject) {
let tipPercentages = [0.10, 0.15, 0.20]
defaults.synchronize()
if (tipControl.selectedSegmentIndex == 0) {
let index = tipControl.selectedSegmentIndex
}
if (tipControl.selectedSegmentIndex == 1) {
let index = tipControl.selectedSegmentIndex
}
if (tipControl.selectedSegmentIndex == 2) {
let index = tipControl.selectedSegmentIndex
}
if (tipControl.selectedSegmentIndex == 3) {
let index = defaults.integer(forKey: "defaultTipControlKey")
}
let tipPer = tipPercentages[index]
let bill = Double(billField.text!) ?? 0
let people = Double(peopleField.text!) ?? 1
let tip = bill * tipPer
let total = bill + tip
let perPerson = total / people
tipLabel.text = String(format: "$%.2f", tip)
totalLabel.text = String(format: "$%.2f", total)
perPersonLabel.text = String(format: "$%.2f Each", perPerson)
}
}
我知道它必须通过将索引变量设置为整数来做一些事情,我试图这样做,但我遗漏了一些东西。任何帮助将不胜感激。
最佳答案
问题是您在所有 if block
中声明的 index
常量,因此它的范围对应于该 if block
所以它在 if block
之后对您不可用。要解决这个问题,请在 for if block 之前声明索引变量,而不是创建多个 if block ,您只需要像这样的单个 if condition
。
var index = defaults.integer(forKey: "defaultTipControlKey") //Default value
if (tipControl.selectedSegmentIndex != 3) {
index = tipControl.selectedSegmentIndex
}
let tipPer = tipPercentages[index]
注意:您的if 条件
没有任何意义,您只想在一个if block
之上获取索引。
关于arrays - 收到错误 : “Cannot subscript a value of type ‘[Double]’ with an index of type ‘(Any) -> Int’ ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41478248/
目前我使用服务器接收 GCP Pub/Sub 消息,我想执行 Purchase.Subscriptions:acknowledge调用以确认在服务器收到通知时尚未确认的订阅。 这是场景... 用户购
在 SQL Server 中,在“订阅端”,如何知道表是否处于复制/订阅状态? 有什么想法吗? 最佳答案 我不确定这个问题是否有一个简单的答案,并且我认为答案可能会根据复制类型的不同而有所不同。我认为
我正在使用 Angular 5 并使用 subscribe() 订阅了一个 observable方法。我想知道是否只调用unsubscribe()订阅上的方法足以清理所有内容,或者我也应该调用 rem
我似乎无法解决这个问题。下面是我的代码: #include #include #include _Bool are_anagrams (const char *word1, const char *w
当我使用 gcc -Wall 运行程序时,我得到了 warning: array subscript has type ‘char’ 请帮我看看哪里出了问题。警告显示在第 20:7 和 21:7 行。
我有一个来自 BlockCollection 的 Observable,我像队列一样使用它 IObservable GetObservable() { _queue.GetConsum
错误 1:当我尝试从元数据中获取 stringValue 时,在 Swift3 中显示上述错误: let myMetadata: AVMetadataMachineReadableCodeObject
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我正在将Paypal的订阅整合到我的网络服务中。。我想知道当我收到onApprove回调(Java SDK)时,订阅是否被激活。。这也意味着onApprove回调是否等同于从WebHook接收通知事件
这个问题在这里已经有了答案: Warning: array subscript has type char (3 个答案) 关闭 3 年前。 问题是这个警告在 15 和 18 警告:数组下标的类型为
根据卷积定理,卷积运算在傅里叶域变为逐点乘法——这里我有'fft_x'的形状(批量大小,高度,宽度,in_channels)这是输入数据的fft和类似的形状(高度,宽度,in_channels,out
我正在按照该教程进行操作,我正在运行带有 cygwin 编译器的 WindowsXP 32 位,该教程要求我运行以下代码: #include #include // forward declara
我有一个网站,我想实现付费订阅服务。它是一项简单的服务,只有两种类型的计划。现在,生病只是使用 Paypal 。但是在开始之前我有点迷茫,主要是数据模型。 我现在的主要问题是,我需要为每个订阅保留哪些
我们可以仅依靠subscr_eot来激活/停用帐户吗? 假设我们有以下情形: 在9/16,客户使用Paypal支付每月定期费用 服务。 24小时后, Paypal (Paypal)发送“subscr_
我的应用程序已在我的模拟器上完成,但当我尝试在我的手机上使用时,我遇到了 2 个错误 “下标”的使用不明确 我正在阅读这篇文章,但无法修复它。 How to solve Ambiguous use o
我在尝试使用下标访问范围的第 n 个元素时遇到问题。代码 super 简单: var range = 0.. = 0..)[0] 但是这些都没有解决问题。有没有办法让 Xcode 消除对 subscr
我是 angualr 2 的新手。我正在做一个小的练习应用程序。我有一个用户登录时的身份验证服务。这是它的登录功能。 login(email:string, password:string){
我是 Swift 的初学者,对运算符没有任何高级知识。 我有以下类(class) class Container { var list: [Any] = []; } 我想实现运算符 subscr
我正在尝试从 99 张纸中复制第二行,并将它们一个接一个地添加到第 100 张纸中。 vba 代码如下,但是我不断收到以下错误: 运行时错误'9': 下标超出范围 Sub copyrow()
我正在尝试在 Excel 中的 VBA 中创建一个宏,以将一个工作表的内容复制到另一个工作表(最终将升级为将内容从一个工作簿复制到另一个工作簿,但需要先证明这个概念)但我一直遇到运行时错误'9':“下
我是一名优秀的程序员,十分优秀!