Xcode does not allow me to specify file name when adding in SPM - xcode

I have a stand alone SPM (local) in my project. When ever I add any new files to this package Xcode does not show the prompt to specify the name. Usually when adding files in a project it requests the location which allows to specify a name. But when adding in a SPM it straight away creates a file with File.swift always.
I don't mind renaming but its just that the header is also having a generic File.swift
//
// File.swift
//
//
// Created by McDuck, Scrooge on 02/06/89.
//
Any work arounds ?
I have tried on Xcode 13.4 as well

#user5381191 I had the same problem. I did some searching and found this answer.
The file you want to edit is located in:
/Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File Templates/Source/Package Swift File.xctemplate. Edit the TemplateInfo.plist.
As a test I just copied the TemplateInfo.plist file from the Multiplatform/Source/Swift File.xctemplate and it worked. EDIT: For some reason it worked, and then stopped working?
Copy the suggested plist text from the link above into your template file and it works!
Once I did that, I get a new file type "Package Swift File" in the New File dialog.
Instead of the standard file dialog, it gives a dialog for entering the file name. Better than before!
Make sure to save your old template file before making your changes, just in case you need to revert.
It doesn't insert the package name in the header. That might be a nice thing to try and get working, but I'm happy with this for now.

Actually it works as promised (according to Apple guidelines).
In this scenario (as shown on demo) a menu command without ellipsis (...), so no dialog expected.
Once file created Xcode enters edit-name mode, so we can specify a file name which we need.
The file hat is different question and can be edited in templates if needed, or not. Usually depends on project and might even not include file name at all, but for default template - yes we need to change it to align.
Tested with Xcode 13.4

Related

Xcode "Cannot parse contents of Info.plist"

