for循环中的Swift编译器错误

时间:2014-06-30 18:19:40

标签: swift

当我尝试使用下面的代码列出系统中的可用字体时,我收到了错误消息。我的代码有什么问题,还是编译错误?

for fontFamilyName: String! in UIFont.familyNames() {
  for fontName: String! in UIFont.fontNamesForFamilyName(fontFamilyName) {
    println("font: \(fontName)")
  }
}

错误讯息:

0  swift                    0x0000000101678e08 llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x00000001016792f4 SignalHandler(int) + 452
2  libsystem_platform.dylib 0x00007fff8968a5aa _sigtramp + 26
3  libsystem_platform.dylib 0x0000000102a79a00 _sigtramp + 2034168944
4  swift                    0x00000001015759b7 llvm::StoreInst::StoreInst(llvm::Value*, llvm::Value*, bool, llvm::Instruction*) + 55
5  swift                    0x0000000100aca412 swift::irgen::IRBuilder::CreateStore(llvm::Value*, llvm::Value*, swift::irgen::Alignment) + 66
6  swift                    0x0000000100a9737c (anonymous namespace)::PayloadEnumImplStrategyBase::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 124
7  swift                    0x0000000100abd070 swift::irgen::SequentialTypeInfo<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo, true>::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 224
8  swift                    0x0000000100abd070 swift::irgen::SequentialTypeInfo<(anonymous namespace)::LoadableStructTypeInfo, swift::irgen::LoadableTypeInfo, (anonymous namespace)::StructFieldInfo, true>::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address) const + 224
9  swift                    0x0000000100ae5a48 swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 31240
10 swift                    0x0000000100add846 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8678
11 swift                    0x0000000100a5ecd8 swift::irgen::IRGenModule::emitGlobalTopLevel() + 184
12 swift                    0x0000000100acacc3 performIRGeneration(swift::IRGenOptions&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1859
13 swift                    0x0000000100acb613 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 51
14 swift                    0x0000000100a3d95a frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 4842
15 swift                    0x0000000100a3c65d main + 1533
16 libdyld.dylib            0x00007fff889d05fd start + 1
    1.  While emitting IR SIL function @_TFC10SwiftTests11AppDelegate11applicationfS0_FTCSo13UIApplication29didFinishLaunchingWithOptionsGSqCSo12NSDictionary__Sb for 'application' at /Users/hb/Developer/martsys/SwiftTests/SwiftTests/AppDelegate.swift:15:3

<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)

2 个答案:

答案 0 :(得分:0)

通过强制转换类方法返回的数组,你会更好:

for fontFamilyName in UIFont.familyNames() as Array<String!> {
    for fontName in UIFont.fontNamesForFamilyName(fontFamilyName) as Array<String!> {
        println("font: \(fontName)")
    }
}

// prints fontFamilyName: Marion,
//        font: Marion-Italic
//        font: Marion-Bold
//        font: Marion-Regular
//        fontFamilyName: Copperplate
//        font: Copperplate-Light
// etc.

答案 1 :(得分:-1)

我认为。您的代码是错误的,它暴露了编译器错误。它似乎在吹嘘类型断言String!。我认为这是因为UIFont.familyNames()的返回类型是AnyObject[]!。这里的!表示它被强制解包,因此您希望获得AnyObject[]而不是Optional<AnyObject>。所以String!不是Optional,这应该是错误的消息,而不是崩溃的Xcode。

你需要做的是告诉编译器它返回的AnyObject []类型为String []:

for fontFamilyName in UIFont.familyNames() as String[] {
    for fontName in UIFont.fontNamesForFamilyName(fontFamilyName) as String[] {
        println("font: \(fontName)")
    }
}