符合Swift 3.1中的Strideable Protocol

时间:2017-05-27 13:52:36

标签: swift swift3 swift-protocols

我正在经历转换为Swift 3.1的绝对快乐。我遇到了Strideable协议的问题。以下代码无法编译;唯一的错误信息是类型' Segment'不符合协议Strideable(也不符合_Strideable)。在我周围搜索我发现了很多关于相关类型和嵌套类型的东西,但没什么特别的。

struct segment: Equatable, Comparable, Hashable, Strideable {
var segTurn: Int
var segSegment: Int
typealias Stride = Int

init() {
    segTurn = 0
    segSegment = 12
}

init(segment: Int, turn: Int) {
    segTurn = turn
    segSegment = segment
}

var hashValue: Int {
    get {
        return "\(self.segTurn),\(self.segSegment)".hashValue
    }
}

var description: String {
    if segTurn == 0 && segSegment == 12 {
        return "Start of Combat Segment 12"
    }
    return "Turn: \(self.segTurn) Segment: \(self.segSegment)"
}

static func successor() -> segment {
    return self + 1
}

static func advanced(by n: Int) -> segment {
    return self + n
}

static func distance(to other: segment) -> Int {
    return other - self
}

static postfix func ++(theSeg:inout segment) -> segment {
    theSeg = theSeg + 1
    return theSeg
}

static func +(lhs:segment, rhs:Int) -> segment {
    var intSeg = lhs.segSegment + rhs
    var intTurn = lhs.segTurn
    while intSeg > 12 {
        intTurn += 1
        intSeg = intSeg - 12
    }
    while intSeg < 1 {
        intTurn -= 1
        intSeg = intSeg + 12
    }

    return segment(segment: intSeg, turn: intTurn)
}

static func -(lhs:segment, rhs:Int) -> segment {
    var intSeg = lhs.segSegment - rhs
    var intTurn = lhs.segTurn
    while intSeg > 12 {
        intTurn += 1
        intSeg = intSeg - 12
    }
    while intSeg < 1 {
        intTurn -= 1
        intSeg = intSeg + 12
    }

    return segment(segment: intSeg, turn: intTurn)
}

static func ==(lhs:segment, rhs:segment) -> Bool {
    let areEqual = lhs.segTurn == rhs.segTurn &&
        lhs.segSegment == rhs.segSegment

    return areEqual
}

static func <(lhs:segment, rhs:segment) -> Bool {
    var isLessThan = false
    if lhs.segTurn < rhs.segTurn {
        isLessThan = true
    } else if lhs.segTurn == rhs.segTurn && lhs.segSegment < rhs.segSegment {
        isLessThan = true
    }
    return isLessThan

}

}

1 个答案:

答案 0 :(得分:1)

以下是您需要修改以使其工作的内容:

  1. 从细分中的所有功能中删除静态关键字。 (留下它们作为实例方法)
  2. 将所有操作员功能移至Segment结构定义之外。
  3. 然后你的代码将如下所示,它将在Swift 3.1中编译而没有错误/警告:

    struct Segment: Equatable, Comparable, Hashable, Strideable {
        var segTurn: Int
        var segSegment: Int
        typealias Stride = Int
    
        init() {
            segTurn = 0
            segSegment = 12
        }
    
        init(segment: Int, turn: Int) {
            segTurn = turn
            segSegment = segment
        }
    
        var hashValue: Int {
            get {
                return "\(self.segTurn),\(self.segSegment)".hashValue
            }
        }
    
        var description: String {
            if segTurn == 0 && segSegment == 12 {
                return "Start of Combat Segment 12"
            }
            return "Turn: \(self.segTurn) Segment: \(self.segSegment)"
        }
    
        func successor() -> Segment {
            return self + 1
        }
    
        func advanced(by n: Int) -> Segment {
            return self + n
        }
    
        func distance(to other: Segment) -> Int {
            return other - self
        }
    }
    
    postfix func ++(theSeg:inout Segment) -> Segment {
        theSeg = theSeg + 1
        return theSeg
    }
    
    func +(lhs:Segment, rhs:Int) -> Segment {
        var intSeg = lhs.segSegment + rhs
        var intTurn = lhs.segTurn
        while intSeg > 12 {
            intTurn += 1
            intSeg = intSeg - 12
        }
        while intSeg < 1 {
            intTurn -= 1
            intSeg = intSeg + 12
        }
    
        return Segment(segment: intSeg, turn: intTurn)
    }
    
    func -(lhs:Segment, rhs:Int) -> Segment {
        var intSeg = lhs.segSegment - rhs
        var intTurn = lhs.segTurn
        while intSeg > 12 {
            intTurn += 1
            intSeg = intSeg - 12
        }
        while intSeg < 1 {
            intTurn -= 1
            intSeg = intSeg + 12
        }
    
        return Segment(segment: intSeg, turn: intTurn)
    }
    
    func ==(lhs:Segment, rhs:Segment) -> Bool {
        let areEqual = lhs.segTurn == rhs.segTurn &&
            lhs.segSegment == rhs.segSegment
    
        return areEqual
    }
    
    func <(lhs:Segment, rhs:Segment) -> Bool {
        var isLessThan = false
        if lhs.segTurn < rhs.segTurn {
            isLessThan = true
        } else if lhs.segTurn == rhs.segTurn && lhs.segSegment < rhs.segSegment {
            isLessThan = true
        }
        return isLessThan
    }