Files added to xcode project show red on another Mac - xcode

Files added to xcode project show red on another Mac. Both the windows while adding files to the project look the same. When I re-add them in the newly opened project, it starts working fine. I need to know what's causing this.

When you add files to Xcode project, you would see "Copy items to destination group's folder". By default it's unchecked. So it copies references. When you move the resource that you added to Xcode to a different location on same computer or open in another computer it looks for that reference which is missing. So you see missing items as red.
If you check that box "Copy items to destination group's folder" when prompted for, you wouldn't see that.
There can be another reason. If you or someone have accidentally removed file from the Project folder from finder but not Xcode, you would see same effect.

There is an option in the project selecting if files should be stored with absolute or relative paths; it may be set to absolute. If it is, other machines will only be able to find the files if they are in the exact same location and not just in the same location relative to the project file.
You can test this by adding a file and saving the project then checking it in a text editor. Right click on the project in Finder and click "Show package contents." You can then open project.pbxproj (or something like that) in any text editor. You can even use Xcode. Find the file you just added and see how it is storing the path.

I managed to get everything red by accidentally moving the project folder.
After moving the directory back all files were red-marked.
I resolved this by clicking on the project root on the left pane
Clicking the arrow pointing right next to the "Full path" on the right pane under Identity.
By selecting another option and back the project refreshed itself.

I guess it depends on your preferred work environment but i continuously copy all my files over to avoid the dreaded red triangles.

xcode buggyness, you should try this next time:
rm -rf ~/Library/Developer/Xcode/DerivedData/

Related

Couldn't load a Xcode project with pods [duplicate]

