11

我正在尝试这个是计算器的代码。如何处理来自用户的无效输入?

//答案:将标头桥接到 Objective-C// https://github.com/kongtomorrow/TryCatchFinally-Swift

这是同样的问题,但在 objc 中,但我想迅速做到这一点。从 NSExpression 捕获 NSInvalidArgumentException

如果它不起作用,我只想显示一条消息,但是现在当用户没有输入正确的格式时我得到一个异常。

import Foundation

var equation:NSString = "60****2"  // This gives a NSInvalidArgumentException', 
let expr = NSExpression(format: equation) // reason: 'Unable to parse the format string
if let result = expr.expressionValueWithObject(nil, context: nil) as? NSNumber {
    let x = result.doubleValue
    println(x)
} else {
    println("failed")
}
4

3 回答 3

13

更多“Swifty”解决方案:

@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

这将生成 Swift 代码:

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

你可以使用它try

do {
    try TryCatch.tryBlock {
        let expr = NSExpression(format: "60****2")
        ...
    }
} catch {
    // Handle error here
}
于 2015-11-20T19:15:10.167 回答
8

这在 Swift 2 中仍然是一个问题。如前所述,最好的解决方案是使用桥接头并在 Objective C 中捕获 NSException。

https://medium.com/swift-programming/adding-try-catch-to-swift-71ab27bcb5b8描述了一个很好的解决方案,但确切的代码在 Swift 2 中无法编译,因为try现在catch是保留关键字。您需要更改方法签名以解决此问题。这是一个例子:

// 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
于 2015-09-06T21:38:23.310 回答
2

https://github.com/kongtomorrow/TryCatchFinally-Swift编辑一个不错的解决方案:

首先创建TryCatch.h&TryCatch.m并将它们桥接到 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();
    }
}

然后TryCatch在 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)
    }
}

最后,使用它!:)

`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)")
}
于 2016-01-21T10:56:06.233 回答