简体   繁体   English

如何在 ios/swift 中将 url 转换为字符串?

[英]how to convert url to string in ios/swift?

In my code ios/SWIFT I an trying to read the url from a previous image that I successful saved at Storage Firebase.在我的代码 ios/SWIFT 中,我试图从我成功保存在 Storage Firebase 的先前图像中读取 url。 There is an error at instruction let url1 = url as?指令 let url1 = url as 有错误? String I need to convert url to string but the cast doesn't work The print instruction is ok I see the correct url at log screen print(url..absoluteURL as Any )字符串 我需要将 url 转换为字符串,但转换不起作用

   let armazenamento = Storage.storage().reference()
    let imagens = armazenamento.child("imagens")
    if let imagemSelecionada=imagem.image {
        if let imagemDados = imagemSelecionada.jpegData(compressionQuality: 0.1) {
            imagens.child("\(self.idImagem).jpg").putData(imagemDados, metadata: nil, completion: {(metaDados, erro) in
                if erro == nil {
                    print("Sucess upload")
                    imagens.child("\(self.idImagem).jpg").downloadURL(completion: { (url, erro) in
                        if(erro == nil)
                        {
                            print(url?.absoluteURL as Any )
                            let url1 = url as! String
                            self.performSegue(withIdentifier: "selecionarUsuarioSegue", sender: url1)
                        }else{
                            print(err!);
                        }
                    })

to convert url to string use this将 url 转换为字符串使用这个

var myurl: NSURL
var urlString: String = myurl.absoluteString

hope it helps希望能帮助到你

yes its easy to do.You can try:是的,这很容易做到。您可以尝试:

var yourURL:NSUL!
var a = yourURL.absoluteString

Doesnt' work, but what is the error?不起作用,但错误是什么?

Try not to force-downcast your optionals when not absolutely necessary.尽量不要在非绝对必要时强行向下转换你的可选值。 Use if let optional unwrapping or guard Anyway, try this -使用if let optional unwrapping or guard无论如何,试试这个 -

if let strurl = url {
    let urlString = strurl.absoluteString 
    print(type(of: urlString))  // should print "String"
}
else {
    // Your url is nil 
} 

Go through this link Here you can find url to string and string to url通过此链接 Here 你可以找到 url to string 和 string to url

https://www.zerotoappstore.com/swift-url-to-string.html https://www.zerotoappstore.com/swift-url-to-string.html

You can convert url to string by using url.absoluteString .您可以使用url.absoluteString将 url 转换为字符串。

imagens.child("\(self.idImagem).jpg").downloadURL(completion: { (url, erro) in
                        if(erro == nil)
                        {
                            print(url?.absoluteString)
                            let urlString = url.absoluteString
                            self.performSegue(withIdentifier: "selecionarUsuarioSegue", sender: urlString)
                        }else{
                            print(err!);
                        }
                    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM