Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中将Int转换为字符串_Ios_String_Swift_Overloading_Type Conversion - Fatal编程技术网

Ios 在Swift中将Int转换为字符串

Ios 在Swift中将Int转换为字符串,ios,string,swift,overloading,type-conversion,Ios,String,Swift,Overloading,Type Conversion,我正在尝试使用String(duration)将duration(Int类型)转换为字符串。我得到一个错误: var duration = waveFnList.waves[indexPath.row].duration if let _duration = String(duration) { viewDuration = _duration } else { viewDuration = "" } 编辑:为了更好地理解,我将发布更多代码: 全球: Could not fin

我正在尝试使用String(duration)将duration(Int类型)转换为字符串。我得到一个错误:

var duration = waveFnList.waves[indexPath.row].duration
if let _duration = String(duration) {
    viewDuration = _duration
} else {
    viewDuration = ""
}
编辑:为了更好地理解,我将发布更多代码:

全球:

Could not find an overload for 'init' that accepts the supplied arguments
第一视图控制器

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

字符串不能用Int?初始化,但可以用Int初始化。由于字符串(Int:Int)不返回可选值,您可以用相同数量的代码获得想要的效果:

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
    }
}
另外,请尝试以下代码:

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

我希望,这些对你有用

你能再加一点代码吗?当编译器有足够的信息时,代码推理是很好的,但是根据您发布的内容,我们没有。也许WaveNList是什么类型,waves属性是什么类型?基本上,你能告诉我们持续时间的类型肯定是整数吗?@Acey我添加了更多的代码。提前谢谢你的帮助。@Zaph那是什么?不需要-1,只需解释:)这是一个转换。获取整数并创建新的字符串对象。他们在记忆上不一样。稍微了解一下底层的位和字节会带来很大的收益。考虑编辑你的问题。+ 1的修正。