import "cocos2d.h" works in some files, but not all - objective-c

Here is what I did:
In Xcode 4.3.1
File -> New -> Project -> Single view application
Dragged the file cocos2d-ios.xcodeproj into navigator.
In build phases settings : Added : libcocos2d.a (becomes highlighted in red), as a linked library (required).
Added OpenGles.framework, Quartzcore, and libz.dylib
Changed build settings - Set "Always Search User Paths" to YES
Added cocos2d source directory to "User Header Search Paths"
Now, it seems I can type: import "cocos2d.h" , in the app delegate and root view controller that Xcode created. But if I create a new file, and I add the line "import "cocos2d.h"" to the top, Xcode complains that the file is not found. But it seems to build fine. Also, in this new file code sense does not work.
What should I do? Why can I import only in the files that Xcode created? Is there some setting I need to change so that in the files I create, I can import cocos2d ?
EDIT: It seems to build and run fine. I can call methods in the cocos2d api. code sense just doesn't seem to see cocos2d.h in the new files I create.
EDIT - it seems that code sense suggests cocos2d.h when i type : import "
But it does not suggest classes / methods from the cocos2d api.

Turns out I had only added the "user header search paths" to my target, but not in the project settings.
Adding it fixed the problem. I guess the reason it compiled fine was because I had added it to the target, but it didn't work in the text editor since I hadn't added it to the project.

This happened to me as well, even though I did add it to both project and target.
However, what worked for me was to select the RECURSIVE checkbox under "User Header Search Prefixes" (which was set to "/lib/**")

I had same issue.
My project -> Build Settings
Look for the entry for Search Paths
Always Search User Paths - YES
User Header Search paths - "myProjectName/libs" //I put box2d folder here
Hope this help someone.

Related

How to import obj c framework into swift bridging header?

