具有通用项目的Swift数组扩展 [英] Swift Array extension with generic items

查看:145
本文介绍了具有通用项目的Swift数组扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  struct Section< InfoT:Equatable,ItemsT:Equatable> {
让info:InfoT?
let items:[ItemsT]
}

这个泛型类型元素的方法:

 扩展数组元素==部分< Equatable,Equatable> {
//只是虚拟示例函数:
func debugDummy(){
for self {
let info = section.info $ b $ print(section info: \(info)。)
for section in section.items {
print(item:\(item)。)
}
}
}
}

这给我编译错误:

 错误:不支持'Equatable'作为符合协议'Equatable'的具体类型
扩展数组其中Element == Section ^

错误:GenericsListPlayground.playground:6:24:错误:'Element'类型的值没有成员'info'
let info = section.info
^ ~~~~~~ ~~~~

错误:GenericsListPlayground.playground:8:25:error:类型'Element'的值没有成员'items'
for item in section.items {
^ ~~~~~~ ~~~~~

如何正确声明这样的扩展名?我已经尝试了几个声明这个扩展的变体,比如:

 扩展数组元素==部分(无参数)$ b $ 



 错误:对泛型类型'Section'的引用需要< ...>中的参数。 

etc ...他们都不想编译。



  import Foundation 
$ b解决方案

$ b协议Section {
associatedtype InfoT:可衡量
相关类型ItemsT:可衡量
var info:InfoT? {get}
var items:[ItemsT] {get}
}

扩展数组元素:部分{
//虚拟示例函数:
func debugDummy(){
for section in self {
let info = section.info
print(section info:\(String(describe:info))。)
$为b中的项目{b $ b print(item:\(item)。)
}
}
}
}


I am using Swift 3.1 and have some generic struct:

struct Section<InfoT: Equatable, ItemsT: Equatable> {
    let info: InfoT?
    let items: [ItemsT]
}

And want array extension with custom method for elements of this generic type:

extension Array where Element == Section<Equatable, Equatable> {
    // Just dummy example function:
    func debugDummy() {
        for section in self {
            let info = section.info
            print("section info: \(info).")
            for item in section.items {
                print("item: \(item).")
            }
        }
    }
}

This gives me compilation errors:

error: using 'Equatable' as a concrete type conforming to protocol 'Equatable' is not supported
extension Array where Element == Section<Equatable, Equatable> {
                                 ^

error: GenericsListPlayground.playground:6:24: error: value of type 'Element' has no member 'info'
            let info = section.info
                       ^~~~~~~ ~~~~

error: GenericsListPlayground.playground:8:25: error: value of type 'Element' has no member 'items'
            for item in section.items {
                        ^~~~~~~ ~~~~~

How to properly declare such extension? I have tried few variations for declaring this extension, like:

extension Array where Element == Section (no arguments)

produces:

error: reference to generic type 'Section' requires arguments in <...>

etc... none of them want to compile.

解决方案

Try this:

import Foundation

protocol Section {
    associatedtype InfoT: Equatable
    associatedtype ItemsT: Equatable
    var info: InfoT? { get }
    var items: [ItemsT] { get }
}

extension Array where Element: Section {
    // Just dummy example function:
    func debugDummy() {
        for section in self {
            let info = section.info
            print("section info: \(String(describing: info)).")
            for item in section.items {
                print("item: \(item).")
            }
        }
    }
}

这篇关于具有通用项目的Swift数组扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