How do I rename a project in Xcode 5?
What steps do I need to take?
In the past this was always a very tricky manual process.
Well, the answer is very very very Apple simple in Xcode 5!
In the Project Navigator on the left side, click 2 x slowly and the Project file name will be editable.
Type the new name.
A sheet will appear with a warning and will list all the items Xcode 5 believes it should change.
You can probably trust it, but you should inspect it.
The list will include the info.plist and various files, but also all the relevant strings from nib/xib files like MainMenu menus.
Accept the changes and you will get the prompt to save a snapshot of the project.
Always make a snapshot when Xcode asks, it will be useful to restore if something does not work.
Change the project name:-
Click on the target in xcode, on the right in "Identify and Type" under name change the name and press the ENTER button on your keyboard.
A window will appear confirming the change and what it will change. Once you confirm it will make the changes.
Change the root folder name:-
Go to the project directory and rename the root folder,
Open the project and u will find all the file are missing, u need to add all the files of project again
Right click the project bundle .xcodeproj file and select “Show Package Contents” from the context menu. Open the .pbxproj file with any text editor.
4>Search and replace any occurrence of the original folder name with the new folder name.
5>Save the file.
Change the Scheme name:-
rename .xscheme file
If your Project is static framework then make sure your header file has public target membership
I really recommend just opening the folder in a general editor such as Sublime Text, and doing a find/replace across the whole folder. The other methods I found were unstable, particularly when combined with .xcworkspace and cocoapods.
In Xcode 8.0, to rename your project, just go through the following instructions as described in Xcode help:
1- Select your project in the project navigator.
2- In the Identity and Type section of the File inspector, enter a new
name into the Name field.
3- Press Return.
A dialog is displayed, listing the items in your project that can be
renamed. The dialog includes a preview of how the items will appear
after the change.
4- To selectively rename items, disable the checkboxes for any items
you don’t want to rename. To rename only your app, leave the app
selected and deselect all other items.
5- Click Rename.
Source: http://help.apple.com/xcode/mac/8.0/#/dev3db3afe4f
Xcode 6 (beta 6 as of now) seems to be not very reliable with renaming projects. For me it didn't rename several of the files and groups. It also doesn't rename the physical folder the project is in. To rename my project to be sure that everything is clean I went the extra length to create a new project with the new name and copy over all the files. The assets are easy to copy but groups have to be recreated. The biggest issue with this however are CoreData data model files. Trying to simply copy this will result in a corrupt model file, even though everything looks like it is alright.
When you re-name the project name in XCode5 then info.plist entry removed from Targets --- > General ---> identity. You just need to mention it again.
In Xcode 7, renaming a project can still break your app. Make sure you backed it up before trying it.
Click on the project icon and find the project name in the inspector pane. If you change it there, Xcode will ask you if you want to rename related files. Might work. But if not, try this brute force approach:
Close Xcode
Using an advanced text editor like Sublime Text or Atom, open the
root folder. It will open the folder structure.
Perform a Global Search and Replace (it's probably cmd + shift + f), and
replace My Wrong App Name with New App. If your project name contained spaces, also search for My_Wrong_App_Name and replace
with
New_App. This changes all file contents.
Now you need to find all
the files inside the project with your old app name. Rename them
all, also the folders.
Important: Open the project file with
right click > Show Package Contents, and rename all files in there.
Reopen your Xcode project or workspace. Compile.
If you use Pods, you need to open the pods project as well and change the files in there.
Here is another great example which works well with xcode 5

How do I rename a project in Xcode 5?

How do I rename a project in Xcode 5?
What steps do I need to take?
In the past this was always a very tricky manual process.
Well, the answer is very very very Apple simple in Xcode 5!
In the Project Navigator on the left side, click 2 x slowly and the Project file name will be editable.
Type the new name.
A sheet will appear with a warning and will list all the items Xcode 5 believes it should change.
You can probably trust it, but you should inspect it.
The list will include the info.plist and various files, but also all the relevant strings from nib/xib files like MainMenu menus.
Accept the changes and you will get the prompt to save a snapshot of the project.
Always make a snapshot when Xcode asks, it will be useful to restore if something does not work.
Change the project name:-
Click on the target in xcode, on the right in "Identify and Type" under name change the name and press the ENTER button on your keyboard.
A window will appear confirming the change and what it will change. Once you confirm it will make the changes.
Change the root folder name:-
Go to the project directory and rename the root folder,
Open the project and u will find all the file are missing, u need to add all the files of project again
Right click the project bundle .xcodeproj file and select “Show Package Contents” from the context menu. Open the .pbxproj file with any text editor.
4>Search and replace any occurrence of the original folder name with the new folder name.
5>Save the file.
Change the Scheme name:-
rename .xscheme file
If your Project is static framework then make sure your header file has public target membership
I really recommend just opening the folder in a general editor such as Sublime Text, and doing a find/replace across the whole folder. The other methods I found were unstable, particularly when combined with .xcworkspace and cocoapods.
In Xcode 8.0, to rename your project, just go through the following instructions as described in Xcode help:
1- Select your project in the project navigator.
2- In the Identity and Type section of the File inspector, enter a new
name into the Name field.
3- Press Return.
A dialog is displayed, listing the items in your project that can be
renamed. The dialog includes a preview of how the items will appear
after the change.
4- To selectively rename items, disable the checkboxes for any items
you don’t want to rename. To rename only your app, leave the app
selected and deselect all other items.
5- Click Rename.
Source: http://help.apple.com/xcode/mac/8.0/#/dev3db3afe4f
Xcode 6 (beta 6 as of now) seems to be not very reliable with renaming projects. For me it didn't rename several of the files and groups. It also doesn't rename the physical folder the project is in. To rename my project to be sure that everything is clean I went the extra length to create a new project with the new name and copy over all the files. The assets are easy to copy but groups have to be recreated. The biggest issue with this however are CoreData data model files. Trying to simply copy this will result in a corrupt model file, even though everything looks like it is alright.
When you re-name the project name in XCode5 then info.plist entry removed from Targets --- > General ---> identity. You just need to mention it again.
In Xcode 7, renaming a project can still break your app. Make sure you backed it up before trying it.
Click on the project icon and find the project name in the inspector pane. If you change it there, Xcode will ask you if you want to rename related files. Might work. But if not, try this brute force approach:
Close Xcode
Using an advanced text editor like Sublime Text or Atom, open the
root folder. It will open the folder structure.
Perform a Global Search and Replace (it's probably cmd + shift + f), and
replace My Wrong App Name with New App. If your project name contained spaces, also search for My_Wrong_App_Name and replace
with
New_App. This changes all file contents.
Now you need to find all
the files inside the project with your old app name. Rename them
all, also the folders.
Important: Open the project file with
right click > Show Package Contents, and rename all files in there.
Reopen your Xcode project or workspace. Compile.
If you use Pods, you need to open the pods project as well and change the files in there.
Here is another great example which works well with xcode 5

How can I change the location of files in xcode project?

I put some files on the same level of *.xcodeproj file carelessly. When I move these files into the right location in Finder, Xcode tells me that these files are missing. So I delete the group which contains these files whose names have been read (I realize it is a mistake). And I drag these files from Finder into the xcode to build a new group. But when I try to run the project, Xcode still can't find these code. How can I solve the problem?
When you see a file that is red (or in the wrong place), you can easily correct the location of these files by using the file inspector (which appears along the right side panel of your Xcode window).
It looks like this:
Clicking on the Folder icon next to the name will bring up an open panel from which you can choose the correct location of the file.
There is an easier way to re-organize files through the Xcode project. You can use Synx, it automatically fixes file paths, through the Xcode project groups.
You can check Github page for Synx
Synx
After intalling Synx just run the command below
synx path/to/my/project.xcodeproj
I found the answer.
3 things
1
removed the reference to the red files
then dragged them back into the project
relinked all my .m and .h files just incase
2
then removed the .app reference and dragged it back in to the project.
3
removed any duplicates or red files in the build phases
Fixed
Thanks everyone for your suggestions and help
From my point of view i would definitely avoid the virtual folders in the Xcode.The disadventage of this approach is whe nthe project gets bigger and bigger you can not find corresponding files in your filesystem easy.Even you click on "Show in finder" you need to look for the file among tens of files.
My approach is creating the folder in the finder and then dragging it to project.when i need to create a new file in this subfolder is will be added there anyhow.
Drag the new files from the finder to the Xcode project file structure.
Remove the old files.
I had a lot of class and find a good solution for solve this problem :)
1- Select a file that you want to change the location (Red)
2- Select "show the file inspector" from right side in Xcode
3- Select wrong path or unexpected path and copy like this screenshot
4- Close your project
5- Go to your root project and right click on "project *.xcodeproj" and select "Show package contents"
6- Right click on "project.pbxproj" and open with TextEdit
7- In the file select "Find and replace" or click on " Option+Command+F " and find location that you copied and replace with "" empty
8- Save and open the Xcode :)

