2

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!);
                        }
                    })
3
  • 1
    "but the cast doesn't work" 1) Always copy/paste error and exception output! 2) For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. 3) What language is this, Scala? Please add the language tag. Commented Dec 1, 2019 at 23:49
  • 1
    What does this have to do with Swing. Swing is a graphical UI for Java. You question has nothing to do with a graphical component. The code posted does not look like Java. Is it JavaScript? (I know JavaScript has a "let" keyword) if so, then using proper tags will give you a better chance of getting an answer. Commented Dec 1, 2019 at 23:50
  • The conversion depends strongly on the kind of the URL (remote or local) Commented Dec 3, 2019 at 12:10

5 Answers 5

3

to convert url to string use this

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

hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

I have to modify to urlString = (strurl as AnyObject).absoluteString!
3

yes its easy to do.You can try:

var yourURL:NSUL!
var a = yourURL.absoluteString

1 Comment

have you tried ? let url = URL(string: "zerotoappstore.com/") let urlString = url.absoluteString
1

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 
} 

1 Comment

I have to modify to; urlString = (strurl as AnyObject).absoluteString!
1

Go through this link Here you can find url to string and string to url

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.