ChallengerGuy ChallengerGuy - 2 months ago 89
iOS Question

How do I express an empty URL in Swift 3?

I have an

iOS
project I'm working on using
Xcode 8
and now
Swift 3
. Before I started converting my project from
Swift 2
to the latest, I had the following in my
AppDelegate
:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {

// Transfer incoming file to global variable to be read
if url != "" {

// Load from Mail App

incomingFileTransfer = url

incomingStatus = "Incoming"

} else {

// Regular Load

print("App Delegate: No incoming file")

incomingFileTransfer = nil

}

return true
}


Worked great and it would see if there was an incoming PDF type file being opened by the app. I have PDF files associated with my app and being to open them within my app.

I used the migration tool to help convert my code to the new
Swift 3
code.

It converted my code in the
AppDelegate
and changed it to:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {

// Transfer incoming file to global variable to be read
if url != "" {

// Load from Mail App

incomingFileTransfer = url

incomingStatus = "Incoming"

} else {

// Regular Load

print("App Delegate: No incoming file")

incomingFileTransfer = nil

}

return true
}


However it gives the error:


Binary operator '!=' cannot be applied to operands of type 'URL' and
'String'


I understand the 'open url' that was an
NSURL
is now a type
URL
instead. How do I express an empty URL in
Xcode 8
and
Swift 3
now? I looked online and cannot find anything about this?

Thank you for the help and advice.

Answer

Try to use absoluteString property of URL to compare is it empty.

if url.absoluteString != "" {

}
else {

}

Note: If you just want to check for empty, then batter to use isEmpty property of String in your case it is look like if !url.absoluteString.isEmpty {

Comments