我有一个扩展了NSObject
的Obj-C类。它的属性之一是NS_ENUM
;我们称之为direction
。
然后,我有一个Swift类,带有这个Obj-C类的可选属性;我们称之为message
。
以下示例代码很好:
switch message?.direction
{
case .Inbound:
...
case .OutBound:
...
case nil:
...
}
但是,如果我再介绍:
guard let message = self.message else { return }
并删除上面代码中的?
,编译器崩溃;参见下文。
它似乎被case nil:
弄糊涂了,因为如果我删除崩溃,它就会消失;当然不再需要它。
很好奇,有人见过这个Swift编译器崩溃吗?
1. Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
2. While emitting IR SIL function "@$s7SomeFunction...".
for 'update()' (at /Users/.../SomeFile.swift:83:11)
0 swift 0x00000001133554ea PrintStackTraceSignalHandler(void*) + 42
1 swift 0x0000000113354cc0 SignalHandler(int) + 352
2 libsystem_platform.dylib 0x00007fff6b8d75fd _sigtramp + 29
3 swift 0x000000010f1aaa4c (anonymous namespace)::SinglePayloadEnumImplStrategy::consume(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Atomicity) const + 1244
4 swift 0x00000001131d082c llvm::IRBuilderBase::CreateLifetimeEnd(llvm::Value*, llvm::ConstantInt*) + 28
5 swift 0x000000010f2105a1 swift::irgen::FixedTypeInfo::deallocateStack(swift::irgen::IRGenFunction&, swift::irgen::StackAddress, swift::SILType) const + 81
6 swift 0x000000010f2e8244 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 33828
7 swift 0x000000010f2dc8e3 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8835
8 swift 0x000000010f190032 swift::irgen::IRGenerator::emitGlobalTopLevel() + 1410
9 swift 0x000000010f2baed9 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1097
10 swift 0x000000010f0b236f performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, bool, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, bool, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 4255
11 swift 0x000000010f0a77c0 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 48416
12 swift 0x000000010f01f4d3 main + 1283
13 libdyld.dylib 0x00007fff6b6decc9 start + 1
error: Segmentation fault: 11 (in target 'SomeApp' from project 'SomeApp')
我建议像这样安全地解开message.direction。>
If let msg.dir = message?.direction {
Run your switch statment here without nill case
} else{
Do what you want in nill case
}