1

My app have to use int to do some multiplication, it is easy to meet two fairly big numbers' multiplication.

Of course it will crash. And how can I remark some bool value. Just like every time before we'll quit the app, we saveData in the AppDelegate.swift's function:

func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
Jobert
  • 1,532
  • 1
  • 13
  • 37
Microos
  • 1,728
  • 3
  • 17
  • 34

2 Answers2

3

If the result of an integer arithmetic operation (+, -, *, /, ...) overflows, the application terminates immediately. There is no way to catch this situation or to get notified e.g. to save data. There is no Swift error or NSException thrown which you could catch. The same would happen for other runtime errors like accessing an array element outside of the valid bounds, or unwrapping an optional which is nil.

This means that you have to check beforehand if the integer arithmetic operation can be executed. Alternatively – depending on your needs – you can

  • use the "Overflow operators" &+, &- and &* instead, which truncate the result instead of triggering an error, similar as in (Objective-)C.
  • use addingReportingOverflow() and similar methods which “return the sum of this value and the given value, along with a Boolean value indicating whether overflow occurred in the operation.”
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • it seems &+,&* can handle something like [Int.max*Int.max], So if there is no int-overflow the result is nothing different from using +,* , right? – Microos Nov 07 '15 at 09:21
  • @Microos: If the operation does not overflow then the result of `&+` is identical to the result of `+`. – Martin R Nov 07 '15 at 09:25
  • @ Martin R thank you! I will do some trials on it, that's really help me a lot – Microos Nov 07 '15 at 09:26
  • How do you accomplish "... you have to check beforehand if the integer arithmetic operation can be executed."? And &+ allows overflow rather than truncating – George White Oct 18 '20 at 06:50
  • 1
    @GeorgeWhite: I meant “overflow” in the sense of “the result exceeds the available range and the program terminates immediately”, and “truncate” in the sense of “the result is truncated to the number of available bits”. – I have added another option which allows you to compute the sum *and* test for overflow in a single operation. – Martin R Oct 18 '20 at 07:27
  • Thanks - and I misunderstood "truncate" to mean "limit to the maximum value" rather than live with the available bits. – George White Oct 18 '20 at 21:56
0

You should prefer NSInteger over Int.

Use NSInteger when you don't know what kind of processor architecture your code might run on, so you may for some reason want the largest possible int type, which on 32 bit systems is just an int, while on a 64-bit system it's a long.

stick with using NSInteger instead of int/long unless you specifically require them.

NSInteger/NSUInteger are defined as *dynamic typedef *s to one of these types, and they are defined like this:

#if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
Rahul
  • 5,594
  • 7
  • 38
  • 92
  • Hey buddy, thank you for coming and help me, but those data sometime is bigger than Int64.max, so I this is not able for me to handle then :( – Microos Nov 07 '15 at 09:28