swift - Print evens app error help - cannot convert to type string -
i've been trying take number , display of numbers said number in string. currently, think have working i'm having trouble getting display in label.
the error i'm getting on: label.text = factor
. error states: "cannot assign value of type '()' type 'string?'
i wonder if had idea how fix or improve code , explain did wrong? i'm still kind of new swift.
here's code:
import uikit class viewcontroller: uiviewcontroller { @iboutlet var input1 : uitextfield! @iboutlet var label : uilabel! @ibaction func factoraction(sender: uibutton) { if let text = input1.text { if let num = int(text) { // text int let factor = getevens(<#t##input: int##int#>) label.text = factor } else { // show user entered text isn't number } } else { // there's no text } } // notifies user when no text or if non number string/whole integer has been entered. // recycled code factor application because useful func getevens(_ input: int){ var output: string = "" in 0 ... input { if (i % 2 == 0){ output += string(i) + "," } } output.remove(at: output.index(before: output.endindex)) print(output); } // returns result override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. }
}
thanks again guys!
edit 2: went ahead , updated code; i'm getting thread 1: breakpoint 1.1 whenever try , enter number textbox when testing app. ideas why happening?
import uikit class viewcontroller: uiviewcontroller { @iboutlet weak var input1 : uitextfield! @iboutlet weak var label : uilabel! @ibaction func factoraction(sender: uibutton) { if let text = input1.text { if let num = int(text) { // text int let factor = getevens(num) label.text = factor } else { // show user entered text isn't number } } else { // there's no text } } // notifies user when no text or if non number string/whole integer has been entered. // recycled code factor application because useful func getevens(_ input: int) -> string { let output = stride(from: 0, through: input, by: 2) .map(string.init) .joined(separator: ",") return output } override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } }
your getevens()
function prints string console, not return anything. (more precisely, return type void
or ()
, , type '()'
in error message
"cannot assign value of type '()' type 'string?'
comes from.)
what want
func getevens(_ input: int) -> string { // <-- return type `string` var output: string = "" in 0 ... input { if (i % 2 == 0){ output += string(i) + "," } } output.remove(at: output.index(before: output.endindex)) return output // <-- returned value }
note can achieve same result with
func getevens(_ input: int) -> string { let output = stride(from: 0, through: input, by: 2) .map(string.init) .joined(separator: ",") return output }
which may bit more "swifty":
stride(from: 0, through: input, by: 2)
gives sequence of numbers0
input
,map(string.init)
converts each number string, andjoined(separator: ",")
concatenates results single string, adding comma between elements.
Comments
Post a Comment