Error[8]: Undefined offset: 22, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

如何在Swift中找到列表项的索引

由于Swift在某些方面要比面向对象的功能更强大(并且Array是结构而不是对象),因此请使用“
find”函数对数组进行 *** 作,该数组将返回可选值,因此请准备处理nil值:

let arr:Array = ["a","b","c"]find(arr, "c")!   // 2find(arr, "d")    // nil

Swift 2.0更新:

find
Swift 2.0不再支持旧功能!

使用Swift 2.0,

Array
可以使用扩展中定义的函数
CollectionType
Array
实现)来找到元素的索引:

let arr = ["a","b","c"]let indexOfA = arr.indexOf("a") // 0let indexOfB = arr.indexOf("b") // 1let indexOfD = arr.indexOf("d") // nil

此外,另一个扩展支持在数组中找到满足谓词的第一个元素

CollectionType

let arr2 = [1,2,3,4,5,6,7,8,9,10]let indexOfFirstGreaterThanFive = arr2.indexOf({
find
> 5}) // 5let indexOfFirstGreaterThanoneHundred = arr2.indexOf({Swift 3.0更新: > 100}) // nil

请注意,这两个函数会像

Equatable
以前一样返回可选值。

let indexOfA = arr.index(of: "a")

注意indexOf的语法已更改。对于符合条件的物品,

Equatable
可以使用:

index(where:)

有关该方法的详细文档,请参见https://developer.apple.com/reference/swift/array/1689674-index

对于不符合条件的数组项,

let index = cells.index(where: { (item) -> Bool in  item.foo == 42 // test if this is the item you're looking for})
您需要使用Swift 4.2的更新:

index

firstIndex

在Swift
4.2中,

lastIndex
不再使用它,而是将其分开
let arr = ["a","b","c","a"]let indexOfA = arr.firstIndex(of: "a") // 0let indexOfB = arr.lastIndex(of: "a") // 3
并[+++]进行更好的说明。因此,根据您要查找的项目的第一个还是最后一个索引:

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 23, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

如何在Swift中找到列表项的索引

由于Swift在某些方面要比面向对象的功能更强大(并且Array是结构而不是对象),因此请使用“
find”函数对数组进行 *** 作,该数组将返回可选值,因此请准备处理nil值:

let arr:Array = ["a","b","c"]find(arr, "c")!   // 2find(arr, "d")    // nil

Swift 2.0更新:

find
Swift 2.0不再支持旧功能!

使用Swift 2.0,

Array
可以使用扩展中定义的函数
CollectionType
Array
实现)来找到元素的索引:

let arr = ["a","b","c"]let indexOfA = arr.indexOf("a") // 0let indexOfB = arr.indexOf("b") // 1let indexOfD = arr.indexOf("d") // nil

此外,另一个扩展支持在数组中找到满足谓词的第一个元素

CollectionType

let arr2 = [1,2,3,4,5,6,7,8,9,10]let indexOfFirstGreaterThanFive = arr2.indexOf({
find
> 5}) // 5let indexOfFirstGreaterThanoneHundred = arr2.indexOf({Swift 3.0更新: > 100}) // nil

请注意,这两个函数会像

Equatable
以前一样返回可选值。

let indexOfA = arr.index(of: "a")

注意indexOf的语法已更改。对于符合条件的物品,

Equatable
可以使用:

index(where:)

有关该方法的详细文档,请参见https://developer.apple.com/reference/swift/array/1689674-index

对于不符合条件的数组项,

let index = cells.index(where: { (item) -> Bool in  item.foo == 42 // test if this is the item you're looking for})
您需要使用Swift 4.2的更新:

index

firstIndex

在Swift
4.2中,

lastIndex
不再使用它,而是将其分开
let arr = ["a","b","c","a"]let indexOfA = arr.firstIndex(of: "a") // 0let indexOfB = arr.lastIndex(of: "a") // 3
并进行更好的说明。因此,根据您要查找的项目的第一个还是最后一个索引:

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
如何在Swift中找到列表项的索引?_随笔_内存溢出

如何在Swift中找到列表项的索引?

如何在Swift中找到列表项的索引?,第1张

如何在Swift中找到列表项的索引

由于Swift在某些方面要比面向对象的功能更强大(并且Array是结构而不是对象),因此请使用“
find”函数对数组进行 *** 作,该数组将返回可选值,因此请准备处理nil值:

let arr:Array = ["a","b","c"]find(arr, "c")!   // 2find(arr, "d")    // nil

Swift 2.0更新:

find
Swift 2.0不再支持旧功能!

使用Swift 2.0,

Array
可以使用扩展中定义的函数
CollectionType
Array
实现)来找到元素的索引:

let arr = ["a","b","c"]let indexOfA = arr.indexOf("a") // 0let indexOfB = arr.indexOf("b") // 1let indexOfD = arr.indexOf("d") // nil

此外,另一个扩展支持在数组中找到满足谓词的第一个元素

CollectionType

let arr2 = [1,2,3,4,5,6,7,8,9,10]let indexOfFirstGreaterThanFive = arr2.indexOf({
find
> 5}) // 5let indexOfFirstGreaterThanoneHundred = arr2.indexOf({Swift 3.0更新: > 100}) // nil

请注意,这两个函数会像

Equatable
以前一样返回可选值。

let indexOfA = arr.index(of: "a")

注意indexOf的语法已更改。对于符合条件的物品,

Equatable
可以使用:

index(where:)

有关该方法的详细文档,请参见https://developer.apple.com/reference/swift/array/1689674-index

对于不符合条件的数组项,

let index = cells.index(where: { (item) -> Bool in  item.foo == 42 // test if this is the item you're looking for})
您需要使用Swift 4.2的更新:

index

firstIndex

在Swift
4.2中,

lastIndex
不再使用它,而是将其分开
let arr = ["a","b","c","a"]let indexOfA = arr.firstIndex(of: "a") // 0let indexOfB = arr.lastIndex(of: "a") // 3
并进行更好的说明。因此,根据您要查找的项目的第一个还是最后一个索引:



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5666509.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存