Renaming xcode 4 project and the actual folder

I know how to rename the project in Xcode 4, but how do you rename the source folder? The thing is that renaming the project in Xcode, does only rename within Xcode (Though it is progress compared to previous) - but why Xcode is not renaming the folder in the filesystem I don't know.
You can do this from within Xcode without resorting to hand editing the pbxproj file in a text editor!
Here's how:
Rename your source folder in the Finder. This will break the project initially because Xcode doesn't automatically detect when the real folders that the virtual (yellow) project folders point to are changed, so all the files inside that folder will turn red in Xcode. Don't worry about that, we'll fix it in step 3.
In Xcode, click on the (now broken) virtual folder in the left-hand sidebar and rename it to match the name in the Finder (you can actually call it anything you want, or not rename it at all, but that would be insane). That won't fix the red files inside the folder, but that's OK.
In the right-hand sidebar in Xcode, click the icon shown in the screenshot below to bring up a file dialog. Select the folder that you previously renamed. This will re-associate the virtual folder with the real folder on disk, and will automatically fix the links to all the files inside.
In your build settings, search for the old folder name and manually rename any occurrences you find. There will typically be two of them, oldfolder/prefix.pch and oldfolder/info.plist, but there may be others.
All done!
EDIT: this option is still there in Xcode 5, but is helpfully hidden by default. To show it, click the thin "Identity and Type" header in the inspector above the "Text Settings" panel to expand it.
I've done it dozens of times:
Rename the Project by clicking then editing the Title in your Xcode Project Navigator.
Close Xcode.
Rename the source folder.
From the Finder, open the .pbxproj file inside project bundle (context menu "Show Package Contents") with any text editor.
Search and replace any occurrence of the original folder name with the new folder name.
Save the file.
Open Xcode project.
Task:
Rename "Play" to "Play2" both in Xcode and in Finder
Change this:
To this:
STEPS:
// Before anything, please backup the project in Finder!
Rename project in Xcode Project navigator // Click project name then hit enter
Rename group in Xcode Project navigator // not affecting folder name in Finder
Close Xcode
Rename outer folder (left column in image) in Finder
// All above steps are straight-forward and without side effects
//
// Now comes the tricky part: rename the inner folder (middle column in image) in Finder (the one at the same level with .xcodeproj)
Rename the inner folder in Finder while keeping Xcode closed
Right click .xcodeproj, select "Show Package Contents"
Open .pbxproj file with Mac TextEdit
Search and replace all "Play" with "Play2". To guarantee everything goes right, click the arrow on the left end of the search bar and modify the search attribute. Change "Contains" to "Full word", and uncheck "Ignore case"
Save .pbxproj file
Open Xcode project and Run
Here is how to rename a project effectively in XCode4 (INCLUDING THE FILES FOLDER):
Close all open projects completely in XCode to prevent any issues
Copy the Project in question to a new Folder (IMPORTANT: KEEP A BACKUP OF YOUR OLD PROJECT)
Open the Project
Rename the Project itself in XCode4 (double click on the project name), change the name and follow the steps (yes to everything)
Go to product, manage schemes and rename the scheme (double click on the scheme name)
Close your project (make sure is completely closed otherwise the next steps will not work)
In Finder go to the Project folder and right click on the .xcodeproj file, then select the option "Show Package Contents"
Then right click on the .pbxproj file and open it with TextEdit
Replace all occurrences of the Folder Name where the projects used to live (The old project name for the new project name)
Save the file
Open the Project again and Build it!
First of - dont rename an Xcode project!!!
If you have to, the beneath could be it - I never got it to work, ended up creating a new project and started all over copying in the existing files. Xcode is so ahh.... It has so many non productive things, like also try renaming a file from "Name" to "name". Well, well have fun.
I found the answer or how to work around this: Open the pbxproj file by right clicking "Show Package Content" and in there you edit a project.pbxproj file - you have to rename a reference called path and some other stuff - just search for the folder name added "/".
That is it.
Hope it helps some one.
Here is another issue that you should be aware of while replacing values in your *.pbxproj file. If your old folder name was FooBar you will have an entry in your *.pbxproj file that looks like this:
path = FooBar
Suppose your new folder name is Foo Bar. That is, you are introducing a space. Then, this line should become
path = "Foo Bar"
If you just do a simple find and replace, the result will be a *.pbxproj file that Xcode can't parse. If your new folder name has a space and the old name didn't have a space, be sure to include the double quotes.
I am summing up for myself, after examining various answers above and trying myself.
Let's assume the current project name is ProjAbc and you want to rename it to ProjDef:
Exit Xcode of that project.
In Finder, Duplicate the folder ABOVE ProjAbc.xcodeproj for backup.
In Finder, Rename freely the folder ABOVE ProjAbc.xcodeproj if you feel you need it to suit new project name.
In Finder, Rename the source files folder parallel to ProjAbc.xcodeproj from ProjAbc to ProjDef
Open Xcode for ProjAbc.xcodeproj (in Project Navigator, all files in virtual source folder ProjAbc will be in Red color).
In Xcode, click on the (now broken) virtual folder in the Project Navigator in the left-hand sidebar and rename it to match the name in Finder - ProjDef. That won't yet fix the red files inside the folder, but that's OK.
In Xcode, (still selecting the virtual folder in the left-hand sidebar) in the right-hand sidebar, in the File inspector, click the icon shown in the screenshot below to bring up a file dialog. In the file dialog, select the folder that you previously renamed (ProjDef). This will re-associate the virtual folder with the real folder on disk, and will automatically fix the links to all the files inside.
In Xcode, in Project Navigator, click on the ProjAbc project name, and rename it to ProjDef.
You will be prompted: "Rename project content items?". Click "Rename" button.
You will be prompted: "Would you like Xcode to take automatic snapshots before Project Rename and similar operations?". Click either "Disable" or "Enable". It does not matter.
In Xcode, now open "Build Settings" inside ProjDef target (inside project ProjDef in Project Navigator). Rename both foldername and filename of both *-Info.plist and *-Prefix.pch files to contain ProjDef instead of ProjAbc.
In Xcode, in Project Navigator, in ProjDef virtual sources folder, in "Supporting Files", also rename again both *-Info.plist and *-Prefix.pch files to contain ProjDef instead of ProjAbc
In Xcode Menu, press Product/Scheme/Manage Schemes... (or click "ProjAbc"> Scheme name in Scheme Bar at left top of Xcode, and press "Manage Schemes...") and in the dialog box rename Scheme from ProjAbc to ProjDef, then press "OK" button. If Project name is composed of single word, you may run it just now!
In Xcode, edit the top comment line in Supporting Files/*-Prefix.pch file, and replace ProjAbc by ProjDef (2 times). Of course correcting this comment is optional.
Exit Xcode.
In Finder, point to ProjDef.xcodeproj and open it in text editor. (Either open it in TextWrangler.app , or, mouse right-click and in the pop-up menu press "Show Package Contents" then edit file project.pbxproj in TextEdit.app)
In the Text Editor, search "ProjAbc" (Entire Word, Match Case) which should appear once (in paragraph: /* Begin PBXNativeTarget section */ in line: productName = ProjAbc;) and replace it by "ProjDef". Indeed, project could run anyway!, so this step is optional.
if project name is composed of 2 or more separate words, like "ProjDef Geh", then you MUST do next steps: Search for the reference to the Finder folder of the virtual source files folder -
Search for the line: path = ProjDef Geh;
It will appear once, in paragraph /* ProjDef Geh */
Replace it by the line: path = "ProjDef Geh";
Save from text editor, and exit the Text Editor.
That's it. As written above, if project name is composed of single word, you can run right at end of step 11.
I found the above answers really useful, especially Nick Lockwood's answer.
I just renamed a couple of folders in Xcode 8 and encountered two more issues:
A) "missing from working copy errors"
B) Core Data crashes.
How to resolve these two issues:
A) "missing from working copy errors"
I found the below answer very useful: Swift 3 / Xcode 8 Upgrade - 100's of DerivedData files missing from working copy errors
Basically Xcode 8 has rigorous source control, which can lead to the above errors. You have to:
Clean Xcode derived data.
Go to Source Control and click Refresh Status.
Go to Source Control and click Commit; select all the files on the left sidebar of the commit menu when committing (right click -> Select All).
You'll probably get an error.
Close the Commit menu.
Refresh Status.
Close your project by quitting Xcode.
Reopen your project, refresh status, commit again.
This time the commit should work.
Click Project -> Clean.
I'm assuming that you have already enabled Source Control. If you haven't, or if you get stuck following my instructions, just follow the more detailed instructions in the link provided.
B) Core Data crashes
Renaming folders will sometimes screw up Core Data. There are several possible reasons why this is so, and below are some fixes I can suggest:
Incorrect AppDelegate model name. This was the case in my situation. Go to your AppDelegate and find let container = NSPersistentContainer(name: "YOUR CORE DATA MODEL NAME"). Make sure that the name is correct.
In the future (several years after this post, when Apple changes Core Data again) or if you're using an older version of Core Data, take a careful look at AppDelegate. There's probably something funky there that's messing with your Core Data.
Incorrect model name in xcdatamodeld. Go to Finder and open your .xcdatamodeld in a text editor.
Check the following:
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>YOUR CORE DATA MODEL NAME.xcdatamodel</string> <-- Make sure this is correct.
</dict>
</plist>
More detail here: Core Data model files does not load on rename
If the two things above don't work, try to 1) delete your entity and then add another entity that's identical or 2) delete your xcdatamodeld and then add another data model that's identical (and make sure that the name of the new xcdatamodeld matches what is in your AppDelegate and the rest of your code). Never do this with a live App - or if you do this, make sure to properly migrate your Core Data whilst doing so.
If you are have old reference of source project into your project, When dublicate the project (every change made affect to both proejct)
here the solution
Dublicate Project from source project
Rename source folder of source project that break reference issue.
In dublicate proejct everything goes red choose your current folder of project. (screenshot is shows in green tick answer of stackoverflow)
========================================================================

