If I understand, the error is here:
func fetchLink() async {
do {
url = URL(string: GlobalString().link)
let (data, _) = try await URLSession.shared.data(from: url!)
If you look at URL documentation, you see that URL(string: _) returns an optional which may be nil (that's what happens in your case)
init?(string: String)
Creates a URL instance from the provided string.
Why is it nil, that's what you have now to investigate.
To make your code robust, test for nil:
func fetchLink() async {
do {
url = URL(string: GlobalString().link)
if url == nil {
print(GlobalString()) // <<-- To understand what happens
return // do not continue
}
let (data, _) = try await URLSession.shared.data(from: url!) // <<-- cannot be nil anymore
Do the same by testing post_url if the error is here:
let postUrl: URL? = linkResponse.post_url != nil ? URL(string: linkResponse.post_url!) : nil
Note: In Swift we avoid using underscore and use camelCase instead.
That means post_url should better be postUrl or postURL for instance.