Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Arrays 反向枚举以过滤出数组中的类似对象_Arrays_Swift - Fatal编程技术网

Arrays 反向枚举以过滤出数组中的类似对象

Arrays 反向枚举以过滤出数组中的类似对象,arrays,swift,Arrays,Swift,我有一个CLLocations数组。我的目标是找到数组中与以下位置位置相同的最后一个位置,然后删除以下所有对象 例如: [lat: 11.123, long: 11.123, lat: 12.345, long: 123.123, lat: 14.124, long: 14.124, lat: 16.1661, long: 16.1616, lat: 15.1515, long: 15.1515, lat: 15.1515, long: 15.1515, //Remove lat: 15.151

我有一个
CLLocations
数组。我的目标是找到数组中与以下位置位置相同的最后一个位置,然后删除以下所有对象

例如:

[lat: 11.123, long: 11.123,
lat: 12.345, long: 123.123,
lat: 14.124, long: 14.124,
lat: 16.1661, long: 16.1616,
lat: 15.1515, long: 15.1515,
lat: 15.1515, long: 15.1515, //Remove
lat: 15.1515, long: 15.1515] //Remove 
我想对存储它们的数组进行变异,而不是创建一个新的数组

我迄今为止的努力:

 var reversedLocations = Array(locations.reverse())
            for (index,location) in reversedLocations.enumerate() {
                if index+1 <= reversedLocations.count-1 {
                    let distanceToNext = LocationInterface.sharedInterface.distanceBetweenLocations(first: location, last: locations[index+1])
                    if distanceToNext < 10 {
                        reversedLocations.removeAtIndex(reversedLocations.indexOf(location)!)
                        toDate = reversedLocations.first!.timestamp
                    }
                }
            }
var reversedLocations=Array(locations.reverse())
对于reversedLocations.enumerate()中的(索引,位置){

如果index+1首先,如果您有一个
CLLocation
数组,那么它可能如下所示

let locations: [CLLocation] = [
    CLLocation(latitude: 11.123, longitude: 11.123),
    CLLocation(latitude: 12.345, longitude: 123.123),
    CLLocation(latitude: 14.124, longitude: 14.124),
    CLLocation(latitude: 16.1661, longitude: 16.1616),
    CLLocation(latitude: 15.1515, longitude: 15.1515),
    CLLocation(latitude: 15.1515, longitude: 15.1515),
    CLLocation(latitude: 15.1515, longitude: 15.1515)]
var added = Set<CLLocation>()
现在我们可以像这样创建一个
集合

let locations: [CLLocation] = [
    CLLocation(latitude: 11.123, longitude: 11.123),
    CLLocation(latitude: 12.345, longitude: 123.123),
    CLLocation(latitude: 14.124, longitude: 14.124),
    CLLocation(latitude: 16.1661, longitude: 16.1616),
    CLLocation(latitude: 15.1515, longitude: 15.1515),
    CLLocation(latitude: 15.1515, longitude: 15.1515),
    CLLocation(latitude: 15.1515, longitude: 15.1515)]
var added = Set<CLLocation>()

现在,您的结果在
过滤的

中,首先,如果您有一个
CLLocation
数组,那么它可能如下所示

let locations: [CLLocation] = [
    CLLocation(latitude: 11.123, longitude: 11.123),
    CLLocation(latitude: 12.345, longitude: 123.123),
    CLLocation(latitude: 14.124, longitude: 14.124),
    CLLocation(latitude: 16.1661, longitude: 16.1616),
    CLLocation(latitude: 15.1515, longitude: 15.1515),
    CLLocation(latitude: 15.1515, longitude: 15.1515),
    CLLocation(latitude: 15.1515, longitude: 15.1515)]
var added = Set<CLLocation>()
现在我们可以像这样创建一个
集合

let locations: [CLLocation] = [
    CLLocation(latitude: 11.123, longitude: 11.123),
    CLLocation(latitude: 12.345, longitude: 123.123),
    CLLocation(latitude: 14.124, longitude: 14.124),
    CLLocation(latitude: 16.1661, longitude: 16.1616),
    CLLocation(latitude: 15.1515, longitude: 15.1515),
    CLLocation(latitude: 15.1515, longitude: 15.1515),
    CLLocation(latitude: 15.1515, longitude: 15.1515)]
var added = Set<CLLocation>()

现在,您的结果已在
中过滤

要以良好的时间复杂性完成此操作,您需要一个有序集数据结构实现。下面我将展示如何使用依赖于的纯Swift解决方案来完成此操作

如果您不介意使用NSObject子类作为纬度和经度对类型,您可以使用
NSOrderedSet(数组:…您的位置对象…)来实现这一点.array
其中,
您的位置对象…
指的是您的NSObject子类,它包装了纬度和经度。您需要将
-hash
-isEqual:
实现到您的
位置
类。也就是说,下面示例代码中的原则非常类似:您需要一个包含您的纬度的类型de和经度对,可清洗(覆盖NSObject子类中的
-hash
)和相等(覆盖NSObject中的
-isEqual:

导入达尔文
导入分类集
结构位置:可散列{
让拉特:加倍
让我们长一点:双倍
var hashValue:Int{
//这是一个非常糟糕的散列函数。
//您可以从其他地方找到更高性能的说明。
返回整数(ceil(横向+纵向))
}
}
func==(左:位置,右:位置)->Bool{
返回lhs.lat==rhs.lat&&lhs.long==rhs.long
}
让orderedLocations=OrderedSet(序列:[位置(lat:11.123,long:11.123)),
位置(纬度:12.345,长:123.123),
位置(纬度:14.124,长:14.124),
位置(纬度:16.1661,长:16.1616),
位置(纬度:15.1515,长:15.1515),
位置(纬度:15.1515,长:15.1515),
位置(纬度:15.1515,长:15.1515)])
对于orderedLocations中的位置{
打印(位置)//按原始顺序打印位置,删除重复项。
}

要以良好的时间复杂度完成此任务,您需要一个有序的集合数据结构实现。下面我将展示如何使用依赖于的纯Swift解决方案来完成此任务

如果您不介意使用NSObject子类作为纬度和经度对类型,您可以使用
NSOrderedSet(数组:…您的位置对象…)来实现这一点.array
其中,
您的位置对象…
指的是您的NSObject子类,它包装了纬度和经度。您需要将
-hash
-isEqual:
实现到您的
位置
类。也就是说,下面示例代码中的原则非常类似:您需要一个包含您的纬度的类型de和经度对,可清洗(覆盖NSObject子类中的
-hash
)和相等(覆盖NSObject中的
-isEqual:

导入达尔文
导入分类集
结构位置:可散列{
让拉特:加倍
让我们长一点:双倍
var hashValue:Int{
//这是一个非常糟糕的散列函数。
//您可以从其他地方找到更高性能的说明。
返回整数(ceil(横向+纵向))
}
}
func==(左:位置,右:位置)->Bool{
返回lhs.lat==rhs.lat&&lhs.long==rhs.long
}
让orderedLocations=OrderedSet(序列:[位置(lat:11.123,long:11.123)),
位置(纬度:12.345,长:123.123),
位置(纬度:14.124,长:14.124),
位置(纬度:16.1661,长:16.1616),
位置(纬度:15.1515,长:15.1515),
位置(纬度:15.1515,长:15.1515),
位置(纬度:15.1515,长:15.1515)])
对于orderedLocations中的位置{
打印(位置)//按原始顺序打印位置,删除重复项。
}
类似的东西会有用的。你可能需要调整它以满足你的特殊需要

*编辑-这些其他解决方案看起来更复杂,也许我误解了您的需要

var locations = [1, 2, 3, 4, 5, 5, 5, 1, 2, 3, 4, 3, 3, 3]

while !locations.isEmpty && locations.dropLast().last == locations.last
{
    locations.removeLast()
}

print(locations)
类似的东西会有用的。你可能需要调整它以满足你的特殊需要

*编辑-这些其他解决方案看起来更复杂,也许我误解了您的需要

var locations = [1, 2, 3, 4, 5, 5, 5, 1, 2, 3, 4, 3, 3, 3]

while !locations.isEmpty && locations.dropLast().last == locations.last
{
    locations.removeLast()
}

print(locations)
使用
Map
Reduce
Filter
是不必要的,事实上,在这种情况下建议不要使用它们。因为一旦发现与上一个元素不同的元素,您应该停止读取所有元素

就像@Eendje说的:

其他答案也将删除除答案以外的任何其他重复项 最后的

OP使用的解决方案:

                while !locations.isEmpty && (LocationInterface.sharedInterface.distanceBetweenLocations(first: locations.dropLast().last!, last: locations.last!) > 15)
实际上,使用
Map
Reduce
Filter
是不必要的