Strings and Images

Strings:

let name: String = “Dani”
print(name[0])
// “D”

Images:

let myImage = UIImage(url: http://example.com/puppy.jpg)

// Instead of url to data, data to image etc

Hi, Dani!

This is trivially solved using an extension on UIImage:

extension UIImage {
    public convenience init?(url: URL) {
        guard let data = try? Data(contentsOf: url) else { return nil }
        self.init(data: data)
    }
}

// Or even:
extension UIImage {
    public convenience init?(url urlString: String) {
        guard
            let url = URL(string: urlString),
            let data = try? Data(contentsOf: url)
            else { return nil }
        self.init(data: data)
    }
}

I don’t think the use case is common enough to be included in the standard SDK, though. Also, notice that the initializer will most probably be called on the main thread, which is not a good place to send synchronous HTTP requests on.

1 Like

I‘m confused by this request. I don‘t see any change request for the former while latter is a request about Apple‘s UIKit framework. I‘ll mark this post as #off-topic (correct me if I‘m wrong).

1 Like

I think the first is a request to use Int as a subscript on String

1 Like

What @Letan said :blush:

And thank you @Zoul that’s helpful.

@DevAndArtist I’m new here. Where would UIKit be on-topic?

Thanks!

1 Like

Sorry I forgot to mention I’ve opened a proposal about this already

1 Like

Apple‘s Developer Forums or radar issues. (Latter for filing API requests)

1 Like