2014-06-30 62 views
0

當我嘗試使用下面的代碼列出系統中的可用字體時,我收到附帶的錯誤消息。我的代碼有什麼問題,或者這是一個編譯器錯誤?for循環中的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

如果編譯器崩潰,那麼這是一個編譯器錯誤(無論您的代碼有多麼錯誤)。 –

回答

-1

兩個,我想。你的代碼是錯誤的,它暴露了一個編譯器錯誤。它似乎在吹捧類型斷言String!。我認爲這是因爲UIFont.familyNames()的返回類型是AnyObject[]!。這裏的!表示它被強制解包,所以你期望得到AnyObject[]而不是Optional<AnyObject>。所以String!不是Optional,這應該是一個錯誤的消息,而不是崩潰的Xcode。

你需要做的,而不是什麼是告訴編譯器的AnyObject []它正在恢復的類型爲字符串[]:

for fontFamilyName in UIFont.familyNames() as String[] { 
    for fontName in UIFont.fontNamesForFamilyName(fontFamilyName) as String[] { 
     println("font: \(fontName)") 
    } 
} 
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.