在swift中捕获无效用户输入的异常 [英] Catch an exception for invalid user input in swift

查看:168
本文介绍了在swift中捕获无效用户输入的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



// ANSWER:将标题连接到Objective-C // https://github.com/kongtomorrow/TryCatchFinally-Swift



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



全部我想显示是一个消息,如果它不起作用,但现在我得到一个例外,当用户没有输入正确的格式。

  import Foundation 

var equation:NSString =60 **** 2// This给出一个NSInvalidArgumentException',
let expr = NSExpression(format:equation)// reason:'如果let result = expr.expressionValueWithObject(nil,context:nil),则无法解析格式字符串
? NSNumber {
let x = result.doubleValue
println(x)
} else {
println(failed)
}


解决方案

这仍然是Swift 2中的一个问题。如上所述,最好的解决方案是使用桥接头并捕获目标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- (void(^)())try catchBlock:(void(^)(NSException *)(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


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/kongtomorrow/TryCatchFinally-Swift

Here is the same question but in objc but I want to do this in swift. Catching NSInvalidArgumentException from NSExpression

All I want to show is a message if it doesn't work, but now I am getting an exception when the user doesn't input the correct format.

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")
}

解决方案

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

这篇关于在swift中捕获无效用户输入的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