Xcode 6 Swift 在文本字段中将字符串转换为 Int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25729902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:29:51  来源:igfitidea点击:

Xcode 6 Swift Converting String to Int in textfields

xcodestringswiftintxcode6

提问by Alexander

so i have been stuck with this problem for a very long time now and i cannot figure it out.

所以我已经被这个问题困扰了很长时间,我无法弄清楚。

so my basic idea is that i have 2 textfields, where the user puts in a number (i don't know how to open the keypad yet would be nice if someone knew how) in both textfields and i want to divide those to numbers by each other.

所以我的基本想法是我有 2 个文本字段,用户在两个文本字段中输入一个数字(我不知道如何打开键盘,但如果有人知道如何打开会很好),我想将它们除以数字彼此。

< Label.text = (Textfield1.text) / (Textfield2.text) >

< Label.text = (Textfield1.text) / (Textfield2.text) >

so i got 3 variables, a label, and two textfields, but i keeps giving me this error that string isn't convertible to a UIint8.

所以我得到了 3 个变量、一个标签和两个文本字段,但我一直给我这个错误,字符串不能转换为 UIint8。

here is a screenshot with the code

这是带有代码的屏幕截图

enter image description here

在此处输入图片说明

here is the storyboard

这是故事板

enter image description herePS: i am kinda new to this Xcode, so the problem might be REALLY obvious :/

在此处输入图片说明PS:我对这个 Xcode 有点陌生,所以问题可能真的很明显:/

here is the error code (it gave me an error when i did it without

这是错误代码(当我没有这样做时它给了我一个错误

let answer2 = textfield1.text.toInt()! / textfield2.text.toInt()!

让 answer2 = textfield1.text.toInt()!/ textfield2.text.toInt()!

2014-09-12 15:16:05.764 lb[3107:261572] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key ButtonO.' *First throw call stack: ( 0 CoreFoundation 0x000000010f2fb3e5 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000110e11967 objc_exception_throw + 45 2 CoreFoundation 0x000000010f2fb029 -[NSException raise] + 9 3 Foundation 0x000000010f713383 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259 4 CoreFoundation 0x000000010f2453f0 -[NSArray makeObjectsPerformSelector:] + 224 5 UIKit 0x000000010fe4716d -[UINib instantiateWithOwner:options:] + 1506 6 UIKit 0x000000010fca9d78 -[UIViewController _loadViewFromNibNamed:bundle:] + 242 7 UIKit 0x000000010fcaa368 -[UIViewController loadView] + 109 8 UIKit 0x000000010fcaa5d9 -[UIViewController loadViewIfRequired] + 75 9 UIKit 0x000000010fcaaa6e -[UIViewController view] + 27 10 UIKit 0x000000010fbcb2b1 -[UIWindow addRootViewControllerViewIfPossible] + 58 11 UIKit 0x000000010fbcb64a -[UIWindow _setHidden:forced:] + 276 12 UIKit 0x000000010fbd7a81 -[UIWindow makeKeyAndVisible] + 42 13 UIKit 0x000000010fb825cd -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628 14 UIKit 0x000000010fb8529c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350 15 UIKit 0x000000010fb84168 -[UIApplication workspaceDidEndTransaction:] + 179 16 FrontBoardServices 0x0000000112665513 __31-[FBSSerialQueue performAsync:]_block_invoke + 16 17 CoreFoundation 0x000000010f230aac __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK+ 12 18 CoreFoundation 0x000000010f2267f5 __CFRunLoopDoBlocks + 341 19 CoreFoundation 0x000000010f2265b5 __CFRunLoopRun + 2389 20 CoreFoundation 0x000000010f2259f6 CFRunLoopRunSpecific + 470 21 UIKit 0x000000010fb83bdf -[UIApplication _run] + 413 22 UIKit 0x000000010fb86990 UIApplicationMain + 1282 23 lb 0x000000010edb6e5e top_level_code + 78 24 lb 0x000000010edb6e9a main + 42 25 libdyld.dylib 0x00000001113b3145 start + 1 26 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

2014-09-12 15:16:05.764 lb[3107:261572] * 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不是键值编码兼容的键 ButtonO .' *首先抛出调用堆栈:(0 CoreFoundation 0x000000010f2fb3e5exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000110e11967 objc_exception_throw + 45 2的CoreFoundation 0x000000010f2fb029 - [NSException提高] + 9 3基金会0x000000010f713383 - [NSObject的(的NSKeyValueCoding)的setValue:forKey:] + 259 4的CoreFoundation 0x000000010f2453f0 - [NSArray的makeObjectsPerformSelector:] + 224 5 UIKit 0x000000010fe4716d -[UINib instantiateWithOwner:options:] + 1506 6 UIKit 0x000000010fca9d78 -[UIViewController _loadViewFromNibNamed:bundle:] + 242 7的UIKit 0x000000010fcaa368 - [UIViewController中的loadView] + 109 8的UIKit 0x000000010fcaa5d9 - [UIViewController中loadViewIfRequired] + 75 9的UIKit 0x000000010fcaaa6e - [UIViewController的视图] + 27 10的UIKit 0x000000010fbcb2b1 - [一个UIWindow addRootViewControllerViewIfPossible] + 58 11的UIKit 0x000000010fbcb64a - [的UIWindow _setHidden :forced:] + 276 12 UIKit 0x000000010fbd7a81 -[UIWindow makeKeyAndVisible] + 42 13 UIKit 0x000000010fb825cd -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628 14的UIKit 0x000000010fb8529c - [UIApplication的_runWithMainScene:transitionContext:完成:] + 1350 15的UIKit 0x000000010fb84168 - [UIApplication的workspaceDidEndTransaction:] + 179个16 FrontBoardServices 0x0000000112665513 __31- [FBSSerialQueue performAsync:] _ block_invoke + 16 17的CoreFoundation 0x000000010f230aac __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK+ 12 18的CoreFoundation 0x000000010f2267f5 __CFRunLoopDoBlocks + 341 19的CoreFoundation 0x000000010f2265b5 __CFRunLoopRun + 2389 20的CoreFoundation 0x000000010f2259f6 CFRunLoopRunSpecific + 470 21的UIKit 0x000000010fb83bdf - [UIApplication的_run] + 413 22的UIKit 0x000000010fb86990 UIApplicationMain + 1282 23磅0x000000010edb6e5e top_level_code + 78 24磅0x000000010edb6e9a主+ 42 25 libdyld .dylib 0x00000001113b3145 开始 + 1 26 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: 以 NSException (lldb) 类型的未捕获异常终止

回答by Hyman Chorley

In order to turn a string into an int, simply use .toInt()and it will give you an integer value to use. As for changing the keyboard, you can only bring up a keypad on an iPhone - if you try on an iPad, it will simply show the number side of the keyboard, but the user can change back to normal letters, therefore you need a custom keyboard on an iPad. Nonetheless, below is the code to get the keypad on the iPhone:

要将字符串转换为 int,只需使用.toInt()它,它就会为您提供一个整数值以供使用。至于换键盘,你只能在 iPhone 上调出一个键盘——如果你在 iPad 上尝试,它只会显示键盘的数字侧,但用户可以改回普通字母,因此你需要一个自定义iPad 上的键盘。尽管如此,以下是在 iPhone 上获取键盘的代码:

textField.keyboardType = UIKeyboardType.NumberPad

Hope that helps!

希望有帮助!

回答by Dhruv Ramani

You could try this:

你可以试试这个:

@IBAction var text1:UITextField!
@IBAction var text2:UITextField!
@IBAction var Answer:UILabel!

@IBAction func buttonClicked(sender:UIButton){
  Answer.text="\(text1.text.toInt()/text2.text.toInt())"
}