Swift - 编译器错误 - 分段错误11 - 泛型符合协议

Fra*_*lin 4 generics ios swift

Swift 1.2/XCode 6.4

我有以下代码:

public protocol MapShape : AnyObject
{
    func isEqualTo(other : MapShape) -> Bool
}
Run Code Online (Sandbox Code Playgroud)

还有一个我尝试遵循该协议的通用类

public class MapMulti<T:MapShape>
{
    let items : [T]
    init(items : [T])
    {
        self.items = items
    }
}
extension MapMulti : Equatable {}
    public func ==<T:MapShape>(lhs: MapMulti<T>, rhs: MapMulti<T>) -> Bool
    {
         return true //simplify code
    }


extension MapMulti : MapShape {
    public func isEqualTo(other: MapShape) -> Bool {
        if (object_getClassName(other) == object_getClassName(self))
        {
            return self == other as! MapMulti
        }
        return false
     }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试构建失败时,使用:

0  swift                    0x000000010a3da2b8       llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x000000010a3da794 SignalHandler(int) + 452
2  libsystem_platform.dylib 0x00007fff95704f1a _sigtramp + 26
3  libsystem_platform.dylib 0x00007fff55f3c832 _sigtramp + 3229841714
4  swift                    0x0000000109d0112b swift::irgen::emitCategoryData(swift::irgen::IRGenModule&, swift::ExtensionDecl*) + 1819
5  swift                    0x0000000109d09432 swift::irgen::IRGenModule::emitExtension(swift::ExtensionDecl*) + 514
6  swift                    0x0000000109d061a4 swift::irgen::IRGenModule::emitSourceFile(swift::SourceFile&, unsigned int) + 100
7  swift                    0x0000000109d87c77 performIRGeneration(swift::IRGenOptions&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 2151
8  swift                    0x0000000109d88693 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 51
9  swift                    0x0000000109cc4087 frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 6647
10 swift                    0x0000000109cc24e6 main + 1814
11 libdyld.dylib            0x00007fff998e75c9 start + 1
12 libdyld.dylib            0x0000000000000069 start + 1718717089
Stack dump:

1.  While emitting IR for source file /<PROJECT>/MyProject/MapMulti.swift
Run Code Online (Sandbox Code Playgroud)

导致失败的部分是尝试符合MapShape协议的扩展,如果我评论它编译.

我认为我的泛型理解中的某些内容是错误的.当我尝试这个时:

let multipoint : MapMulti<MapShape> = MapMulti<MapPoint>(items: [P1,P2,P3])
Run Code Online (Sandbox Code Playgroud)

它说MapMulti(MapPoint)不能转换为MapMulti(MapShape)
即使MapPoint符合MapShape,我也能做到:

let shape : MapShape = MapPoint()
Run Code Online (Sandbox Code Playgroud)

相反,我必须做:我不喜欢.

let multiShape : MapMulti<MapShape> = MapMulti<MapShape>(items: [P1,P2,P3])
Run Code Online (Sandbox Code Playgroud)

请帮助!

编辑:添加了MapPoint实施

public class MapPoint : MapShape{
    let lat : Double
    let long : Double

    init (lat : Double, long : Double)
    {
        self.lat = lat
        self.long = long
    }



}
extension MapPoint : Equatable{}
public func ==(lhs: MapPoint, rhs: MapPoint) -> Bool
{
    return lhs.long == rhs.long && lhs.lat == rhs.lat
}

extension MapPoint : MapShape
{
    public func isEqualTo(other: MapShape) -> Bool {
        if (object_getClassName(other) == object_getClassName(self))
        {
            return self == other as! MapPoint
        }
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)

dGa*_*bit 7

我对你的代码做了一些小改动,现在编译

public protocol MapShape // Removed the : AnyObject
{
    func isEqualTo(other : MapShape) -> Bool
}

public class MapMulti<T:MapShape>
{
    let items : [T]
    init(items : [T])
    {
        self.items = items
    }
}

extension MapMulti : Equatable {}

public func ==<T:MapShape>(lhs: MapMulti<T>, rhs: MapMulti<T>) -> Bool
{
    return true //simplify code
}
Run Code Online (Sandbox Code Playgroud)

现在,如果您只想知道两个元素是否是同一个类的实例,则下一个更改有效:

extension MapMulti : MapShape {
    public func isEqualTo(other: MapShape) -> Bool {
        // You can compare against dynamic class here 
        return self.dynamicType == other.dynamicType
    }
}
Run Code Online (Sandbox Code Playgroud)

有一个在这个问题回答了很好的解释,例如dynamicType的:确定与斯威夫特泛型子类的自定义类的工作,但不与UITapGestureRecognizer

我希望它有所帮助