Catch an exception for invalid user input in swift

前端 未结 3 1253
悲哀的现实
悲哀的现实 2020-11-28 16:02

I am trying this code that is a calculator. How can I handle input from the user that is not valid?

//ANSWER: Bridging header to Objective-C// https://github.com/kon

相关标签:
3条回答
  • 2020-11-28 16:15

    A nice solution editing from https://github.com/kongtomorrow/TryCatchFinally-Swift:

    First create TryCatch.h & TryCatch.m and bridge them to Swift:

    TryCatch.h

    #import <Foundation/Foundation.h>
    
    void tryCatch(void(^tryBlock)(), void(^catchBlock)(NSException *e), void(^finallyBlock)());
    

    TryCatch.m

    #import <Foundation/Foundation.h>
    
    void tryCatch(void(^tryBlock)(), void(^catchBlock)(NSException *e), void(^finallyBlock)()) {
        @try {
            tryBlock();
        }
        @catch (NSException *exception) {
            catchBlock(exception);
        }
        @finally {
            finallyBlock();
        }
    }
    

    Then create the class TryCatch in Swift:

    func `try`(`try`:()->()) -> TryCatch {
        return TryCatch(`try`)
    }
    class TryCatch {
        let tryFunc : ()->()
        var catchFunc = { (e:NSException!)->() in return }
        var finallyFunc : ()->() = {}
    
        init(_ `try`:()->()) {
            tryFunc = `try`
        }
    
        func `catch`(`catch`:(NSException)->()) -> TryCatch {
            // objc bridging needs NSException!, not NSException as we'd like to expose to clients.
            catchFunc = { (e:NSException!) in `catch`(e) }
            return self
        }
    
        func finally(finally:()->()) {
            finallyFunc = finally
        }
    
        deinit {
            tryCatch(tryFunc, catchFunc, finallyFunc)
        }
    }
    

    Finally, use it! :)

    `try` {
        let expn = NSExpression(format: "60****2")
    
        //let resultFloat = expn.expressionValueWithObject(nil, context: nil).floatValue
        // Other things...
        }.`catch` { e in
            // Handle error here...
            print("Error: \(e)")
    }
    
    0 讨论(0)
  • 2020-11-28 16:18

    This is still an issue in Swift 2. As noted, the best solution is to use a bridging header and catch the NSException in Objective C.

    https://medium.com/swift-programming/adding-try-catch-to-swift-71ab27bcb5b8 describes a good solution, but the exact code doesn't compile in Swift 2 because try and catch are now reserved keywords. You'll need to change the method signature to workaround this. Here's an example:

    // https://medium.com/swift-programming/adding-try-catch-to-swift-71ab27bcb5b8
    
    @interface TryCatch : NSObject
    
    + (void)tryBlock:(void (^)())try catchBlock:(void (^)(NSException *))catch finallyBlock:(void (^)())finally;
    
    @end
    
    @implementation TryCatch
    
    + (void)tryBlock:(void (^)())try catchBlock:(void (^)(NSException *))catch finallyBlock:(void (^)())finally {
        @try {
            try ? try() : nil;
        }
        @catch (NSException *e) {
            catch ? catch(e) : nil;
        }
        @finally {
            finally ? finally() : nil;
        }
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-28 16:20

    More "Swifty" solution:

    @implementation TryCatch
    
    + (BOOL)tryBlock:(void(^)())tryBlock
               error:(NSError **)error
    {
        @try {
            tryBlock ? tryBlock() : nil;
        }
        @catch (NSException *exception) {
            if (error) {
                *error = [NSError errorWithDomain:@"com.something"
                                             code:42
                                         userInfo:@{NSLocalizedDescriptionKey: exception.name}];
            }
            return NO;
        }
        return YES;
    }
    
    @end
    

    This will generate Swift code:

    class func tryBlock((() -> Void)!) throws
    

    And you can use it with try:

    do {
        try TryCatch.tryBlock {
            let expr = NSExpression(format: "60****2")
            ...
        }
    } catch {
        // Handle error here
    }
    
    0 讨论(0)
提交回复
热议问题