I'm new to Swift. I was pulling an image from the Internet when I came across NSURL
and URL
and NSData
and Data
. I'm really confused. Which ones do I use? I used the following code, but I had to convert the types as shown below. What is the proper way of doing this and what is the difference between NSURL
and URL
and NSData
and Data
? Somebody please help.
if let theProfileImageUrl = tweet.user.profileImageURL{
if let imageData = NSData(contentsOf: theProfileImageUrl as URL){
profileImageView.image = UIImage(data: imageData as Data)
}
}
Many class names dropped the
NS
prefix with the release of Swift 3. They should only be used with Objective-C code. If you are writing Swift 3 code you should use only the updated classes. In this case,URL
andData
.However,
URL
is bridged toNSURL
andData
is bridged toNSData
so you can cast between them when needed.I don't know where
tweet.user.profileImageURL
is defined but it appears to be usingNSURL
so you need to cast it toURL
in your code.