Search code examples
iosuiimageuikitfoundation

Why isn't UIImage part of the Foundation framework?


I'm working on an app that allows the user to add a picture to the data model. (I'm using Realm, but I've done the same thing with CoreData and SQLite in the past.) I began building a model object with code like this:

import Foundation
import RealmSwift

class Picture: Object {
    dynamic var imagePath: NSString = ""

    var image: UIImage {
        get {
            ...
        }
        set(UIImage) {
            ...
        }
    }
}

This of course yields a Swift Compiler Error:

Use of undeclared type 'UIImage'

Adding import UIKit solves this problem, but then I wondered:

  • Why isn't UIImage a data model type?

  • Why isn't UIImage part of Foundation?

Does anyone know?


Solution

  • Why isn't UIImage part of Foundation?

    Why should it be? :)

    The UIImage class is for presenting an image on iOS (and the extended iOS family: watchOS, tvOS...). For macOS there's the NSImage class.

    It makes sense that UIImage is not in Foundation, because Foundation is shared on all platforms.

    Moreover, an image is not part of the fundamentals of a computing system, which is what the Foundation class tries to address.

    Therefore handling images should not go in Foundation but in classes of higher level, possibly specialized ones for specific platforms, and that's what Apple has done with UIImage and NSImage.