Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 为什么这会引起这么多麻烦?(协议及其关联类型上的类型别名)_Swift_Swift3 - Fatal编程技术网

Swift 为什么这会引起这么多麻烦?(协议及其关联类型上的类型别名)

Swift 为什么这会引起这么多麻烦?(协议及其关联类型上的类型别名),swift,swift3,Swift,Swift3,我试着做一些类似的事情: protocol Protocol { associatedtype T associatedtype ArrayT = Array<T> } struct Struct<ProtocolType: Protocol> { func doSomething(with: ProtocolType.ArrayT) { let _ = with.map { $0 } // ^ compil

我试着做一些类似的事情:

protocol Protocol {
    associatedtype T
    associatedtype ArrayT = Array<T>
}

struct Struct<ProtocolType: Protocol> {

    func doSomething(with: ProtocolType.ArrayT) {

        let _ = with.map { $0 } 
        // ^ compiler complains on this line
        // "value of type ProtocolType.ArrayT has no member map"
    }
}   
协议{
关联T型
associatedtype ArrayT=数组
}
结构{
func doSomething(带:ProtocolType.ArrayT){
设=with.map{$0}
//^编译器在这一行抱怨
//“ProtocolType.ArrayT类型的值没有成员映射”
}
}   
其中,我定义了一个方便的typealias
ArrayT
,它使用
associatedtype
T
。似乎当我尝试在
doSomething(:)
中使用
ArrayT
时,我丢失了
ArrayT
类型信息

ArrayT
不应该肯定是一个
Array
并因此成为
Sequence
协议的成员,公开
map
功能吗 当您“初始化”一个
关联类型时,您并没有定义它。首先,这不是关联类型的要点。关联的类型被故意解除绑定(“占位符”),直到采用类解析它们为止。您所做的只是给它一个默认值,允许一致性类重写该值。要扩展您的示例,请执行以下操作:

protocol Protocol {
    associatedtype T
    associatedtype ArrayT = Array<Self.T>

    func useSomeT(myFavoriteT: T)

    func useSomeArrayT(myLeastFavoriteArrayT: ArrayT)
}

class Whatever: Protocol {
    func useSomeT(myFavoriteT: Int) {
        print("My Favorite Int: \(myFavoriteT)")
    }

    func useSomeArrayT(myLeastFavoriteArrayT: [Int: String]) {
        print(myLeastFavoriteArrayT.map { $0.1 })
    }
}

struct Struct<ProtocolType: Protocol> {
    func doSomething(stuff: ProtocolType.ArrayT) {
        print(type(of: stuff))
    }
}

let x = Struct<Whatever>()
x.doSomething(stuff: [3: "Doggies"])
协议{
关联T型
associatedtype ArrayT=数组
函数useSomeT(myFavoriteT:T)
func useSomeArrayT(MyLeastFavoriteArayt:ArrayT)
}
类别:协议{
func useSomeT(myFavoriteT:Int){
打印(“我最喜欢的Int:\(myFavoriteT)”)
}
func useSomeArrayT(myLeastFavoriteArayt:[Int:String]){
打印(myLeastFavoriteArrayT.map{$0.1})
}
}
结构{
func doSomething(内容:ProtocolType.ArrayT){
印刷品(种类:材料)
}
}
设x=Struct()
x、 doSomething(材料:[3:“小狗”])

对于您的示例,似乎您真正想要的只是声明
使用:Array
(或者简单地
使用:[ProtocolType.T]
)作为参数类型。

associatedtype ArrayT=Array
只告诉编译器
ArrayT
默认值是
Array
。协议的自适应仍然可以更改
ArrayT
如下:

struct U: Protocol {
    typealias T = UInt32
    typealias ArrayT = UInt64   // <-- valid!
}
结构U:协议{ 类型别名T=UInt32
typealias ArrayT=UInt64//ah,这很有道理!我最初尝试将其声明为
typealias
,然后让Xcode提示告诉我将其更改为
associatedtype
。我不知道您可以为关联类型定义默认值!谢谢您,漂亮的人!接受答案@kennytm pr虽然两种方法都可以满足要求,但都是在先到先得的基础上进行的
// does not work yet.
protocol Protocol {
    associatedtype T
    typealias ArrayT = Array<T>
}