Basic Idea, note that this only works in Swift 1.x (check out ParaSara’s answer to see how it works in Swift 2.x):
// toInt returns optional that's why we used a:Int? let a:Int? = firstText.text.toInt() // firstText is UITextField let b:Int? = secondText.text.toInt() // secondText is UITextField // check a and b before unwrapping using ! if a && b { var ans = a! + b! answerLabel.text = "Answer is \(ans)" // answerLabel ie UILabel } else { answerLabel.text = "Input values are not numeric" }
Update for Swift 4
... let a:Int? = Int(firstText.text) // firstText is UITextField let b:Int? = Int(secondText.text) // secondText is UITextField ...
Related Posts:
- UITableView example for Swift
- libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
- libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
- Xcode 10.2.1 Command PhaseScriptExecution failed with a nonzero exit code
- How do you create a Swift Date object?
- Swift 4 Attempt to present ViewController whose view is not in the window hierarchy
- Xcode – How to fix ‘NSUnknownKeyException’, reason: … this class is not key value coding-compliant for the key X” error?
- Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy
- Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy
- Where is the variable “code” in SKError defined?
- Command CompileSwift failed with a nonzero exit code in Xcode 10
- Always get build error : No such module ‘Alamofire’
- what does Error “Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)” mean?
- Date Format in Swift
- What the meaning of question mark ‘?’ in swift?
- Loaded nib but the ‘view’ outlet was not set
- Could not insert new outlet connection: Could not find any information for the class named
- Swift Error: Variable used within its own initial value
- Prepare for Segue in Swift
- How to play a sound using Swift?
- App iOS stuck on blank white screen when build release React Native
- iOS Simulator – Can’t log in with iCloud
- Load Animated gif in UIImageView IOS
- iOS: Random black screen when loading the app
- Drawing Hexagon grid in Cocos 2d-x
- iPhone system font
- How to write C++ on Mac?
- Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3
- Linker Command failed with exit code 1 (use -v to see invocation), Xcode 8, Swift 3
- A server with the specified hostname could not be found
- Where is redeem code for public link for TestFlight?
- Xcode 10, Command CodeSign failed with a nonzero exit code
- What are the difference between macOS and iOS?
- Xcode couldn’t find any provisioning profiles matching
- symbol(s) not found for architecture x86_64 in xcode 10
- CFNetwork SSLHandshake failed (-9824) NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)
- Is there a private messaging UI (inbox, sent, compose) framework for iOS?
- ‘strcmp’ was not declared in this scope
- malloc: *** error: incorrect checksum for freed object – object was probably modified after being freed
- NSURLErrorDomain error codes description
- Duplicate symbols for architecture x86_64 under Xcode
- Print: Entry, “:CFBundleIdentifier”, Does Not Exist
- Xcode 9 – “Fixed Width Constraints May Cause Clipping” and Other Localization Warnings
- iPhone 6 and 6 Plus Media Queries
- Got a EXC_CORPSE_NOTIFY symbol crash, how to track it?
- “Could not find any information for class named ViewController”
- warning: ‘characters’ is deprecated: Please use String or Substring directly
- com.apple.WebKit.WebContent drops 113 error: Could not find specified service
- What is the meaning of ‘No bundle URL present’ in react-native?
- Convert .cer to .p12
- Add a simple UIView as header of UICollectionView
- UIButton title text color
- Xcode: Could not locate device support files
- Remove alpha channel in an image
- Pod install is staying on “Setting up CocoaPods Master repo”
- Download Xcode simulator directly
- Alert”Developer tools access needs to take control of another process for debugging to continue.Type your password to allow this.”
- Could not load NIB in bundle
- Xcode error “Could not find Developer Disk Image”
- IOS home screen icons: where put and what size?
- Does webp images not shows on ios
- Generate unique random numbers between 1 and 100
- How do you round UP a number?
- How do you round UP a number?
- What is the maximum value for an int32?
- Why has the int32 type a maximum value of 2³¹ − 1? [duplicate]
- Converting integer to string in Python
- How to convert a string to an integer in JavaScript?
- How to convert an int to string in C?
- Visual List of iOS Fonts?
- Differences between arm64 and aarch64
- How to convert an int to string in C?
- What does value & 0xff do in Java?
- Maximum and Minimum values for ints
- Maximum and Minimum values for ints
- Safest way to convert float to integer in python?
- How do I parse a string to a float or int?
- Round a floating-point number down to the nearest integer?
- max value of integer
- Converting characters to integers in Java
- How to concatenate a std::string and an int
- How do I generate random integers within a specific range in Java?
- Type of expression is ambiguous without more context Swift
- What does “Fatal error: Unexpectedly found nil while unwrapping an Optional value” mean?
- Alternative to itoa() for converting integer to string C++?
- How do I generate random integers within a specific range in Java?
- Signed versus Unsigned Integers
- Error Running React Native App From Terminal (iOS)
- How do you express binary literals in Python?
- Strange error nw_protocol_get_quic_image_block_invoke dlopen libquic failed
- Add swipe left/right to web page, but use default swipe up/down
- Maximum and Minimum values for ints
- How to convert an int to string in C?
- Generating random whole numbers in JavaScript in a specific range?
- Python: TypeError: cannot concatenate ‘str’ and ‘int’ objects
- How can I convert a character to a integer in Python, and viceversa?
- How to convert an int to string in C?
- MpMovie Player not rotating to land scape mode
- Python Error io.UnsupportedOperation: not readable
- How can I properly compare two Integers in Java?