Search code examples
iosamazon-web-servicesaws-amplify

Xcode project fails to find the 'AmplifyModels' file that is generated for iOS project using AWS Amplify DataStore


Recently, I was looking to begin an iOS project that uses the DataStore service of AWS Amplify. To get familiar, I followed the 'Getting Started' documentation found at https://sandbox.amplifyapp.com/start#datastore and accepted all the defaults to create a simple Blog application.

When I was in the 'Test' stage and on the last part of step 4, my Xcode project was failing to find 'AmplifyModels' even though they were generated successfully and exist in the /amplify/generated/models folder.

This is the code

import SwiftUI
import Amplify
import AmplifyPlugins

@main
struct BlogApp: App {
    
    public init() {
        let dataStorePlugin = AWSDataStorePlugin(modelRegistration: AmplifyModels())
        //let apiPlugin = AWSAPIPlugin(modelRegistration: AmplifyModels()) // UNCOMMENT this line once backend is deployed

        do {
            try Amplify.add(plugin: dataStorePlugin)
            //try Amplify.add(plugin: apiPlugin) // UNCOMMENT this line once backend is deployed
            try Amplify.configure()
            print("Initialized Amplify");
        } catch {
            print("Could not initialize Amplify: \(error)")
        }
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

This is the error

Cannot find 'AmplifyModels' in scope

Steps taken to try and resolve the error

  1. Clean and build Xcode project, along with quitting and restarting Xcode
  2. Manually adding the files to the project in Xcode. The code was able to find the files only if I removed them from the 'amplify' folder and placed them in the same folder as BlogApp and ContentView, but that cannot be correct I imagine.
  3. Cleared the DerivedData for the project and rebuilt.

I am running on Xcode 12.2. The reason that I decided to follow the 'Getting Started' documentation step by step is because I was failing with this same error for my own project I was creating, so I wanted to make sure that it was not some simple user error on my part.

Thank you in advance!


Solution

  • The instructions leave out one small step. You need to add the generated source files to the project so they'll get compiled.

    1. pick menu "File > Add Files to ..."

    2. select the amplify folder, and pick the 'Create groups' option

    file adding dialog