gpt4 book ai didi

swift - swift 2 中 sort 和 sortInPlace 的区别?

转载 作者:搜寻专家 更新时间:2023-10-30 22:04:41 24 4
gpt4 key购买 nike

我一直在尝试在 swift 中使用 sortInPlace 函数,但它不起作用。当我使用 sort 函数而不是 sortinplace 时,它​​起作用了。

请解释这两个函数之间的区别。如果您能提供演示这两个函数的用法的小代码示例,那将非常有帮助。

最佳答案

var mutableArray = [19, 7, 8, 45, 34]

// function sort sorts the array but does not change it. Also it has return

mutableArray.sort()

mutableArray
// prints 19, 7, 8, 45, 34

// function sortInPlace will mutate the array. Do not have return

mutableArray.sortInPlace()

mutableArray
// prints 7, 8, 19, 34, 45

关于swift - swift 2 中 sort 和 sortInPlace 的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33580177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com