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

I'm trying to convert duration (of type Int) to a String using String(duration). I am getting an error:

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

EDIT: I will post more of the code for a 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 VIEW 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
    }
}
5
  • Could you include a bit more code? Code inference is great when the compiler has enough information, but with what you've posted, we don't. Maybe what type waveFnList is, and what type the waves property is? Basically, can you show us enough to know that the type of duration is certainly an Int. Commented Aug 10, 2014 at 18:57
  • @Acey I added more code. Thanks in advance for your help. Commented Aug 10, 2014 at 19:09
  • @Zaph what is it then? No need to -1 just explain :) Commented Aug 10, 2014 at 19:17
  • It is a conversion. Taking an integer and creating a new String object. They are not the same in memory. Learning a little about the underlying bits and bytes provides a big payout. Consider editing your question. Commented Aug 10, 2014 at 19:19
  • 1
    +1 for the correction. Commented Aug 10, 2014 at 19:36

2 Answers 2

7

String cannot init with Int?, but it can with Int. Since String(int:Int) doesn't return an optional, you can get the effect you want with the same amount of code:

if let _duration = duration {
  viewDuration = String(_duration)
} else {
  viewDuration = ""
}
Sign up to request clarification or add additional context in comments.

Comments

2

Also, try below code :

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

I hope, these works for you . . .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.