Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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 - Fatal编程技术网

使用函数查找Swift中两个序列中的公共元素

使用函数查找Swift中两个序列中的公共元素,swift,Swift,我正在努力完成苹果新书《Swift编程语言》第46页的练习。它给出了以下代码: func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> Bool { for lhsItem in lhs {

我正在努力完成苹果新书《Swift编程语言》第46页的练习。它给出了以下代码:

func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> Bool {
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                return true
            }
        }
    }
    return false
}
anyCommonElements([1, 2, 3], [3])
func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element:     Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> T.GeneratorType[] {
    var toReturn = T.GeneratorType[]()
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                toReturn.append(lhsItem)
            }
        }
    }
    return toReturn
}
anyCommonElements([1, 2, 3], [3])
func anyCommonElements(左:T,右:U)->Bool{
对于lhs中的lhsItem{
适用于rhs中的rhsItem{
如果lhsItem==rhsItem{
返回真值
}
}
}
返回错误
}
任意公共元素([1,2,3],[3])
练习是更改函数,以便返回两个序列中的所有元素。为此,我尝试使用以下代码:

func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> Bool {
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                return true
            }
        }
    }
    return false
}
anyCommonElements([1, 2, 3], [3])
func anyCommonElements <T, U where T: Sequence, U: Sequence, T.GeneratorType.Element:     Equatable, T.GeneratorType.Element == U.GeneratorType.Element> (lhs: T, rhs: U) -> T.GeneratorType[] {
    var toReturn = T.GeneratorType[]()
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                toReturn.append(lhsItem)
            }
        }
    }
    return toReturn
}
anyCommonElements([1, 2, 3], [3])
func anyCommonElements(lhs:T,rhs:U)->T.GeneratorType[]{
var toReturn=T.GeneratorType[]()
对于lhs中的lhsItem{
适用于rhs中的rhsItem{
如果lhsItem==rhsItem{
toReturn.append(lhsItem)
}
}
}
返回返回
}
任意公共元素([1,2,3],[3])
但是在第2行,我得到了一个错误:找不到成员“subscript”


出现此错误的原因是什么?解决此问题的最佳方法是什么?

我通过将返回值设置为T.GeneratorType.Element的数组使其正常工作

func anyCommonElements <T, U where T: SequenceType, U: SequenceType, T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element> (lhs: T, rhs: U) -> Array<T.Generator.Element> {
    var toReturn = Array<T.Generator.Element>()
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                toReturn.append(lhsItem)
            }
        }
    }
    return toReturn
}
anyCommonElements([1, 2, 3], [3])
func anyCommonElements(左:T,右:U)->数组{
var toReturn=Array()
对于lhs中的lhsItem{
适用于rhs中的rhsItem{
如果lhsItem==rhsItem{
toReturn.append(lhsItem)
}
}
}
返回返回
}
任意公共元素([1,2,3],[3])

我在上述两种解决方案中遇到了编译器错误,我正在Xcode 6.01的iBook中运行指南。我在这里发现的数组声明一直受到编译器的抱怨,所以我认为海报可能使用了早期版本的swift。如果我错了,知道就好了

对于数组声明,我发现

    var thingList : [ThingType] = []
我一直在努力,所以我倾向于这样做,放弃

    var thing[],thing[]()  // gave compiler errors/warnings
我的环境从未解决过一个名为T.GeneratorType[.Element]的问题

我为这个实验想出的解决方案是


func-anyCommonElements
(左:T,右:U)
->[T.发生器.元件]
{
var returnValue:[T.Generator.Element]=[]
对于lhs中的lhsItem{
适用于rhs中的rhsItem{
如果lhsItem==rhsItem{
returnValue.append(lhsItem)
}
}
}
返回值
}
设commonNumberList=anyCommonElements([1,2,3,4,5,6,7,8],[2,3,9,14,8,21])
println(“公共数字=\(公共数字列表)”)
让commonStringList=anyCommonElements([“a”、“b”、“c”]、[“d”、“e”、“f”、“c”、“b”]))
println(“公共字符串=\(commonStringList)”)


教程的内容真的没有让我在没有大量额外阅读的情况下真正解决实验问题。感谢在座的每个人提供了他们的解决方案,这真的帮助我对Swift有了一个很好的介绍。

问题是将返回值定义为一个数组,以便可以向其中添加元素

func anyCommonElements<T, U where T:SequenceType, U:SequenceType, 
T.Generator.Element: Equatable, T.Generator.Element == U.Generator.Element >(lhs: T, rhs: U) -> Array <T.Generator.Element> {
   var result = Array <T.Generator.Element>();
   for lhsItem in lhs {
       for rhsItem in rhs {
          if lhsItem == rhsItem {
            result.append(lhsItem);
          }
       }
  }
  return result;
}