I just hit a brick wall with xCode not wanting to parse my Info.plist file. I've replaced the file several times with older (identical) versions of the file that I had previously backed up, and I'm still getting the same error.
Here is the complete error message:
couldn't parse contents of '/Users/...Info.plist': The data couldn’t be read because it isn’t in the correct format.
I'm clueless on this one. I'm using xCode 5.0.1, Mavericks
This is one of the best way to detect on which line the error is occurring.
Just go the directory where the plist file is present then write this command on terminal->
plutil filename.plist
Another cause of this issue can be from attempting to put URLs (really just slashes etc.) in your app-Info.plist.
Get around it by simply raising the -traditional flag inside of the Info.plist Other Preprocessor Flags option in your project build settings.
I think you have used source-control tools, you can use basic text-edit tool without Xcode to open this plist, command + F to find "<<<<<" or ">>>>>", then you will probably find error string such as:
<<<<<<< .mine
>>>>>>> .r605
select the correct string, and delete the other one, error is resolve!
Looks like replacing the file with a backup, then deleting the derived data for the project in Organizer was the cure. Hope this helps someone else later on.
I personally ruined the file when git merge raised conflicts. .plist is an XML file and git conflict added <<<< and >>>> in there to tag the differences.
Find the .plist file in Finder (in one of your project's folders). Open it in a text editor, find the lines that don't look like proper XML, remove them and be sure to remove the duplicate XML line/node (due to pre and post git recorded changes).
This may also simply happen because you have moved the Info.plist file into a new folder, or removed it from a folder (basically, if its path changed).
Go to Build Settings and search for "Info.plist file". Edit the value. For instance, if you have moved your plist file from the main folder to a subfolder called Resources, you will have to do the following change:
Before:
TargetName/Info.plist
After:
TargetName/Resources/Info.plist
When building for iOS, the Info.plist may be corrupted whenever Default Orientation is set to Auto Rotation. The Info.plist file is created properly on the first build, but subsequent builds results in...
<key>UIInterfaceOrientation</key>
<string></string>
</string>
... at this point Xcode fails to build the project.
I was having the same error, and realized the issue was that I had a URL (e.g. http://example.com/something) as a value in my Info.plist, and I just switched on pre-processing for it (without the -traditional flag). Apparently Xcode will treat the // as a comment marker, and omit the rest of the line.
A work-around I found is to embrace the pre-processor, like so:
http:/${}/example.com/something, which breaks up the // by putting an empty string substation in the middle so it doesn't look like a comment to Xcode, but after pre-processing it's back to a normal URL.
This error comes whenever Xcode preprocessor is not able to parse the info.plist file.
So to find out the error in the specific line do the following steps :
Open your project.xcworkspace in Xcode
Go to the project's navigator
Inside your project click on the info file, if an error exists then it will popup the dialog with the line number where the error exists
You can see the below image of the popup dialog for reference, which is in my case was showing the error on line 35
Hope this will help you or somebody else. Thanks!
Happy Coding :-)

Interface builder file content missing

Am a new bie to IOS development. While working with some sample apps, my mac restarted and after opening xcode i found one interface builder file .xib marked as red. Double click on it shows no content at all. I tried deleting and adding it back from xcode but its not working.
I dont see the file in the project directory too, but when i replace it with old file, it says
an item with same name exists
but its not visible in finder.
Any help would be appreciated.
I am not sure what is a clean solution for this but a quick fix/workaround which worked for me is this:
Along with the error message 'an item with same name exists' you must be getting an id/number which is duplicated.
Open the xib file in any text editor (TextWrangler etc.). It is just an xml file.
Search for that number in the xib. You'll find multiple occurrences of that id.
Replace one of the occurrences with another number and make sure the new number you've added is not a duplicate.
Save this file and open in xcode. The error should be resolved.
Right click on one file in the project and it displays the showinfinder . Then it opens project folder in the finder. Check your file whether it is existing in that or not.

What Does Question Mark Mean in Xcode Project Navigator?

In the project navigator in Xcode, I am not sure what the ? is trying to tell me next to the file name.
I am sure it is not a coincidence that this is the same file that I dragged from my root directory for the project to the Classes folder (because the .mm file was there and I wanted them to be together). The project still compiles just fine, however my code sense broke recently and I am wondering whether this may be part of the issue.
Thanks!
It's the file untracked by source control.
Those characters refer to the source control.
? - Unversioned
M - Modified
A - Added
A+ - Moved/Renamed (R as of Xcode 13)
U - Newer version of a file on source control
More reference here
You can add to source control by selecting the untracked files
Following steps resolved issue for me:
Select all your files with "?"
Right click and select Source Control
Select Add Selected Files
Select Commit Selected Files...
On the popup dialog just drop some comment (no matter what=)
Screen
Those are uncommitted files if your using GIT, you need to right click Source Control>Commit Selected File manually. It should take away the "?" on the new version of Xcode 6.3.2 as of today
Along with all other answers. This shouldn't happen by default UNLESS you have changed the settings in Xcode.
From Xcode Preferences >> Source Control >> make sure 'Add and remove files automatically' is selected
I suddenly get one my file Unversioned (with ? mark), but it was old file.
It was .m file. Before problem occurred, I created copy of it, for making new similar class. I renamed a class and add new file to XCode.
Problem (with old file become Unversioned) was in not renamed comment string at beginning:
//
// MyFirstClass.m
When I renamed it, problem with Unversioned file was resolved.
//
// MySecondClass.m
Hope it'll helpful somebody.
P.S. Additionally maybe needed to remove and copy back this files.
Stage the Unstaged file, if its a working copy and the ? will change to A

How to localize Settings.bundle in xcode4?

I am working on xcode4 and created a Settings.bundle for my project. I can edit the Root.plist file in xcode4 but it is not possible to edit the Root.strings file. It isn't even displayed in the editor.
I can not expand the en.lproj folder as you see in the picture.
But when I do right-click, open in external editor it gives me:
Double click on Root.strings gives me:
I tried it several times, creating a new window-based application project and creating a new Settings.bundle. I always can not localize it. Any ideas?
In XCode 4.2 you have to follow this steps (just a modification of Xcode 3.2.2 and localization of Settings.bundle)
Reveal your Settings.bundle in Finder.
Right click (or Ctrl-click) on it and select Show Package contents.
Create a new folder called as the desired language (e.g., fr.lproj).
Copy the Root.strings file from the en.lproj folder and paste in fr.lproj folder.
EDIT: Apple fixed, once again, the wrong bug. Their change rendered my workaround useless. It just doesn't work anymore, you can't add files to the Settings.bundle.
Please refer to the answer of Javi for a method that seems to work.
I hope you like ugly workarounds.
Right click on the settings bundle in the side bar.
Select New File
Pick Resource / Strings file
Name it Root.strings, make sure it is placed inside settings.bundle
In the side bar the file will be visible twice. In the "root" section, and in the Settings.bundle. But both point to the same files.
Select the Root.strings file in the root section. (AFAIR you have to convert it to UTF16)
Add localization, xcode will ask you if you want to replace the file. Yes, you want this.
Add as much localizations as you want. You should be able to edit the file in the root section of the sidebar.
I hope this works for you, for me it does.
And if you haven't done it please report the bug at bugreport.apple.com
I'll install the new release now, let's see if this is fixed. Edit: Not fixed.
EDIT: I don't know If I understood your second question correctly. But when you add a localization to the file that appears outside of the bundle it adds a localization folder to the settings.bundle
After adding spanish localization to Root.strings:
The actual file is in the foo.lproj folder, it just doesn't show them in the xcode sidebar. If you check the location in the file system you'll see they are inside the settings bundle.
Yesterday I checked with a english and german file and it worked correctly in the simulator. I guess it's just a wrong sidebar layout. The underlying locations and the handling of the files seems correct.
For me it worked to change the File Type (in the File Inspector) of the Folder "en.lproj" from "Default / Directory" to "Directory". Magic.
Xcode 4 is creating the "Root.strings" as a binary property list. so what I had to do was set the file type to "Property List (binary)" for it to show up correctly.
I have just given up trying to do something with Root.strings. Instead, I just copied the plist into each *.lproj, and it worked!
As far as this is about translation into English and my native language, no problem.
So I have:
Settings.bundle
|- en.lproj
| |- Root.plist
| `- Root.strings -- nothing useful there
|
`- zz.proj
`- Root.plist
The good news is that Root.plist is utf8.
If you already have a Root.strings file that is a "Property List (binary)" and you wish to convert it to a proper text-based strings file, you can use plutil in the Terminal application to convert it, for instance into JSON format:
> plutil -convert json /path/to/Settings.bundle/en.lproj/Root.strings
N.B. You can run plutil -h for usage help.
However, you will then need to convert the JSON format:
{"key1":"value1","key2":"value2"}
to that of a strings file:
"key1" = "value1";
"key2" = "value2";
This is easily done with a few search/replace operations in a text editor.

Adding an existing implementation file to an Xcode project won't work

As I write most of my code using MacVIM, I need to add the new files to the XCode project in order to compile them into the executable. Today however I encountered an implementation file (.m) which XCode won't allow to be added. It appears light gray in the appropriate finder window as if it were already included in the project, but when I try to build, I get a linker error, stating that the symbol defined this implementation file could not be found. The corresponding header file could be added without problems.
Any idea what could have caused this problem?
Btw. I deleted a former version of the same file from the "Classes" tree before, as well as many others which were successfully re-added.
I think I saw a similar problem once with an old version of Xcode. IIRC the way I resolved it was to:
rename the source file (temporarily)
add the renamed file to the project
do a "Save As..." on the renamed file to get it back to the original name
The file is probably already in your project, but not in the target you're building. Use the Detail view and Search bubble to find it, Get Info, and in the Targets tab of the info window, check the check box for the target you're building.
The other answer (removing and re-adding) works because you end up removing it from the project, then when you re-add it, it's added to the current target by default.
I found that deleting my pbxuser file from inside the .xcodeproj solved the problem for me. There were references to the files I couldn't add in there, surprisingly. This has happened to me many times over the years.
Another trick is to:
Move the file into a different directory
Add it to the XCode project from there
Move it to the desired location
Select the file in Groups & Files
Choose File > Get Info from the menu bar. The full path will appear in red.
Press the Choose... button and select the file at the new location.
If you are using Xcode 4, you can do the following steps to add an existing file into your target:
1) Select your file in "Project navigator" view (aka. click your myfile.m file in the folder tree of Xcode)
2) Click menu item "View" => "Utilities" => "Show File inspector" (or use the shortcut "Alt + Command + 1)
3) There is one section called "Target Membership", check the target you want
I fixed this by deleting the .xcuserstate file and restarting the project. I am using a workspace file, for all it's worth.
You can find any and all referencing files via
grep -r -h [NameOfFileThatYouAreTryingToAdd] *
from within your project directory.

Resources