Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Use of undeclared type" error in Swift 5.1?

Tags:

swift

swiftui

I'm new to Swift and iOS development and try to learn by following the official SwiftUI tutorials. I got errors in section 5(https://developer.apple.com/tutorials/swiftui/handling-user-input). I followed each step meticulously and after I did section 5 step 1, Xcode popped up an error "Use of undeclared type 'UserData'". I tried to ignore and followed through. However, my LandmarkList, LandmarkDetail, SceneDelegate files warned me of the same error. I even checked the completed version of the project that I downloaded from the tutorial page. They looked exactly the same except the parts that I haven't done. Does anyone here have a similar experience as mine or can you kindly offer some suggestions? Thank you!

I'm using Xcode 11 and Swift 5.

This is the UserData class I created under the Models folder.

import Combine
import SwiftUI

final class UserData: ObservableObject {
    @Published var showFavoritesOnly = false
    @Published var landmarks = landmarkData
}

This is the rootview file that keeps prompting errors.

import SwiftUI

struct LandmarkList: View {
    @EnvironmentObject private var userData: UserData  // Use of undeclared type 'UserData'

    var body: some View {
        NavigationView {
            List { // Unable to infer complex closure return type; add explicit type to disambiguate
                Toggle(isOn: $userData.showFavoritesOnly){ // Use of unresolved identifier '$userData'
                    Text("Favorites Only")
                }

                ForEach(userData.landmarks) { landmark in
                    if !self.userData.showFavoritesOnly || landmark.isFavorite {
                        NavigationLink(destination: LandmarkDetail(landmark: landmark)
                            .environmentObject(self.userData)) {
                            LandmarkRow(landmark: landmark)
                        }
                    }
                }
            }
            .navigationBarTitle(Text("Landmarks"))
        }
    }
}

struct LandmarkList_Previews: PreviewProvider {
    static var previews: some View {
        ForEach(["iPhone SE", "iPhone XS Max"], id: \.self) { deviceName in
            LandmarkList()
                .previewDevice(PreviewDevice(rawValue: deviceName))
                .previewDisplayName(deviceName)
        }
        .environmentObject(UserData()) // Use of unresolved identifier 'UserData'
    }
}
like image 425
Amir Nie Avatar asked Jan 01 '23 13:01

Amir Nie


2 Answers

You need to clean and close Xcode, then delete Derived Data, then re-open Xcode. I had been constantly having this problem while doing apple tutorials.

like image 199
Yevgeniy Leychenko Avatar answered Jan 13 '23 02:01

Yevgeniy Leychenko


don't need to delete derived data folder or exit Xcode. just close project after tutorial section 5 step 5 then open project, allow it to index build then do step 6. this enables project to rebuild it's cache in memory.

like image 41
John LA Avatar answered Jan 13 '23 03:01

John LA