2

I was working on a project the other day, and my preview stopped working. I got the error "Compiling Failed, cannot find "DataType" in scope" (all code/relationships will be shown below). I have restarted XCode a few times, and even commented out that area of code but it continues to not work, is anyone else having this problem and how can I fix it?

I am previewing a sheet called "Profile Sheet", just a standard swiftUI View, so the preview code looks like:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ProfileSheet()
    }
}

In the profile sheet, it is accessing data from a static function associated with my model file (different struct, different file), the functions it is using, and the ones that define "DataType", the problematic generic are here:

static func saveData<DataType>(data: DataType, for key: String) {
        defaults.setValue(data, forKey: key)
    }
    
static func retrieveData<DataType>(defaultValue: DataType, for key: String) -> DataType {
        guard let value = defaults.value(forKey: key) as? DataType else {return defaultValue}
        return value
    }
    
static func saveComplexData<DataType>(data: DataType, for key: String) where DataType: Codable {
        let encodedData = try! JSONEncoder().encode(data)
        defaults.setValue(encodedData, forKey: key)
    }

static func retrieveComplexData<DataType>(defaultValue: DataType, for key: String) ->  DataType where DataType: Codable {
        guard let retrievedData = defaults.value(forKey: key) as? Data else { return defaultValue}
        let decodedData = try! JSONDecoder().decode(DataType.self, from: retrievedData)
        return decodedData

    }

all of these function as intended, and obviously compile. Even when I run to launch the previewer and compiles my app does it builder properly, its only when it is then trying to launch the preview simulator.

Finally, I thought Id remove those functions entirely and just try to display a View that has no connection to them, so I previewed Text("test") and got the same error.

on Xcode 13 beta 3 & 4 I am able to run the simulator, but I cannot work only on those betas as they are still fairly unstable.

Any help is appreciated :)

Brian.Masse
  • 75
  • 1
  • 6
  • Did you try clearing the XCode caches? Go to: < About This Mac - Storage - Developer > and delete the Project Build Data and Indexes there. – Simon Aug 06 '21 at 14:48

1 Answers1

0

I've seen quite a bit of weird behavior with frameworks, I think due to changes to the simulators to support Apple silicon. My temporary workaround is, in my app/extension targets, to add "arm64" to the Excluded Architectures build setting when building for the simulator (as your preview appears to be trying to do), and setting "Build Active Architecture Only" to No for all schemes. Might be worth a try.

As per triplette in apple developer answers.

Try to check it out https://developer.apple.com/forums/thread/657913

I believe it's not an issue with your code it's an issue with Xcode due to apple silicon macs. try to do a software update and reinstall the Xcode.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • thanks so much, I just did exactly this and I'm able to preview again. l'll also loop in this post alogn with your solution, for anyone else having this issue, the two seem closely related :) https://stackoverflow.com/questions/63607158/xcode-12-building-for-ios-simulator-but-linking-in-object-file-built-for-ios – Brian.Masse Aug 08 '21 at 16:19
  • ill also add taht you have to exclude "arm64" to all targets plus the app itself, or it still doesnt preview – Brian.Masse Aug 08 '21 at 16:25
  • 1
    This solution does not work for me, there are errors with multiple libraries that contain only arm64 and not x86-64 – Oleg991 Jan 08 '22 at 08:47
  • @Oleg991 are you running it on M1 Chip? Because it has worked it for me for M1 Chip – Nursultan Yelemessov Jan 13 '22 at 08:53
  • @NursultanYelemessov yes I do run it on M1. All dependencies are installed via SPM. I suppose the problem is in 1-2 libraries from Yandex which do not support M1 at all. – Oleg991 Feb 21 '22 at 10:13