我想解决我的问题。
我在Xcode中有文本字段,我想Xcode计算用户输入的字符串:
var x = 1
var y = 2
像这样
我想如果用户在文本字段中输入xy
,则Xcode计算如下:1+2
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var label1: UILabel!
@IBOutlet var label2: UILabel!
@IBOutlet var label3: UILabel!
@IBOutlet var label4: UILabel!
@IBOutlet var tex1: UITextField!
@IBOutlet var tex2: UITextField!
override function viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tex1.delegate = self
tex2.delegate = self
}
override function didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
function textFieldShouldReturn(_ textField: UITextField) -> Bool{
tex1.resignFirstResponder()
tex2.resignFirstResponder()
return true
}
@IBAction function close(_ sender: UIButton) {
tex1.resignFirstResponder()
tex2.resignFirstResponder()
}
@IBAction function pushAction(_ sender: UIButton) {
label4.text = String(Int(tex1.text)+Int(tex2.text))
}
}
答案 0 :(得分:0)
var temp = 0
var prevCount = -1
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
var txtAfterUpdate:NSString = textField.text! as NSString
txtAfterUpdate = txtAfterUpdate.replacingCharacters(in: range, with: string).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) as NSString
let newString:String! = String(txtAfterUpdate)
if(prevCount > newString.characters.count - 1){
if(newString.characters.count - 1 != -1){
self.enterAmount = self.enterAmount - Double(textField.text![(textField.text?.characters.count)! - 1])!
}else{
self.enterAmount = 0
}
}else{
self.enterAmount = Double(newString[newString.characters.count - 1])! + self.enterAmount
}
print("YOUR TOTAL \(self.enterAmount)")
prevCount = newString.characters.count - 1
return true
}
extension String{
subscript(i: Int) -> String {
guard i >= 0 && i < characters.count else { return "" }
return String(self[index(startIndex, offsetBy: i)])
}
subscript(range: Range<Int>) -> String {
let lowerIndex = index(startIndex, offsetBy: max(0,range.lowerBound), limitedBy: endIndex) ?? endIndex
return substring(with: lowerIndex..<(index(lowerIndex, offsetBy: range.upperBound - range.lowerBound, limitedBy: endIndex) ?? endIndex))
}
subscript(range: ClosedRange<Int>) -> String {
let lowerIndex = index(startIndex, offsetBy: max(0,range.lowerBound), limitedBy: endIndex) ?? endIndex
return substring(with: lowerIndex..<(index(lowerIndex, offsetBy: range.upperBound - range.lowerBound + 1, limitedBy: endIndex) ?? endIndex))
}
}
答案 1 :(得分:-1)
你可以像这样使用
template<typename Base, typename Concrete>
std::list<Concrete*> downcast_list (std::list<Base*> const & bases) {
std::list<Concrete*> result;
for (auto const base_ptr : bases) {
Concrete * concrete_ptr = dynamic_cast<Concrete*>(base_ptr);
if (concrete_ptr != nullptr) {
result.push_back(concrete_ptr);
} else {
// Error or ignore?
}
}
return result;
}