Joe Joe - 27 days ago 4x
Swift Question

Load Url image from Plist in Swift

I have a plist running in my project which contains number of image urls.I am trying to pass the url image to my imageView which is in the same viewController.I found similar questions from github like Loading/Downloading image from URL on Swift , Swift - Read plist , Can't get plist URL in Swift I went through all those answers but no luck so far rather than fatal crash. My partial codes as follows....

Method 1:

override func viewDidLoad() {

if let path = Bundle.main.path(forResource: "apps", ofType: "plist"),
let root = (NSArray(contentsOfFile: path))
let url = NSURL(string: path)
let data = NSData(contentsOf: url! as URL)
if let imageData = data {
imageView.image = UIImage(data: imageData as Data)

**// Swift Console Printinging as Follows**

// printing all my plist url links like {icon = "";}

// (/Users/xxxxxx/Library/Developer/CoreSimulator/Devices/52DA3F73-83E0-4C29-9DE1-D8D5F0731C13/data/Containers/Bundle/Applica ... ps.plist)

// nil

} else {
print("Either the file does not exist or the root object is an array")

Method 2:

let path = Bundle.main.path(forResource: "apps", ofType: "plist")
let url = NSURL(string: path!)
let imgData = try? Data(contentsOf: url as! URL)
let img = UIImage(data: imgData!)!

print(img) // fatal crash



path is the path to your plist, not to your image URL.

The image URL is in the key "icon" in the "root" array.

Get the first item of the array and subscript with the key, you should get your image URL:

if let item = root[0] as? [String:Any] {
    if let result = item["icon"] as? String {