How to quickly add back missing file in xcode

I zipped up my project folder in xcode and moved it to a new computer. When I open up the project it has missing files (file appears red).
How can I add all these files back or re-link them. It is like 20+ files?
Why didnt xcode store the relative path?
Xcode may not update location for some files of your project when you copy/move it from one environment to another. The easiest way I found is something like this:
1-Select your files like this, if they are in different groups, then repeat the flow on each group separately:
2- Show the file inspector:
3- You may notice Xcode has an absolute path for them, which is something not useful, so click on the little icon near Multiple Values. navigate to the folder where your selected files are stored in the finder and click "Choose".
Also, don't forget to change the Location to Relative to Project. Now you will get something like this:
• Select all the missing files that are in one folder in Finder.
• Change Location to Relative to Group ( that's in the File Inspector View -> Utilities -> Show File Inspector )
• Click the choose file button; it is under the Location drop down menu; it is an image that looks like a very small window with a document inside it.
A Choose folder containing the selected references sheet should appear.
• find and select the folder containing the missing files in the finder, then click Choose
(Tested in In xCode v4.3.2)
I normally keep all my files related to the project inside one folder (nested where necessary) and yeah I frequently exchange project files (zip and move) with my peers and nothing like that has ever happened to me.
You can always just drag the files en masse back onto the XCode window, and they'll get re-added. If you have file-system folders that match your Xcode internal organization that makes it even easier.
The UI has changed. In Xcode 10, there's a tiny dot with an arrow in it next to the file path. This does nothing as far as I can tell.
There's an obscure folder icon offset up and to the right of that non-functional arrow... this actually is a button, and it DOES allow you to relink the file.

Resources