在Swift中将Int转换为String

时间:2014-08-10 18:54:00

标签: ios string swift overloading type-conversion

var duration = waveFnList.waves[indexPath.row].duration
if let _duration = String(duration) {
    viewDuration = _duration
} else {
    viewDuration = ""
}

我尝试使用String(持续时间)将持续时间(Int类型)转换为String。我收到一个错误:

Could not find an overload for 'init' that accepts the supplied arguments

编辑:我会发布更多代码以便更好地理解:

GLOBAL:

import Foundation

var waveFnList: WaveFunctionList = WaveFunctionList()

var viewName:String = ""
var viewDuration:String = ""
var viewPeriod:String = ""
var viewMinAmp:String = ""
var viewMaxAmp:String = ""
var viewStep:String = ""
var viewType:String = ""

var waveTypes: [Int: String] = [
    0: "Sine",
    1: "Saw",
    2: "Square",
    3: "Triangle",
    4: "Flat"
]

struct WaveFunction {
    var name: String?
    var duration: Int?
    var period: Int?
    var minAmp: Int?
    var maxAmp: Int?
    var step: Int?
    var type: Int?
}

class WaveFunctionList: NSObject {
    var waves = [WaveFunction]()

    func addWave(name: String, duration: Int, period: Int, minAmp: Int, maxAmp: Int, step: Int, type: Int) {
        waves.append(WaveFunction(name: name, duration: duration, period: period, minAmp: minAmp, maxAmp: maxAmp, step: step, type: type))
    }
}

第一视图控制器

import UIKit

class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tblWaveFunctions: UITableView!

    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.
    }

    //Returning to view
    override func viewWillAppear(animated: Bool) {
        tblWaveFunctions.reloadData()
}

    //UITableViewDelete
    func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!) {
        if (editingStyle == UITableViewCellEditingStyle.Delete) {
            waveFnList.waves.removeAtIndex(indexPath.row)
            tblWaveFunctions.reloadData()
        }
    }

    //UITableViewDataSource
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return waveFnList.waves.count
    }

    //Cell data
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

        let cell: UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier: "test")

        cell.textLabel.text = waveFnList.waves[indexPath.row].name
        cell.detailTextLabel.text = waveTypes[waveFnList.waves[indexPath.row].type!]

        return cell
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    // Get the row data for the selected row
    /*
    var alert: UIAlertView = UIAlertView()
    alert.title = waveFnList.waves[indexPath.row].name
    alert.message = waveTypes[waveFnList.waves[indexPath.row].type!]
    alert.addButtonWithTitle("Ok")
    alert.show()
    */
    if let _name = waveFnList.waves[indexPath.row].name {
        viewName = _name
    } else {
            viewName = ""
        }
        var duration = waveFnList.waves[indexPath.row].duration
        if let _duration = String(duration) {
            viewDuration = _duration
        } else {
            viewDuration = ""
        }
        var period = waveFnList.waves[indexPath.row].period
        if let _period = String(period) {
            viewPeriod = _period
        } else {
            viewPeriod = ""
        }
        var min_Amp = waveFnList.waves[indexPath.row].minAmp
        if let _min_Amp = String(min_Amp) {
            viewMinAmp = _min_Amp
        } else {
            viewMinAmp = ""
        }
        var max_Amp = waveFnList.waves[indexPath.row].maxAmp
        if let _max_Amp = String(max_Amp) {
            viewMaxAmp = _max_Amp
        } else {
            viewMaxAmp = ""
        }
        var step = waveFnList.waves[indexPath.row].step
        if let _step = String(step) {
            viewStep = _step
        } else {
            viewStep = ""
        }
        var type = waveFnList.waves[indexPath.row].step
        if let _type = String(type) {
            viewType = _type
        } else {
            viewType = ""
        }
        self.tabBarController.selectedIndex = 1 //go back to firstView
    }
}

第二视图控制器

import UIKit

class SecondViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var txtName: UITextField!
    @IBOutlet var txtDuration: UITextField!
    @IBOutlet var txtPeriod: UITextField!
    @IBOutlet var txtMinAmp: UITextField!
    @IBOutlet var txtMaxAmp: UITextField!
    @IBOutlet var txtStep: UITextField!
    @IBOutlet var txtType: UITextField!

    override func loadView() {
        println("despues")
        println(viewName)
        setInfo(viewName, duration: viewDuration, period: viewPeriod, minAmp: viewMinAmp, maxAmp: viewMaxAmp, step: viewStep, type: viewType)
    }

    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.
    }

    //Events
    @IBAction func btnAddWaveFunction_Click(sender: UIButton) {
        waveFnList.addWave(txtName.text, duration: txtDuration.text.toInt()!, period: txtPeriod.text.toInt()!, minAmp: txtMinAmp.text.toInt()!, maxAmp: txtMaxAmp.text.toInt()!, step: txtStep.text.toInt()!, type: txtType.text.toInt()!)
        self.view.endEditing(true) //hide keyboard
        txtName.text = ""
        txtDuration.text = ""
        txtPeriod.text = ""
        txtMinAmp.text = ""
        txtMaxAmp.text = ""
        txtStep.text = ""
        txtType.text = ""
        self.tabBarController.selectedIndex = 0 //go back to firstView
    }

    //iOS Touch Functions
    override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        self.view.endEditing(true)
    }

    //UITextField Delegate
    //Hide keyboard when click return
    func textFieldShouldReturn(textField: UITextField!) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    func setInfo(name: String?, duration: String, period: String, minAmp: String, maxAmp: String, step: String, type: String) {
        txtName.text = name
        txtDuration.text = duration
        txtPeriod.text = period
        txtMinAmp.text = minAmp
        txtMaxAmp.text = maxAmp
        txtStep.text = step
        txtType.text = type
    }
}

2 个答案:

答案 0 :(得分:3)

String不能用Int?初始化,但它可以用Int。由于String(int:Int)不返回可选项,因此您可以使用相同数量的代码获得所需的效果:

if let _duration = duration {
  viewDuration = String(_duration)
} else {
  viewDuration = ""
}

答案 1 :(得分:1)

另外,请尝试以下代码:

if let _duration = duration {
   viewDuration = _duration.integerValue
} else {
   viewDuration = ""
}

我希望,这些适合你。 。