在swift中,如果进入else块,是否可以使用short guard let try?
并获取发生的异常?
guard let smth = try? myThrowingFunc() else {
print(error) //can I access the exception here somehow?
return
}
Run Code Online (Sandbox Code Playgroud)
VS
let smth: AnyObject?
do {
smth = try myThrowingFunc()
} catch let error {
print(error)
return
}
Run Code Online (Sandbox Code Playgroud) 是否有可能在Swift中捕获NSInternalInconsistencyException?如果是的话,怎么办呢?
有时会发生这种情况 - 从代码中的不同位置 - 我们想要做的UIViewController.presentViewController(a, b, c)
,有时我们已经提出了这种情况,在这种情况下我们得到:
Warning: Attempt to present * on * which is already presenting *
有可能打破这个警告吗?我该如何为此设置断点?
我有一个BaseClass,其方法返回一个类对象,我有一个DerivedClass.现在,当我有一个DerivedClass对象并调用BaseClass中定义的方法时,返回值是ob类型BaseClass,遗憾的是不是DerivedClass类型.
class BaseClass {
public:
typeof(*this) myMethod1() {return *this;} // nice if that would work
BaseClass& myMethod2() {return *this;}
BaseClass myMethod3() {return BaseClass();}
};
class DerivedClass : public BaseClass {};
DerivedClass tmp;
tmp.myMethod1();
tmp.myMethod2();
tmp.myMethod3();
// all three methods should return an object of type DerivedClass,
// but really they return an object of type BaseClass
Run Code Online (Sandbox Code Playgroud)
所以我希望实现的是使用超类的方法,但是使用派生类的返回类型(自动转换?).myMethod1()是我唯一能想到的,但它不起作用.
我搜索过但没有找到任何令人满意的东西.
swift ×3
exception ×2
breakpoints ×1
c++ ×1
casting ×1
guard ×1
inheritance ×1
ios ×1
objective-c ×1
return ×1
return-type ×1
try-catch ×1