How do I add .m files to Xcode without conflicts - objective-c

I'm currently learning Objective-C and working my way through a book which contains lots of exercises. At the beginning of the book I set up an OS X project command line project and have been working in the main.m file, block commenting prior exercises, but it's becoming tiresome working in such a long file, constantly commenting and un-commenting the exercise code I want to build. I'd like to create separate files in the Navigator for each exercise so they are easy to find. How would I go about doing this without causing issues with the file in the Products folder?
I'm currently getting this red alert when I try to build after adding a new .m file:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This might not be a direct answer to the question (multiple files...) and a somewhat unorthodox approach to it, but it is a fun and convenient way to achieve what you describe by utilizing unit testing.
I understand that you are just starting to learn the language and the tools so I will make it as easy as possible. Let's get started!
1. Add a test target to your project
You just select the project in the navigator, and press the + button under 'targets' to add a new one.
select OS X > Other > Cocoa Testing Bundle and click 'Next'
add a product name (something like 'Exercises' would make sense in your case) and click 'Finish'
Now you should be able to see your new target in the list
2. Configuring the test target
Click and hold on the 'Run' button, select 'Test' from the popup menu
Now Xcode will ask you if you'd like to configure the project for testing. Click 'Edit Scheme...'
Select 'Test' from the left and click on the + button
select the test target we have created in previous step and click 'Close'
3. The Fun part
Now if you click on the 'Test Navigator' you'll see a list of your tests (your 'exercises'). Adding a test is as simple as adding a method starting with test - for example -(void)test_Exercise_x.
Furthermore you will now be able to verify that your answer to each exercise is what should be by using assertions and selectively run a test (exercise) on its own by clicking on the icon in the left of each test (the circled one in the screenshot) or of course, all of them by clicking on the 'Test' button or selecting Product > Test from the menu

Objective-C, C and C++ programs must include precisely one definition of main().
I expect it would work for you do something like:
int main( int argc, char *argv[] )
{
printf( "Solution to exercise #%d\n", getExerciseNumber() );
mySolution();
return 0;
}
you could reuse this file in each of the Xcode projects for your exercises. Alternatively you could have just one project that contained multiple targets, one for each exercise.
Does that help? I'm not completely clear that I understand your question.

Adding multiple files to my Xcode project looks like it's beyond my current abilities. I'll try and attempt to break the project down at a later date, when I'm more confident with Objective-C and Xcode. Thanks for your comments and help.

Related

Only the UWP project is running

It doesn't work if I try to add multiple pages to UNO SOLUTION. There are a lot of mistakes coming out. Only the UWP project is running. One of the mistakes.
LIST
First create a Page (Uno Platform UWP) then copy the body.
I also fixed the error in the same way.
To fix InitializeComponent error in Uno, you could refer to the following steps that are from the official document.
You could build your project once, close the solution and reopen it. It is also important to note that Uno uses a multi-project structure, for which each project has to be build individually for errors to disapear from the Error List window (notice the Project column values).
In order to clear the Error List window, build the whole solution completely once. Thereafter, build a specific project and prefer the use of the Output tool window (in the menu View -> Output), taking build messages by order of appearance.

List of all errors in project in CLion

CLion 2016.2 helpfully detects potential errors in the file you're editing, which can be seen in the validation bar to the right of the code.
That's just a single file though, is there a way (like a tool window) to get a list of all such warnings in the whole project, or specific parts of it?
Bonus points if it also lists warnings and errors from the compiler, though that's less important, because the compiler output already includes any it found.
Yes, it is possible. The feature you are looking for is called the Inspector.
Do: Find Action... | Inspect Code. It will show a pop-up that will allow you to select the scope: file, whole project, custom, and the Inspection profile (you can choose the type of errors you want to see):
After clicking OK, this is an example of the output, that you can navigate with the mouse or with keyboard shortcuts:
In version 2017.2, I have it under Code | Inspect Code....
You can also right click a folder in Project view and select Inspect Code... there to be able to check only that folder.

Problems exporting Mac App from Xcode to the computer desktop

I have a somewhat frustrating problem with exporting a compiled copy of a Mac App to my desktop and have it run. First some background...
The app contains two referenced subprojects. The first project creates an archive and the second one creates a framework. They are both used by the main project to perform certain functions.
The app runs fine within Xcode. The problem start when I archive it and try to export it to my desktop to see how well it runs on it's own. I use the regular procedures for this but when I try and start the app from my desktop, it won't load. There's a message saying that the dynamic loader can't find the framework object. The part of the error report that looks most helpful says this...
Dyld Error Message:
Library not loaded: #rpath/JXLS.framework/Versions/A/JXLS
Referenced from: /Users/USER/BuntingSimulator.app/Contents/MacOS/Baseball Situation Simulator
Reason: image not found
JXLS does exist but it's definitely not in the exported Mac app bundle. Anybody have any suggestions on where to look or what to look for? My suspicions lay in the value of #rpath and how the subproject is referenced or built within the main project.
Thanks in advance.
This was added in after I got the answer below. In case anyone needs to use this I thought I'd illustrate the changes made to the project as they are a bit obscure...
First, need to add a copy files step into the build phase into the main target. What does that mean?
The detail steps are:
1: click on the main target for the main project
2: select buid phases
3: click on the plus sign to add a build phase
4: will get a pop up box (select New Copy Files Phase)
5: change 'destination' selection to 'frameworks'
6: drag the framework object from the left panel into the name section of the
new copy phase you just created
Second, need to go into the 'build settings' of the framework target and replace #rpath to #executable_path/../Frameworks
If you are doing this for your own use, make sure you copy the JXLS to /System/Library/Frameworks.
Otherwise, look at this answer.

