Xcode Testing ML model for your app without running on the device

As of this writing, testing the image detection ML model inside the simulator (I am using Xcode 14.0 beta 3), won’t work if you are targeting iOS or iPadOS, you have to use an actual device. But now I stumbled onto the fact that if you can make your app, run for the “My Mac (Designed for iPad)” target, you can actually test the ML model without installing it on an actual device. So next time you need to work on the integration of an ML model in your app, you can use the “My Mac (Designed for iPad)” target, and move trough development without a problem. I do hope in future versions, you will be able to run ML models inside the simulator.

Extension for inverting the Color of your image in SwiftUI based on .dark or .light theme

I am using this little extension to invert the colors of the icons in my app. The icons are in black and white, and designed first to be used in the .light theme mode, that means they have beed design in black lines, so when the user will switch to dark, the icons should change the lines to white.

struct DetectThemeChange: ViewModifier {
    @Environment(\.colorScheme) var colorScheme

    func body(content: Content) -> some View {
        
        if(colorScheme == .dark){
            content.colorInvert()
        }else{
            content
        }
    }
}

extension View {
    func invertOnDarkTheme() -> some View {
        modifier(DetectThemeChange())
    }
}

//usage example

Image("iconName").resizable().scaledToFit().frame(height: 40).invertOnDarkTheme()

That’s it, now let’s get back to WWDC2022 which will start in a couple of hours.

Error – The VNCoreMLTransform request failed

If you are working with the vision framework and you are getting this error, while testing the iOS app in simulator, well, test it on the actual device and see if the error goes away. Very unfortunate error description.

This was very confusing personally, since the code was working fine while I was using it on an macOS app. Also I did not find any warnings anywhere that you are supposed to only test it on an actual device. Not sure how this makes any sense, but glad that I figure it out, It drove me crazy for the last 2 days.