this line gives me error "use of unresolved identifier 'URL'"
let url = URL(string: urlpath!)
if you are swift ver is 3 then use
let url = URL(string: urlpath!)
else it is lesser than swift3 then Use NSURL instead of URL
let url = NSURL(string: urlpath!)
For swift 2.2 , this is the correct way:
let url = NSURL(string: urlpath!)
Related
Compiler throwing following warning when setting image in a cell using SDWebimage in Swift 4.2.
Swift Compiler warning :
Using '!' here is deprecated and will be removed in a future release
let url = NSURL(string: (str_url) as String)
cell.img!.sd_setImage(with: url as URL!, completed: block_image) //--- WARNING ON THIS LINE AT URL!
Any Suggestions ?
Use this code : cell. img!.sd_setImage(with: url! as URL, completed: block_image)
Suggestion: use URL instead of NSURL
let url = URL(string: "" ) //use url String
cell.img!.sd_setImage(with: url, completed: block_image)
Try this:
if let url = URL(string: str_url) {
cell.img!.sd_setImage(with: url, completed: block_image)
}
I'm trying to convert NSURL to NSData. My url string is a local file url which I'm getting from my device.
let url = NSURL(string: url)
let imageData = NSData(contentsOfURL: url)
let image = UIImage(data: imageData)
I'm getting NSURL value properly. But while converting it to NSData it gives nil value. I know there are similar questions in stackoverflow but none of them solves my problem. I'm using swift 2.2
If the string points to a local file url then you are using the wrong API. The correct one is
let url = NSURL(fileURLWithPath: urlString)
NSURL(string: is only for URL strings which start with a valid file scheme (e.g. http or ftp)
Try this code.
let urlString = "/var/mobile/Media/DCIM/101APPLE/IMG_1827.JPG"
let url = URL(fileURLWithPath: urlString)
let imageData = NSData(contentsOf: url)
let image = UIImage(data: imageData as! Data)
The Correct one is use contentsOf: forecastURL! as URL instead of contentsOf: forecastURL
let forecastURL = NSURL(string: "http://photos.state.gov/libraries/media/788/images/90x90.gif")
let testImage = NSData (contentsOf: forecastURL! as URL)
print("data",testImage!)
let image = UIImage(data: testImage! as Data)
print("imaGE :-",image!)
In Swift 2, I had used the following code:
let path = NSBundle.mainBundle().pathForResource("Document", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
webView.loadRequest(NSURLRequest(URL: url))
Now, using Xcode 8 and Swift 3, Xcode automatically translated it to:
let path = Bundle.main.pathForResource("Translation", ofType: "pdf")!
let url = URL.fileURL(withPath: path)
webView.loadRequest(URLRequest(url: url))
On the second line, with the declaration of url, Xcode gives me the following error:
Type 'URL' has no member 'fileURL'
How can I fix this error? Thanks!
The URL struct in Swift 3 has an initializer for that
let url = URL(fileURLWithPath: path)
If you do not use path later, you can write something like this:
let url = Bundle.main.urlForResource("Translation", withExtension: "pdf")
Hello when I use WKWebView codes with Swift 3 gives me this error
'URLRequest'; did you mean to use 'as' to explicitly convert?
I think this is bug I need help or ideas ? My codes under below
Thanks
import UIKit
import WebKit
class SocialsViewController: UIViewController, WKNavigationDelegate {
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://facebook.com")!
webView.loadRequest(NSURLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Use URL and URLRequest instead:
let url = URL(string: "https://facebook.com")!
webView.load(URLRequest(url: url))
This is quite similar to https://stackoverflow.com/a/37812485/2227743: you either use NSURL and have to downcast it as URL, or you directly use the new Swift 3 structs.
If you follow the error message, your example would become:
let url = NSURL(string: "https://facebook.com")!
webView.load(URLRequest(url: url as URL))
It could be even worse:
let url = NSURL(string: "https://facebook.com")!
webView.load(NSURLRequest(url: url as URL) as URLRequest)
All this works but of course it's much better to start using URL and URLRequest in Swift 3, without using all this downcasting.
Try this one
let myUrl = URL(string: "https://facebook.com")!
webView.loadRequest(URLRequest(url:myUrl));
I updated to iOS 8.1 and updated my xcode but now something is broken on with my webview.
I used this part of code:
let stream = "http://stream.hive365.co.uk:8088/live"
let url = NSURL(string: stream)
let request = NSURLRequest(URL: url)
webplayer.loadRequest(request)
xcode gives a error about the let request
value of optional type NSURL? not unwrapped; did you mean to use! or ?
I'm new with iOS swift development and can't get the error away...
any help would be appreciated!
NSURL returns an optional so you need to check if it is nil
let stream = "http://stream.hive365.co.uk:8088/live"
if let url = NSURL(string: stream) {
let request = NSURLRequest(URL: url)
webplayer.loadRequest(request)
}
change
let url = NSURL(string: stream)
to
let url = NSURL(string: stream)?
or
let url : NSURL = NSURL(string: stream) as NSURL