Swift中的以下代码引发NSInvalidArgumentException异常:
task = NSTask()
task.launchPath = "/SomeWrongPath"
task.launch()
Run Code Online (Sandbox Code Playgroud)
我该如何捕捉异常?据我所知,Swift中的try/catch是针对Swift中抛出的错误,而不是针对像NSTask这样的对象引发的NSExceptions(我猜这是用ObjC编写的).我是Swift的新手,所以可能是我错过了一些明显的东西......
编辑:这是一个关于bug的雷达(特别是对于NSTask):openradar.appspot.com/22837476
是否有可能在Swift中捕获异常?给出以下代码:
NSException.raise(NSRangeException,
format: "Now you've gone too far!",
arguments: CVaListPointer(fromUnsafePointer: UnsafePointer()))
Run Code Online (Sandbox Code Playgroud)
是否可以防止异常崩溃整个程序?也就是说,Objective-C中Swift等效的是以下内容:
@try {
[NSException raise:NSRangeException format:@"Now you've gone too far!"];
}
Run Code Online (Sandbox Code Playgroud) 我使用NSExpression来解决数学公式.如果输入公式正确,则Everthing工作正常,但如果输入的公式如下:
6-
Run Code Online (Sandbox Code Playgroud)
我的应用程序因以下错误消息而终止:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "6- == 1"'
Run Code Online (Sandbox Code Playgroud)
在Object-C中,可以将此代码放入@try块中.swift中有同等的东西吗?或者我该如何处理这个例外?
这是我简单的NSExpression代码:
let expression = NSExpression(format: finalFormula)
if let result = expression.expressionValueWithObject(nil, context: nil) as? NSNumber {
return result
}
Run Code Online (Sandbox Code Playgroud)
我解决方案是首先检查输入,但如果我回到零或类似的东西,我会对我好.
我想验证用户创建的表达式(如“2+2”、“5+7”或更复杂的)。我使用 NSExpression 类来解析和计算这个表达式。这是我的游乐场代码:
import UIKit
let string = "2+2"
var ex:NSExpression?
do {
ex = NSExpression(format: string)
}
catch {
print("String is not valid expression")
}
if let result = ex?.expressionValue(with: nil, context: nil) as! NSNumber? {
print("result is \(result)")
}
Run Code Online (Sandbox Code Playgroud)
当我使用有效表达式(“2+2”)时 - 我得到了结果。但有时用户可能会提供错误的字符串(例如“2+”)。有了这个字符串,我的应用程序崩溃了:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "2+ == 1"'
Run Code Online (Sandbox Code Playgroud)
我不明白我如何捕捉这个异常以及为什么上面的代码不这样做。现在我使用 Objective C 类(具有相同的逻辑),从我的 swift 代码中调用这个方法,在那个类中我真的可以捕获这样的异常:
+(NSNumber *)solveExpression:(NSString *)string
{
id value;
@try {
NSExpression *ex = [NSExpression …Run Code Online (Sandbox Code Playgroud) exception-handling nsexpression swift objective-c-swift-bridge
在我的代码中我将字符串作为数学表达式进行评估,例如:
NSString *formula=@"9*7";
NSExpression *expr =[NSExpression expressionWithFormat:formula];
NSLog(@"%@", [[expr expressionValueWithObject:nil context:nil]intValue]);
Run Code Online (Sandbox Code Playgroud)
以上工作正常但我将处理来自用户的动态输入,因此我需要能够在用户输入错误数据时捕获异常,因此我需要能够在以下情况下捕获异常:
NSString *formula=@"9*"; //note the deliberately invalid expression
NSExpression *expr =[NSExpression expressionWithFormat:formula];
@try {
[[expr expressionValueWithObject:nil context:nil]intValue];
}
@catch (NSException *exception) {
NSLog(@"Exception");
}
@finally {
NSLog(@"Finally");
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此代码时,我得到:
因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'无法解析格式字符串"9*== 1"'
有没有办法捕捉这个例外?或者是否有一些方法来测试表达式在我传递之前是否有效?
谢谢!
我有一个快速的功能来从主要包中获取本地化的应用程序名称localizedInfoDictionary以及一些后备情况.
private func defaultAppName() -> String {
var name: NSString = ""
// Check for a localized version of the CFBundleDisplayName
var mainBundle = NSBundle.mainBundle()
// EXC_BAD_ACCESS HERE, despite the optional
var mainBundleInfoDictionary: Dictionary<NSObject, AnyObject>? = mainBundle.localizedInfoDictionary
if let infoDictionary = mainBundleInfoDictionary {
name = infoDictionary["CFBundleDisplayName"] as NSString
if (name.length == 0) {
name = infoDictionary[kCFBundleNameKey] as NSString
}
}
if (name.length == 0) {
mainBundleInfoDictionary = mainBundle.infoDictionary
if let infoDictionary = mainBundleInfoDictionary {
name = infoDictionary["CFBundleDisplayName"] as …Run Code Online (Sandbox Code Playgroud) swift ×5
ios ×3
exception ×2
foundation ×2
nsexpression ×2
cocoa-touch ×1
ios8 ×1
nsexception ×1
try-catch ×1