File not detected by other files upon moving to a folder

Hi,

This problem has occurred quite often for me.
For example,

I declare a file MyStruct.swift
Code Block swift
struct MyStruct {
let memberOne : String
let memberTwo : String
}

I put this into a folder (I personally use the MVC model, so in this case I'm moving this file to the Model folder).

Let's say that I have AnotherFile.swift with a view controller with a fairly trivial amount of code and this code block.

Code Block swift
...
var anArray : [MyStruct] = [
MyStruct(memberOne: "H", memberTwo: "W"),
MyString(memberOne: "e", memberTwo: "o")
]
...

When the file MyStruct.swift is in the Model folder (i.e. AppName/Model/MyStruct.swift and AppName/AnotherFile.swift), it does the above code block gives an error saying that it doesn't recognize the 'MyStruct'. However as soon as I move the file out of the Model folder (i.e. AppName/MyStruct.swift and AppName/AnotherFile.swift), it compiles fine. I'm not sure why this happens. Any help would be appreciated!

I use Xcode 11.5

Answered by Developer Tools Engineer in 615238022
When you move the file, are you moving it with the Finder, or with the Xcode project navigator (the list of files on the left side of Xcode)?

If you’re using the Finder, you might notice that the file turns red in the Xcode project navigator after you move it. This is because Xcode projects have their own list of files in the project, and the Finder doesn’t know that it needs to update that list. You can either drag the file around in the Xcode project navigator to move it, or correct it afterwards by selecting the red file in the project navigator, then using the first tab of the inspector (the pane on the right side of Xcode) to set the new location.

If you’re using the Xcode project navigator to move the file, moving probably ought to just work. You might want to talk with someone in the Xcode Open Hours labs about what you’re seeing—it may be a bug. If the file doesn’t turn red after you move it, cleaning your project (Product > Clean Build Folder) might work around the problem.
Accepted Answer
When you move the file, are you moving it with the Finder, or with the Xcode project navigator (the list of files on the left side of Xcode)?

If you’re using the Finder, you might notice that the file turns red in the Xcode project navigator after you move it. This is because Xcode projects have their own list of files in the project, and the Finder doesn’t know that it needs to update that list. You can either drag the file around in the Xcode project navigator to move it, or correct it afterwards by selecting the red file in the project navigator, then using the first tab of the inspector (the pane on the right side of Xcode) to set the new location.

If you’re using the Xcode project navigator to move the file, moving probably ought to just work. You might want to talk with someone in the Xcode Open Hours labs about what you’re seeing—it may be a bug. If the file doesn’t turn red after you move it, cleaning your project (Product > Clean Build Folder) might work around the problem.
I use the Xcode project navigator to do that. Using the Product > Clean Build Folder works in the sense that the build is successful and it runs properly after doing that, however Xcode still gives me an error: "Use of undeclared type MyStruct".
In the file inspector tab, in the target membership inspector, are MyStruct.swift and AnotherFile.swift both in the same target?
Yes. The same (and apparently the only) option is checked for target membership under the file inspector.

I had the same issue. What I've noticed is that if you move a file into a new group, and the file is already open in the editor, then the file apparently becomes un-locatable. I closed out all the files in the editor, then created a new group and then moved the files into the group. This also worked with 'creating a new group from selected'.

File not detected by other files upon moving to a folder
 
 
Q