Ios url中的随机数生成器

Ios url中的随机数生成器,ios,swift,random,Ios,Swift,Random,目前,我的url中有一个随机数生成器,用于为url的页面参数分配新的数字。当我获取新图像并打印url时,它使用相同的url。我不知道如何删除url中现有的随机数 这是我的声明: public static var random = Int.random(in: 0..<450) static let ImageURL = "https://api.unsplash.com/search/photos/?client_id=\(UnsplashClient.apiKey)&

目前,我的url中有一个随机数生成器,用于为url的页面参数分配新的数字。当我获取新图像并打印url时,它使用相同的url。我不知道如何删除url中现有的随机数

这是我的声明:

public static var random = Int.random(in: 0..<450)
static let ImageURL = "https://api.unsplash.com/search/photos/?client_id=\(UnsplashClient.apiKey)&page=\(random))"


public static var random=Int.random(in:0..根据我的说法,您必须创建一个数字数组,其中包含0到450之间的整数,如下所示
var array=array(0…450)
,然后可以使用
array.randomElement()从该数组中选择随机数!
。选择后,从该数组中删除该数字。

因为您使用的是
静态属性,它的值不会在每次调用时更改,除非您手动更新。您可以通过使用
静态计算属性来解决此问题,该属性的值在每次使用时都会计算

public static var random: Int { .random(in: 0..<450) }
public static var ImageURL: String { "https://api.unsplash.com/search/photos/?client_id=\(UnsplashClient.apiKey)&page=\(random))" }

不要对数组使用静态变量。静态变量只生成一次。因此它不会为您生成随机数。
阅读以下内容:

这不会有什么区别。ImageURL是常量。random只会在一次调用一次!我怎么会忘记呢?最好是随机化一个索引并在删除它时获取元素。
如果让index=array.index.randomElement(){
让value=array.remove(at:index)
}
考虑到重复随机值的概率很低,我认为没有必要从数组中删除该值。这样OP就不需要检查数组是否为空来再次填充。是的,概率非常小,但根据问题要求,我添加了这个。
var imageURL:String{"https://api.unsplash.com/search/photos/?client_id=\(UnsplashClient.apiKey)&page=\(Int.random(in:0..@LeoDabus)感谢您的回复。这确实有帮助,我对Static的使用感到困惑。欢迎使用“静态变量只生成一次”??这与是否声明为静态无关
public static var imageURL: String { "https://api.unsplash.com/search/photos/?client_id=\(UnsplashClient.apiKey)&page=\(Int.random(in: 0..<450)))" }