gpt4 book ai didi

ios - 收到错误没有'+'候选者产生预期的上下文结果类型'NSString'

转载 作者:行者123 更新时间:2023-12-01 20:02:01 24 4
gpt4 key购买 nike

我正在用Swift 3和Xcode 8编写代码。

这是代码:

import Foundation
import UIKit

class CashTextFieldDelegate : NSObject, UITextFieldDelegate {

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {


let oldText = textField.text! as NSString

var newText = oldText.replacingCharacters(in: range, with: string) as NSString

var newTextString = String(newText)

let digits = NSCharacterSet.decimalDigits
var digitText = ""
for c in newTextString.unicodeScalars {
if digits.contains(c) {
digitText.append(String(c))
}
}

// Format the new string
if let numOfPennies = Int(digitText) {
newText = "$" + self.dollarStringFromInt(numOfPennies)+ "." + self.centsStringFromInt(numOfPennies)

} else {
newText = "$0.00"
}

textField.text = newText as String

return false
}

func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.text!.isEmpty {
textField.text = "$0.00"
}
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()

return true;
}

func dollarStringFromInt(value: Int) -> String {
return String(value / 100)
}

func centsStringFromInt(value: Int) -> String {

let cents = value % 100
var centsString = String(cents)

if cents < 10 {
centsString = "0" + centsString
}

return centsString
}

}

对于上面的这一行代码:
newText = "$" + self.dollarStringFromInt(numOfPennies) + "." + self.centsStringFromInt(numOfPennies)

我收到这样的错误:
No '+' candidates produce the expected contextual result type 'NSString'.

无法解决此错误。

没有任何解释的任何帮助将不胜感激

最佳答案

与Swift 2不同,NSStringString不会自动在彼此之间转换。

尝试这样的事情:

newText = ("$" + self.dollarStringFromInt(numOfPennies) + "." + self.centsStringFromInt(numOfPennies)) as NSString

您可以使用一致的类型来进一步清理-整个过程中使用 StringNSString(例如,更改函数的返回值等)。

关于ios - 收到错误没有'+'候选者产生预期的上下文结果类型'NSString',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39781649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com