子类化NSObject并使用泛型时Swift编译错误

前端之家收集整理的这篇文章主要介绍了子类化NSObject并使用泛型时Swift编译错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以下 Swift代码在构建时生成编译错误
import Foundation

class Wrapper<T> : NSObject {
    let obj : T

    init(x : T) {
        self.obj = x
    }
}

我做错了什么或是编译器错误

如果是这样,我该怎么办呢?

错误日志:

CompileSwift normal i386 com.apple.xcode.tools.swift.compiler
    cd /Users/hermespique/workspace/HanekeSwift
    export PATH="/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode6-Beta.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -target i386-apple-ios8.0 -module-name HanekeTests -O0 -sdk /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -g -module-cache-path /Users/hermespique/Library/Developer/Xcode/DerivedData/ModuleCache -I /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator -F /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -c -j4 /Users/hermespique/workspace/HanekeSwift/HanekeTests/XCTestCase+Assert.swift /Users/hermespique/workspace/HanekeSwift/HanekeTests/Cache.swift /Users/hermespique/workspace/HanekeSwift/HanekeTests/Wrapper.swift -output-file-map /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/HanekeTests-OutputFileMap.json -serialize-diagnostics -emit-dependencies -emit-module -emit-module-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/HanekeTests.swiftmodule -Xcc -iquote -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-generated-files.hmap -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-own-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-project-headers.hmap -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator/include -Xcc -I/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/DerivedSources/i386 -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DDEBUG=1 -emit-objc-header -emit-objc-header-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/HanekeTests-Swift.h

0  swift                    0x000000010a889608 llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x000000010a889af4 SignalHandler(int) + 452
2  libsystem_platform.dylib 0x00007fff88e185aa _sigtramp + 26
3  libsystem_platform.dylib 0x00007fff55fb0334 _sigtramp + 3440999844
4  swift                    0x0000000109c87395 swift::irgen::IRGenFunction::emitTypeMetadataRef(swift::CanType) + 21
5  swift                    0x0000000109cbcfb8 (anonymous namespace)::OpaqueArchetypeTypeInfo::destroy(swift::irgen::IRGenFunction&,swift::irgen::Address,swift::CanType) const + 24
6  swift                    0x0000000109cf467b swift::SILVisitor<(anonymous namespace)::IRGenSILFunction,void>::visit(swift::ValueBase*) + 23579
7  swift                    0x0000000109cee266 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8678
8  swift                    0x0000000109c6f6f8 swift::irgen::IRGenModule::emitGlobalTopLevel() + 184
9  swift                    0x0000000109cdb6e3 performIRGeneration(swift::IRGenOptions&,swift::Module*,swift::SILModule*,llvm::StringRef,llvm::LLVMContext&,swift::SourceFile*,unsigned int) + 1859
10 swift                    0x0000000109cdc033 swift::performIRGeneration(swift::IRGenOptions&,swift::SourceFile&,unsigned int) + 51
11 swift                    0x0000000109c4e65a frontend_main(llvm::ArrayRef<char const*>,char const*,void*) + 4842
12 swift                    0x0000000109c4d35d main + 1533
13 libdyld.dylib            0x00007fff8ddbb5fd start + 1
14 libdyld.dylib            0x0000000000000040 start + 1914980932
Stack dump:
0.  Program arguments: /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c /Users/hermespique/workspace/HanekeSwift/HanekeTests/XCTestCase+Assert.swift /Users/hermespique/workspace/HanekeSwift/HanekeTests/Cache.swift -primary-file /Users/hermespique/workspace/HanekeSwift/HanekeTests/Wrapper.swift -enable-objc-attr-requires-objc-module -target i386-apple-ios8.0 -module-name HanekeTests -sdk /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk -I /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator -F /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Frameworks -F /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/Developer/Library/Frameworks -g -module-cache-path /Users/hermespique/Library/Developer/Xcode/DerivedData/ModuleCache -Xcc -iquote -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-generated-files.hmap -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-own-target-headers.hmap -Xcc -ivfsoverlay -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/all-product-headers.yaml -Xcc -iquote -Xcc /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/HanekeTests-project-headers.hmap -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Products/Debug-iphonesimulator/include -Xcc -I/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/DerivedSources/i386 -Xcc -I/Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/DerivedSources -Xcc -DDEBUG=1 -Xcc -DDEBUG=1 -emit-module-doc-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper~partial.swiftdoc -O0 -emit-module-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper~partial.swiftmodule -serialize-diagnostics-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper.dia -emit-dependencies-path /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper.d -o /Users/hermespique/Library/Developer/Xcode/DerivedData/Haneke-dsgqpxenngbaqpctbkhhwtqbiorj/Build/Intermediates/Haneke.build/Debug-iphonesimulator/HanekeTests.build/Objects-normal/i386/Wrapper.o 
1.  While emitting IR SIL function @_TToFC11HanekeTests7WrapperE for 'deinit' at /Users/hermespique/workspace/HanekeSwift/HanekeTests/Wrapper.swift:11:7
<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)
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift Failed with exit code 254
我找到了解决这个问题的解决方法
class Wrapper<T> {
    let _val: T[]

    init(_ v: T) {
        _val = [v]
    }

    var val : T {
        get {
            return _val[0]
        }
        set {
            _val[0] = newValue
        }
    }
}

由于Array是一个引用类型(即使是struct),Wrapper类的大小现在已经修复,编译器可以处理它.

原文链接:/swift/319747.html

猜你在找的Swift相关文章