当我尝试编译Swift项目时,我遇到了分段错误。
以下是我的代码的简化版本,它会导致错误:
enum PluginType
{
case PluginC
case PluginD
}
class Plugin
{
var pluginType : PluginType
var setter : Void -> Void
var unsetter : Void -> Void
init(pluginType : PluginType, setter : Void -> Void, unsetter : Void -> Void)
{
self.pluginType = pluginType
self.setter = setter
self.unsetter = unsetter
}
}
@objc protocol BProtocol
{
func getPlugins() -> [Plugin]
}
class B : BProtocol
{
var a : AProtocol?
func getPlugins() -> [Plugin]
{
return [Plugin(pluginType : .PluginC, setter : a!.setPluginC, unsetter : a!.unsetPluginC),
Plugin(pluginType : .PluginD, setter : a!.setPluginD, unsetter: a!.unsetPluginD)]
}
}
@objc protocol AProtocol
{
func setPluginC()
func unsetPluginC()
func setPluginD()
func unsetPluginD()
}
class A : AProtocol
{
var b : BProtocol?
func setPluginC()
{
}
func unsetPluginC()
{
}
func setPluginD()
{
}
func unsetPluginD()
{
}
}
问题来自这种方法:
func getPlugins() -> [Plugin]
{
return [Plugin(pluginType : .PluginC, setter : a!.setPluginC, unsetter : a!.unsetPluginC),
Plugin(pluginType : .PluginD, setter : a!.setPluginD, unsetter: a!.unsetPluginD)]
}
问题是由我初始化的setter和unsetter引起的。
这是Xcode上的错误:
0 swift 0x000000010456a028 llvm :: sys :: PrintStackTrace(__ sFILE *)+ 40 1 swift
0x000000010456a514 SignalHandler(int)+ 452 2 libsystem_platform.dylib 0x00007fff9596f5aa _sigtramp + 26 3 libsystem_platform.dylib 0x000007f83527198b _sigtramp + 2677023739 4 swift 0x00000001049290df swift :: AbstractStorageDecl :: hasObjCGetterAndSetter()const + 31 5 swift 0x0000000104929205 swift :: AbstractStorageDecl :: requiresObjCGetterAndSetter()const + 69 6 swift 0x00000001039ff3f4 迅速降低:: :: SILGenFunction :: emitGetAccessor(SWIFT :: SILLocation, swift :: AbstractStorageDecl *,llvm :: ArrayRef, swift :: Lowering :: RValueSource&&,bool,swift :: Lowering :: RValue&&, swift :: Lowering :: SGFContext)+ 68 7 swift
0x0000000103a15ca4 迅速降低:: :: SILGenFunction :: emitRValueForPropertyLoad(SWIFT :: SILLocation, swift :: Lowering :: ManagedValue,bool,swift :: VarDecl *, llvm :: ArrayRef,bool,swift :: Type, swift :: Lowering :: SGFContext)+ 1284 8 swift
0x0000000103a2a012 swift :: ASTVisitor<(匿名 namespace):: RValueEmitter,swift :: Lowering :: RValue,void,void,void, void,void,swift :: Lowering :: SGFContext> :: visit(swift :: Expr *, swift :: Lowering :: SGFContext)+ 28402 9 swift
0x0000000103a2b6ed 迅速降低:: :: SILGenFunction :: emitRValueAsSingleValue(SWIFT :: Expr的* swift :: Lowering :: SGFContext)+ 45 10 swift
0x00000001039f3720 迅速降低:: :: RValueSource :: getAsSingleValue(SWIFT ::降低:: SILGenFunction和放大器;, swift :: Lowering :: SGFContext)&& + 176 11 swift
0x0000000103a44154 迅速降低:: :: RValueSource :: forwardInto(SWIFT ::降低:: SILGenFunction和放大器;, swift :: Lowering :: AbstractionPattern,swift :: Lowering :: Initialization *, swift ::降低:: TypeLowering const&)&& + 260 12 swift
0x0000000103a4400e 迅速降低:: :: RValueSource ::兑现(迅速降低:: :: SILGenFunction和放大器;, swift :: Lowering :: AbstractionPattern,swift :: SILType)&& + 334 13迅速 0x0000000103a340fc 迅速降低:: :: SILGenFunction :: emitInjectOptionalValueInto(SWIFT :: SILLocation, swift :: Lowering :: RValueSource&&,swift :: SILValue, swift :: Lowering :: TypeLowering const&)+ 268 14 swift
0x0000000103a2b1fb swift :: ASTVisitor<(匿名 namespace):: RValueEmitter,swift :: Lowering :: RValue,void,void,void, void,void,swift :: Lowering :: SGFContext> :: visit(swift :: Expr *, swift :: Lowering :: SGFContext)+ 32987 15 swift
0x0000000103a2a325 swift :: ASTVisitor<(匿名 namespace):: RValueEmitter,swift :: Lowering :: RValue,void,void,void, void,void,swift :: Lowering :: SGFContext> :: visit(swift :: Expr *, swift :: Lowering :: SGFContext)+ 29189 16 swift
0x0000000103a2b63f swift :: Lowering :: SILGenFunction :: emitIgnoredExpr(swift :: Expr *)+ 191 17 swift 0x0000000103a483f9 swift ::降低:: SILGenFunction :: visitBraceStmt(swift :: BraceStmt *)+ 297 18 swift 0x0000000103a4b2d4 swift :: ASTVisitor :: visit(swift :: Stmt *)+ 148 19 swift
0x0000000103a188c3 swift :: Lowering :: SILGenFunction :: emitFunction(swift :: FuncDecl *)+ 243 20 swift 0x00000001039f8646 swift :: Lowering :: SILGenModule :: emitFunction(swift :: FuncDecl *)+ 230 21 swift 0x0000000103a12763 swift :: ASTVisitor :: visit(swift :: Decl *)+ 355 22 swift
0x0000000103a1112b SILGenType :: emitType()+ 203 23 swift
0x0000000103a0c0fe 迅速降低:: :: SILGenModule :: visitNominalTypeDecl(SWIFT :: NominalTypeDecl *) + 30 24 swift 0x00000001039fa5cb swift ::降低:: SILGenModule :: emitSourceFile(swift :: SourceFile *, unsigned int)+ 411 25 swift 0x00000001039fa88f swift :: SILModule :: constructSIL(swift :: Module *,swift :: SourceFile *, swift ::可选)+ 335 26 swift
0x00000001039fa9d8 swift :: performSILGeneration(swift :: SourceFile&amp ;,, swift ::可选)+ 72 27 swift
0x00000001038dfda9 frontend_main(llvm :: ArrayRef,char const *,void *)+ 3353 28 swift 0x00000001038dd94d main + 1677 29 libdyld.dylib 0x00007fff99b5c5fd start + 1 30 libdyld.dylib 0x000000000000015a start + 1716140894
我正在寻找一种解决方法来避免"分段错误"用编译器。
有什么建议吗?
谢谢!
答案 0 :(得分:1)
最后,我找到了一个解决方法:
func getPlugins() -> [Plugin]
{
return [Plugin(pluginType : .PluginC, setter : { a!.setPluginC() }, unsetter : { a!.unsetPluginC() }),
Plugin(pluginType : .PluginD, setter : { a!.setPluginD() }, unsetter: { a!.unsetPluginD() })]
}
这适用于Xcode 6.1