I always find problem when importing framework headers into my project. For example, my little trial project on this screenshot. What did I do wrong when importing Facebook SDK headers? This is a very simple step and yet I can't find out what's wrong with it. I even follow the step by step closely like on the Facebook Developers site.
EDIT: sorry, maybe I wasn't clear. The bridging header has been set and I've set the bridging header location path on the build settings. That's why it produces error, because it's included in the compilation. The problem is that with the current setup of importing Facebook SDK and bridging header, I can't import the Facebook SDK into the bridging header.
Things I've tried:
#import "FBSDKCoreKit/FBSDKCoreKit.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import "FBSDKCoreKit.h"
#import <FBSDKCoreKit.h>
#import "FBSDKCoreKit.framework/FBSDKCoreKit.h"
#import <FBSDKCoreKit.framework/FBSDKCoreKit.h>
None of them works (all of them produces error like on the screenshot, means that the bridging-header is fine and included in the compilation, but it can't find the header I refer to).
Make a Obj C Bridging:File -> New -> Source -> Header File -> Name as AppName-Bridging-Header.
Add the following (ex. in case of SDWebImage):
#ifndef AppName_AppName_Bridging_Header_h
#define AppName_AppName_Bridging_Header_h
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIImageView+WebCache.h"
#endif
or
#import "UIImageView+WebCache.h"
Note: Build Settings, in Swift Compiler - Code Generation, make sure the Objective-C Bridging Header build setting under has a path to the bridging header file. - its like testSD/testSD-Bridging-Header.h or testSD-Bridging-Header.h (Open the Project folder and find the header file path)
#Chen : In your case, try Adding the “-ObjC” Linker Flag.
Select the project file from the project navigator on the far left side of the window.
Select the target for where you want to add the linker flag.
Select the “Build Settings” tab.Choose “All” to show all Build Settings.
Scroll down to the “Linking” section, and double-click to the right of where it says “Other Linking Flags”.
A box will appear, Click on the “+” button to add a new linker flag.
Type “-ObjC” (no quotes) and press enter.
New File -> iOS Source -> Objective-C File -> Enter whatever (Example "abc") name -> Next, Create -> Will show message on the top, choose Create Bridging Header -> Delete abc.h -> Click ProjectName-Bridging-Header.h -> import -> finish.
Now I've found the answer. The problem is that the framework doesn't stored inside the project folder but in download folder. And yeah, Facebook dev guide told me to drag the framework straight from where the framework is downloaded, and I didn't check the 'copy files if needed' checkbox when importing. From this answer, I found out that I need to fill in search path to the framework base folder in order for Xcode to be able to find it. Thanks for everyone for answering.

Xcode cannot find ProductModuleName-Swift.h

I'm attempting to import my "-Swift.h" file into one of my Objective-C .h files but xcode keeps telling me that the file doesn't exist
#import "Aesculus-Swift.h"
If I command click on the file name it will take me to the generated header file so I know it exists. Why is xcode not able to find it?
This seems like just another issue with Xcode and it's complex tool chain of static analysers and compilers.
Openradar lists radar://21362856 - Swift to Objective-C bridging is unreliable. I am sure there are more but I stopped looking after finding one for this example.
The author imarcelv notes in the description:
I asked a Swift engineer at WWDC in a lab and even he didn't know how to fix this issue.
Steps to Reproduce:
Add a ramdom Swift class to an Objective-C project
Add the #import "ModuleName-Swift.h" file that Xcode generates automatically
Try to use it or just try to compile the project
From time to time it simply doesn't work
It's probably best to file a radar on this issue as it seems that others are already calling it out.
One other thing you could try...
Historically, it was possible for Xcode to completely lose it's syntax highlighting and you could always find out what files the static analyser was giving up on by increasing log level of clang.
I'm not sure if it's still relevant but if I was in your position I'd be trying this command:
defaults write com.apple.dt.Xcode IDEIndexingClangInvocationLogLevel 3
This generates logs you can search with using Console.app for just xcode to highlight the messages. You'll want to trash the derived data of your project to force it to re-compile things.
Although not the same issue as what you're seeing, I have had this post on the syntax highlighting issue bookmarked for years for the above defaults write command to try in times like these.
I solved this recently by adding the following entry to my .xcconfig (you could add it in Xcode's Build Settings > User Header Search Paths if you prefer).
USER_HEADER_SEARCH_PATHS = $(BUILT_PRODUCTS_DIR)/MyFramework.framework/Headers
This tells the compiler to search for headers in the build output directory, which is where Xcode puts the generated header (at least in the case of this framework).
In my case this is a directory like ~/Library/Developer/Xcode/DerivedData/MyProject-LongCode/Build/Products/Debug-iphonesimulator/MyFramework.framework/Headers/MyFramework. You might find your generated header in there too.
Xcode's header and dependency management is a hot mess, and it's not surprising that it doesn't work for you.
I had trouble with this stuff & found that your -Swift file is the Product name of your Target ( not just the name of your Target ) . I found the details here helpful: http://ericasadun.com/2014/08/21/swift-calling-swift-functions-from-objective-c/
When you encounter such situation, just find your kinda "ProductName-Swift.h" file by just cmnd+click on it (even if xcode shows warning about it is not found, the #import "Aesculus-Swift.h" string is still clickable) and then in opened code editor window choose context menu and "Show in Finder" item, then explicitly add it to your project.

Xcode - Use header imports with folder-based paths

I'm using a static library and pointing Xcode to a folder with the headers in it. These headers are organized in a hierarchical folder structure:
headers:
- a.h
- b.h
- subheaders:
- c.h
- moreheaders:
- d.h
I also have some prewritten source code that uses this library, and it refers to the headers based on their locations: #import "subheaders/c.h".
However Xcode flattens the folder hierarchy, forcing me to use #import "c.h". There's a good deal of code, and I can't rewrite it very easily to stop using the foldered imports. Any way to make Xcode recognize the folder structure?
P.S. I'm including these headers using the "Library Search Paths" "Header Search Paths" build setting under my primary Target. The search is non-recursive, so I don't know how it even finds the nested headers...
Thanks for your help!
Xcode builds header maps by default and header maps are flat, except from modules/frameworks, where the map is ModuleName/Header.h. For details about how Xcode builds these maps, which settings exist and what the compiler does, if a file is not found in a map, please see this answer.
Setting an appropriate header search path (HEADER_SEARCH_PATHS or USER_HEADER_SEARCH_PATHS) will make path includes possible, regardless if header maps are used or not, yet the flattened import will still work as well in that case. If you don't want the flattened import to work, you need to disable header maps or ensure that the header files in question are not added to the maps, as shown in the other answer.
Unfortunately this was just a case of my not being observant. I had added the headers to the search path, but I had also earlier added them as source to the project (to see if I could get it to work that way). When I removed the source folder and kept the header search path I could reference files by their full paths. Thanks for the help.
after Hours of try and fail i found an option in XCode 9.4.1 in an XCode 9.3-compatible project the option to disable the usage of
Ues Header maps
This will let Xcode recognize the project structure.
i althought set every file and folders location to be project relative
I Hope there is someone with the same problem and has now an solution

Import Box2D : file not found

I'm trying to import Box2D library but I have a compilation error : 'Box2D/Box2D.h' file not found.
I tried lots of things to solve this problem but without any success. I'm not using cocos2d so I don't have any templates.
I just drag and drop Box2D folder in my Xcode project (tried to check and uncheck the 'copy to destination folder).
Tried to check 'Always Search User Path' etc ... No success.
If someone has the solution : god bless you !
Just drag and drop the Box2D.h/Bow2D.h to your project.If it is not as simple as that, I can recommend you this tutorial which explain everything you need to know to set up Box 2D in XCode.
EDIT
I have just tried the project in the tut I referenced and i believe you didn't set-up your header search path. Box2D files are intended to be included like this: #include so you need to go to Build Settings then add an entry for the Debug and Release key of the Header Search Paths (under Build Settings)
you are looking as something like this: Debug Box2D_v2.1.2/Box2D

Can't import newly added framework file

I have an existing framework (MyFramework) and I've added a new class to it, NewClass. In another project I am already using this framework and I now want to use NewClass as well. I use the following import statements:
#import <MyFramework/OldClass.h>
#import <MyFramework/NewClass.h>
When I compile the project, I get an error on the second import that says "MyFramework/NewClass.h: No such file or directory". NewClass.h is in the exact same location as OldClass.h, the framework compiles with no errors, I've made sure to clean out any old artifacts, and I've verified that in the framework's target I've included NewClass.h in the "Copy Headers" build phase.
What additional step is required to make a framework's header files available to users of the framework?
p.s. I realize that I should likely have a single MyFramework.h for anyone wanting to use the framework, but I'll tackle that once I've figured out how to add new headers!
Set role of the header to Public.
In Xcode 3 right click on the header, select Set Role from the menu.
In Xcode 4 select your target, open Build Phases and change the section of your header in the Copy Headers section from Project to Public.
Update:
In Xcode 4.5 select the header, open the File Inspector in the Utilities panel on the right, change the role to Public in the Target Membership section.