-3

I'm making a mock remote control Iphone app using swift and Xcode is not letting me use .toInt() on the text of a UILabel. I'm trying to convert the text from a label into an integer and I'm not sure how to do this. Can anybody help me out? Thanks.

Here's my code:

@IBAction func channelInc(_ sender: UIButton) {
    var chnl = channel.text!.toInt()

    if (chnl!+1 > 99) {

    } else {
        let newChnl = chnl!+1
        channel.text = "\(String(newChnl))"
    }
}
AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
xxx3223
  • 1
  • 1

2 Answers2

0

There is not method on the String class called toInt(). The way to do it in swift is to use the initializer for the Int class.

let number = Int("5")
// returns a Int? (optional)
Federico Ojeda
  • 758
  • 7
  • 11
  • That fixed the compiler error, but now it's giving me a runtime error because it's taking the address of the string in memory (not the integer that is within the string), converting that to an integer, and then trying to add to it. Do you know how to convert the number that is within the string to an integer? – xxx3223 Feb 10 '17 at 01:37
  • That shouldn't be happening at all. Could you show the output of that?? And the updated code. This int initializer was actually created for this specific purpose in swift 2. You should do `let chnl = Int(channel.text!)`. Is the `channel` a textField or something similar? – Federico Ojeda Feb 10 '17 at 01:43
  • channel is a UILabel. This function is an action that's tied to a button that is supposed to increment channel. When I press the button, the following error shows up: – xxx3223 Feb 10 '17 at 02:01
  • *** First throw call stack: ( 0 CoreFoundation 0x000000010cbfad4b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010b77b21e objc_exception_throw + 48 2 CoreFoundation 0x000000010cc6af04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 3 CoreFoundation 0x000000010cb80005 ___forwarding___ + 1013 – xxx3223 Feb 10 '17 at 02:02
  • 4 CoreFoundation 0x000000010cb7fb88 _CF_forwarding_prep_0 + 120 5 UIKit 0x000000010d1a28bc -[UIApplication sendAction:to:from:forEvent:] + 83 6 UIKit 0x000000010d328c38 -[UIControl sendAction:to:forEvent:] + 67 7 UIKit 0x000000010d328f51 -[UIControl _sendActionsForEvents:withEvent:] + 444 – xxx3223 Feb 10 '17 at 02:02
  • 8 UIKit 0x000000010d327e4d -[UIControl touchesEnded:withEvent:] + 668 9 UIKit 0x000000010d210545 -[UIWindow _sendTouchesForEvent:] + 2747 10 UIKit 0x000000010d211c33 -[UIWindow sendEvent:] + 4011 – xxx3223 Feb 10 '17 at 02:02
  • 10 UIKit 0x000000010d211c33 -[UIWindow sendEvent:] + 4011 11 UIKit 0x000000010d1be9ab -[UIApplication sendEvent:] + 371 12 UIKit 0x000000010d9ab72d __dispatchPreprocessedEventFromEventQueue + 3248 13 UIKit 0x000000010d9a4463 __handleEventQueue + 4879 14 CoreFoundation 0x000000010cb9f761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 – xxx3223 Feb 10 '17 at 02:03
  • 15 CoreFoundation 0x000000010cb8498c __CFRunLoopDoSources0 + 556 16 CoreFoundation 0x000000010cb83e76 __CFRunLoopRun + 918 17 CoreFoundation 0x000000010cb83884 CFRunLoopRunSpecific + 420 18 GraphicsServices 0x0000000110434a6f GSEventRunModal + 161 19 UIKit 0x000000010d1a0c68 UIApplicationMain + 159 20 SerioTAssignment4 0x000000010b1996af main + 111 – xxx3223 Feb 10 '17 at 02:04
  • 21 libdyld.dylib 0x000000010f4ac68d start + 1 22 ??? 0x0000000000000001 0x0 + 1 ) – xxx3223 Feb 10 '17 at 02:04
  • libc++abi.dylib: terminating with uncaught exception of type NSException – xxx3223 Feb 10 '17 at 02:04
  • Updated Code: @IBAction func channelInc(_ sender: UIButton) { let chnl = Int(channel.text!) if(chnl! + 1 > 99){ }else{ let newChnl = chnl! + 1 channel.text = "\(String(newChnl))" } } – xxx3223 Feb 10 '17 at 02:07
0

Try

let chnl:Int? = Int(channel.text!)

RichardMoult
  • 100
  • 4