我可以在C ++ / C,Objective C和Playground中完成。当我尝试制作应用程序时,我无法在Swift中使用它。
有人可以帮忙吗?
//
// ViewController.swift
// Simple Mathemtical Functions_Swift
//
// Created by on 7/01/2015.
// Copyright (c) 2015 . All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet var labelResult: UILabel!
@IBOutlet var Number2: UILabel!
@IBOutlet var Number1: UILabel!
@IBOutlet var field1: [UITextField]!
@IBOutlet var field2: [UITextField]!
@IBAction func Calculate(sender: AnyObject) {
labelResult.text = "\()"}
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.
}
}
答案 0 :(得分:2)
如果field1
和field2
以及UITextField
s,那么[UITextField]!
应更改为UITextField!
。 [UITextField]
表示UITextField
s的数组。
@IBOutlet var labelResult: UILabel!
@IBOutlet var Number2: UILabel!
@IBOutlet var Number1: UILabel!
@IBOutlet var field1: UITextField!
@IBOutlet var field2: UITextField!
@IBAction func Calculate(sender: AnyObject) {
var a = (field1.text as NSString).floatValue
var b = (field2.text as NSString).floatValue
var sum = a + b
labelResult.text = "\(sum)"
}
答案 1 :(得分:0)
也许您遇到了麻烦,因为field1.text
是String?
而不是NSString
。
@IBAction func Calculate(sender: AnyObject) {
labelResult.text = "\((field1.text as NSString).floatValue + (field2.text as NSString).floatValue)"
}