print(anyCommonElements([1,3,7,9,6,8],rhs:[4,6,8]));
func anyCommonElements(左:T,右:U)->数组{
var result=Array();
对于lhs中的lhsItem{
适用于rhs中的rhsItem{
如果lhsItem==rhsItem{
结果追加(lhsItem);
}
}
}
返回结果;
}
打印(任何通用元素([1,3,7,9,6,8],rhs:[4,6,8]);

虽然这个问题已经得到了回答,而且最初的问题是关于使用泛型数组的,但是有一种方法可以使用
并改进stackoverflow知识库,我仍然想发布它

swift类
Set
包含以下四种方法:

Set.union(sequence:)
Set.subtract(sequence:)
Set.intersect(sequence:)
Set.exclusiveOr(sequence:)
此处记录了以下内容:

可以将两个数组转换为集合并使用以下方法:

let array1 = [1, 2, 3]
let array2 = [3, 4]

let set1 = Set<Int>(array1)
let set2 = Set<Int>(array2)

let union = set1.union(set2)                // [2, 3, 1, 4]
let subtract = set1.subtract(set2)          // [2, 1]
let intersect = set1.intersect(set2)        // [3]
let exclusiveOr = set1.exclusiveOr(set2)    // [2, 4, 1]
让数组1=[1,2,3]
设array2=[3,4]
设set1=Set(数组1)
设set2=Set(array2)
让union=set1.union(set2)/[2,3,1,4]
让subtract=set1。subtract(set2)/[2,1]
让intersect=set1.intersect(set2)/[3]
设排他EOR=set1.排他EOR(set2)/[2,4,1]

编辑1:

就像Martin R在评论中提到的那样,
Set
的类型必须继承协议
Hashable
,这比
equalable
的限制性稍强。
而且元素的顺序也不保留,因此考虑有序元素的相关性! 从Swift 3开始,生成器协议被重命名为迭代器协议:

因此,需要编写函数:

func commonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> [T.Iterator.Element]
    where T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element {
        var common: [T.Iterator.Element] = []

        for lhsItem in lhs {
            for rhsItem in rhs {
                if lhsItem == rhsItem {
                    common.append(lhsItem)
                }
            }
        }
        return common
}
func公共元素(lhs:T,rhs:U)->[T.Iterator.Element]
其中T.Iterator.Element:equalable,T.Iterator.Element==U.Iterator.Element{
var common:[T.Iterator.Element]=[]
对于lhs中的lhsItem{
适用于rhs中的rhsItem{
如果lhsItem==rhsItem{
common.append(lhsItem)
}
}
}
返回公共
}

接受的答案对于最新版本的Swift不再有效。这是一个与3.0.1版兼容的更新,它们简化了如何创建通用数组

func showCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> [T.Iterator.Element]
    where T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element {
        var result:[T.Iterator.Element] = []
        for lhsItem in lhs {
            for rhsItem in rhs {
                if lhsItem == rhsItem {
                    result.append(lhsItem)
                }
            }
        }
        return result
}

在Swift 3中解决此问题的最干净的方法是使用以下过滤器功能:

func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> [T.Iterator.Element]
    where T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element {
        return lhs.filter { rhs.contains($0) }
}
func anyCommonElements(lhs:T,rhs:U)->[T.Iterator.Element]
其中T.Iterator.Element:equalable,T.Iterator.Element==U.Iterator.Element{
返回lhs.filter{rhs.contains($0)}
}

在最新版本的Swift(5.1)中,以下代码起作用:

func anyCommonElements<T: Sequence, U: Sequence>(_ lhs: T, _ rhs: U) -> Array<T.Element>
    where T.Element: Equatable, T.Element == U.Element
{
    var templist = Array<T.Element>()
    for lhsItem in lhs {
        for rhsItem in rhs {
            if lhsItem == rhsItem {
                templist.append(lhsItem)
            }
        }
    }
   return templist
}
anyCommonElements([1, 2, 3], [3])
func anyCommonElements(lhs:T,rhs:U)->数组
其中T.Element:equalable,T.Element==U.Element
{
var templast=Array()
对于lhs中的lhsItem{
适用于rhs中的rhsItem{
如果lhsItem==rhsItem{
templast.append(lhsItem)
}
}
}
回归圣堂武士
}
任意公共元素([1,2,3],[3])

您的注释说您返回了T.GeneratorType.Element,但它只返回了T.I是p