Converting Int to String in Swift

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

      

I am trying to convert duration (Int type) to String using String (duration). I am getting the error:

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

      

EDIT: I'll post more code for better understanding:

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

      

FIRST VISION CONTROLLER

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

      

SECOND VIEW CONTROLLER

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

      

+3


source to share


2 answers


A string cannot be initialized with Int? But it can with Int. Since String (int: Int) does not return optional, you can get the desired effect with the same amount of code:



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

      

+3


source


Also, try entering the code:

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

      



Hope this works for you ...

+1


source







All Articles