XCode 4 Autocomplete with C++ library?

I am using Cocos2D Box2D template for my project.
When I type , "b2**" the autocomplete doesnt show any Box2D classes. (pressing ESC also doesnt show any )
I have included Box2D.h and Cocos2d.h in "Prefix.pch" file.
Is there any feature like "rebuild index" in "Eclipse CDT" ?
Any tips?
You can force Xcode to rebuild index by deleting it's service folder (DerivedData for Xcode 4.x):
Close Xcode
Remove service folder
Open your project and Xcode will rebuild index again
In my case this always works and I can call b2... autosuggest.
The DerivedData folder can be found in ~/Library/Developer/Xcode/DerivedData
This may be an obvious suggestion, but have you tried to type out a class that you know exists with what you have included, and see if it recognizes it(With both colorization and/or quick help)? Also, do you have the necessary frameworks included, etc? As far as reloading goes, the only thing that you can do in XCode that I know of is clean(shift+cmd+k) and re-build. Are you getting any sort of warnings, errors, etc? Another potentially obvious issue, is XCode set to c++ as the language? Anyways hopefully you haven't already tried all of this and it helps you get somewhere.
Assuming you're using XCode 4, you can rebuild the index rather simply:
Open the Organizer (Command+Shift+2)
Click 'Projects' in the top section
Select the project you want to rebuild the index for in the left sidebar
Click 'Delete' next to 'Derived Data' in the center portion of the Organizer
This is easier than traversing through the directories and manually deleting a series of folders.
This page lists the steps another user went through to get code completion working for Xcode 4 and Box2D – the steps include deleting the derived data folder, but also changing user header search paths, making sure there were no spaces in the path to the project, and other things. Some of these steps may be random or unrelated, but apparently they fixed the problem for two people on that forum.
Combining all the above posts together, this is how I went about solving my issue:
1) Make sure that all files are in 1 folder. This may sound obvious, but I had my Box2D files not in the same folder as my other code. When creating a new project with XCode, it creates a folder with the name of your project, then another folder within that that has the name of the project again. Example: "{path of project}/TempProject/TempProject". I had my Box2D files in the first TempProject directory and not with the rest of my code that is created under the sub-directory of TempProject.
2) Open your project's settings by double clicking your project in XCode
3) Scroll down to "Search Paths"
4) Edit the "Header Search Paths" value to be: ${SOURCE_ROOT}/${PROJECT_NAME}/** (Note: If your Xcode does NOT create a subdirectory like mine does "TempProject/TempProject", then just use ${SOURCE_ROOT}/**)
5) Delete your project's Derived Data by doing 1 of the 2 following ways:
a) Open Organizer -> Select Projects -> Select your Project -> Select Delete next to Project's Derived Data
b) Open Finder -> Select Go from the toolbar -> type in "~/Library/Developer/XCode/DerivedData" -> Find and Delete desired project's folder
6) Restart Xcode
7) Clean and rebuild code
8) Code sense should now work!

Test Driven Development (TDD): Writing unit tests in Xcode 4

I am keen to write a library project with tests, even if I then include the code in other projects (as opposed to linking to the lib.) for inclusion on ios projects.
It seems checking the 'Include unit tests' isn't enough for xcode 4 to properly initialise a project with tests, and after looking through the Apple documentation I can't find anything relevant.
Does anybody know of any resources or tutorials for getting this working?
Even with the default Cocoa Touch Static Library project, upon choosing "Test" from the "Product" menu, it fails (when linking) with this error (the project is called test1):
library not found for -ltest1
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2 failed with exit code 1
Any help would be much appreciated.
OK I finally got this working.
The trick was to create the library without checking the 'Include unit tests' option in XCode 4. Then I created a new target myself.
(in xcode 4)
Create a new Cocoa Touch Static Library project and DON'T check "Include Unit Tests"
Select the project in the navigator
Go into the "Build Phases" tab
Click 'Add Target' and choose 'Cocoa Touch Unit Testing Bundle'
Go through the wizard until the target is created
Then go in the "Product" menu and choose "Edit Scheme..."
Make sure your project (not the test target) is selected and, select "Test"
Click + to add a new target
Choose your test project and click Add
Now press Cmd+U (this is the shortcut for "Product > Test" menu item) and you'll notice the default test fails.
I hope this helps.