简体   繁体   中英

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. There is an error at instruction 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 )

   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

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 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

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

You can convert url to string by using url.absoluteString .

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!);
                        }
                    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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