Swift 3 - 集合中的结构

时间:2016-11-09 08:00:48

标签: swift struct collections hashable

为什么下面的代码不起作用?我需要做些什么才能让它发挥作用?

//: Playground - noun: a place where people can play

import Cocoa

struct Person: CustomDebugStringConvertible, Hashable {
    let name: String
    let age: Int

    // MARK: CustomDebugStringConvertible

    var debugDescription: String {
        return "\(name) is \(age) years old"
    }

    // MARK: Hashable

    var hashValue: Int {
        return name.hashValue ^ age.hashValue
    }
}

func ==(lhs: Person, rhs: Person) -> Bool {
    return lhs.name == rhs.name && lhs.age == rhs.age
}

let ilse = Person(name: "Ilse", age: 33)
let mark = Person(name: "Mark", age: 38)

extension Collection where Iterator.Element: Person {
    var averageAge: Int {
        let sum = self.reduce(0) { $0 + $1.age }
        let count = self.count as! Int
        return sum / count
    }
}

var people = [Person]()
people.append(ilse)
people.append(mark)

let averageAge = people.averageAge

我发现如果我将结构体设为Swift类,它就可以了。是否与结构为值类型有关?我确实在最后一行看到编译器错误。 “'[Person]'不能转换为'<>'”

谢谢。

2 个答案:

答案 0 :(得分:3)

extension Collection where Iterator.Element: Person

restricts Iterator.Element to types which adopt the protocol Person or are a subclass of Person. Both is not possible with struct Person, and in the full compiler log you'll find

error: type 'Iterator.Element' constrained to non-protocol type 'Person'

What you probably mean is

extension Collection where Iterator.Element == Person 

which restricts the extension to collections of Person. Alternatively, define a protocol

protocol HasAge {
    var age: Int { get }
}

adopt that by Person

struct Person: CustomDebugStringConvertible, Hashable, HasAge { ... }

and define the extension for collections of elements which have a age:

extension Collection where Iterator.Element: HasAge { ... }

答案 1 :(得分:1)

Change your Collection's extension to this

extension Collection where Iterator.Element == Person {
    var averageAge: Int {
        let sum = self.reduce(0) { $0 + $1.age }
        let count = self.count as! Int
        return sum / count
    }
}