I'm currently trying to add LARSAdController to my iOS project with no success.
As soon as i import the files via #import "LARSAdController.h" in my AppDelegate.h the build process fails and on every occurance of (Class)class in LARSAdController.h i get the cryptic error "Expected identifier". BTW I'm using cocoapods.
Example:
- (void)registerAdClass:(Class)class;
which seems fine to me...
If i create a blank project and import the files they compile, so the problem must be in some relation to my code. Anyone got an idea what may cause this?
Thanks for any help in advance!
class is a reserved word in C++, so I would imagine that some of your project uses Objective-C++.
To solve this, use #import LARSAdController.h in Objective-C implementation files only, and remove its use from header files. You can use #class to forward-declare any occurrences of whatever classes are defined in LARSAdController.h in header files (this is best-practise anyway).
If you need to use LARSAdController from an Objective-C++ class then this is more complicated and you will need to use an Objective-C proxy object or modify their header files (which isn't ideal).
Related
I'm trying to keep my code as readable as it possible by keeping methods and files as short as I can and using nested classes for namespacing. It works fine except some really strange moment.
I have some class used for namespacing.
class Space { }
All classes used within that one are implemented in their own files as extensions.
extension Space {
class SomeClass {
// implementation
}
}
One of those SomeClasses have a number of quite sophisticated initialisers, so I have split them up to their own files as well and implemented it as follows:
extension Space.SomeClass {
convenience init(fromSomeSource source: SourceClass) {
self.init()
// other implementation
}
}
The problem is that some of those files works just fine, but some of them throwing 'SomeClass' is not a member type of 'Space' and I don't know why.
All of them are pretty similar. The only difference is implementation of an initialiser itself. All files are held in the same place and I have no idea why some of them works fine and some not.
I tried to move code from not working files into files that works fine and that works – Xcode agrees to see the code and said nothing against it. But when the very same code lies in its own file – Xcode or compiler doesn't want to understand that SomeClass is really a member of Space.
I tried to clean the build, including manual dumping of ~/Library/Developer/Xcode/DerivedData folder. Nothing helps.
Surely I can put it all in a single file and it will work fine, but what the reason why it so picky in my case?
I've tried to create a new file and move there all contents from one of the bad ones. It works, but only with certain file names. Some names gives the same error again, but it seems that if name is totally new and not similar to any of the existing ones - it works. Magic?
I've encountered similar issue, it seems like the complier is trying to process the file where you extend the nested class before the one where it's defined. Therefore you have this error saying that that Space has no member SomeClass.
The solution I've found is to go to your target settings, open Build Phases.
There, in Compile Sources section you should put the file where you define the nested class above files where you extend it.
This solution seems to even play well with your observation that when you recreate the file it sometimes compiles, because when you recreate the file its position in Compile Sources changes.
I am trying to setup Unit Testing for my project.
It is an existing Objective-C app, that I have recently added one Swift class to. I have setup the 'MyProject-Swift.h' and Swift Bridging files (both 'MyProject' and 'MyProjectTest') and I am able to build and run the app just fine using both Objective-C and Swift code.
However, now I want to run some Unit Tests on the new Swift class.
I setup my test file and it looks like the following:
MySwiftClassTests.swift:
import UIKit
import XCTest
import MyProject
class MySwiftClassTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
XCTAssert(true, "Pass")
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
// Put the code you want to measure the time of here.
}
}
}
I get this error when running the app as Test:
'MyProject-Swift.h' file not found
I am not sure why this happens only when trying to run the Tests.
Any suggestions?
"MyProject-Swift.h" file is generated at following path:
"$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources"
I end up adding this to Header Search Paths for my Unit Test target.
Also as #hyouuu pointed out about being the known issue, hopefully Apple will provide some good solution at their end. Until I believe we need to use this above solution.
https://developer.apple.com/library/content/documentation/Xcode/Conceptual/RN-Xcode-Archive/Chapters/xc6_release_notes.html
Thanks to #gagarwal for figuring this out. In our case the product name has a space, which is collapsed in $PROJECT_NAME, so I had to hard code it. Additionally, by using $CONFIGURATION_TEMP_DIR instead of $TARGET_TEMP_DIR, you can remove the parent directory (../) from the path. So the solution is to add the following to the Header Search Paths in your test target:
"$(CONFIGURATION_TEMP_DIR)/Product Name With Spaces.build/DerivedSources"
Or, if your product does not contain spaces:
"$(CONFIGURATION_TEMP_DIR)/$(PROJECT_NAME).build/DerivedSources"
Saw in the Xcode 6.1 release note, that this is a known issue... sign...
Search for "-swift.h" in the release note https://developer.apple.com/library/content/documentation/Xcode/Conceptual/RN-Xcode-Archive/Chapters/xc6_release_notes.html
Tests written in Objective-C cannot import the Swift generated interfaces header ($(PRODUCT_MODULE_NAME)-Swift.h) for application targets, and therefore cannot be used to test code that requires this header.
Tests for Swift code should be written in Swift. Tests written in Objective-C for framework targets can access the Swift generated interfaces by importing the framework module using #import FrameworkName;. (16931027)
Please see #gagarwal's workaround below which WORKS!
I had a similar issue to yours, I think; here was my setup.
I had an object defined in Swift:
// file Foo.swift
#objc public class Foo {
// ...
}
This class was then used in the initializer of an Objective-C object:
// file Bar.h
#import "MyProject-Swift.h"
#interface Bar: NSObject
- (instancetype)initWithFoo:(Foo *)foo;
#end
This made my unit tests for Bar not compile, since the MyProject-Swift.h header isn't real and the unit test target can't see it. The release note shared by #hyouuu is on point - but I'm not testing a Swift class, I'm testing an Objective-C class!
I was able to fix this by changing the header file for Bar to use a forward class reference instead:
// file Bar.h
#class Foo;
#interface Bar: NSObject
- (instancetype)initWithFoo:(Foo *)foo;
#end
I then included MyProject-Swift.h in Bar.m, and everything worked - my tests of Objective-C objects written in Objective-C compiled properly and continued running, and I could write new tests for Swift objects in Swift.
Hope this helps!
After I tried out everything I could find on the topic, the thing that worked for me was actually running the app although it was still showing the 'ModuleName-Swift.h file not found' error.
It went away and my app works perfectly fine. I guess I should have considered that earlier... The error keeps coming back, but after running the app it always just goes away again. So the issue is not really solved for me, but I can continue working on other topics for now...
A simple
#testable import MyProject
has done the job for me.
Strangely, I was seeing this same error, but only when targeting a device (not the simulator). Before running the test I would see the red exclamation point next to the import statement for "MyProjectNameTests-Swift.h".
However, funny thing is, if I just go ahead and run the test anyway (despite this apparent build error), then during the build phase that happens thereafter, XCode actually does generate the "MyProjectNameTests-Swift.h" file, and the test runs just fine!
So, at least in my case, there was no need for the other solutions here, evidently, although I believe they do work also.
I should also note that I deleted my DerivedData directory prior to this, so maybe that is a step also worth trying.
On one of my clients project they added frameworks directly in the project. I solved the errors like this;
Command 1
option command J
now type the framework name (that gives the error) in the filter
select the framework
option command 1
in Target Membership select your *UITests target
mySwiftClassTests (and any other swift classes you want to use in objective-c) needs to be marked #objc:
#objc class MySwiftClassTests: XCTestCase
I couldn't get it to work by adding that filepath mentioned by other answers, but I realized the file where it was complaining wasn't even being tested. I just had to remove it from the test target using the Right Utilities Side Bar.
Adding a .swift file to that target fixes issue on it.
Something strange is going on with my Xcode 5. All of a sudden I'm getting Undeclared Identifier errors for all the values in my Constants.h file, which is imported in my Prefix.pch file.
Two things are weird here:
This hasn't happened before.
When I do build and run, the build succeeds and the app runs with no problems.
I tried restarting Xcode and the simulator, and even restarting the whole machine. No luck.
What's going on? How can I get rid of these false errors?
EDIT following rmaddy's request. The error is Use of undeclared identifier kOffsetFromTop (for example, there are other similar errors with different constants.)
I don't really want to post my entire constants file, but the constant in question is defined like this:
static int const kOffsetFromTop = 20;
When this happens I normally do the following
Comment out the import from the .pch
Clean all ⌘⌥⇧K
Delete derived data
Build
Then uncomment the import from the .pch and build again. I'm not sure which step is actually sorting the issue but this normally gets me going again.
Multiple points here :
You have a warning, not an error. A warning is just that, it warns you but does not prevent the code to compile. That is just to warning you that something is odd or unexpected, or to tell you that what you wrote may not be actually what you were intended, because the compiler finds it odd or not standard.
You didn't have the warning before probably because it wasn't activated by default in previous versions of Xcode. The latest version of Xcode5 activates more warnings (which is a good thing, as it warns you about more things that could go wrong in your code and encourage you to fix them), hence this new one you have but didn't have before.
As I understand what you describe, tour usage of a constant is incorrect (and that's probably why Xcode emits a warning).
The correct way to declare a constant that you want to be accessible from multiple files is to:
Declare it (define its existence and type) in a header (or in your pch) like this: extern <type> const <name>;
And define it (give it a value) only in one implementation file (like your main.m, your AppDelegate.m, some Constants.m file, whatever) like this: <type> const <name> = <value>
Some details and reminders about constants declaration (also valid in standard C)
You use static <type> const <name> = <value>; in an implementation file only, when the constant is local to the file and does not need to be used by other files. In that case, you declare it typically in the .m file in which you will use it, and other files won't have access to it (which is quite what the static keyword means, actually (making the constant attached/local to the file).
In that matter, you should never declare a constant that way in a header file (and especially not in your .pch file), because header (and pre-compiled header) files will be included multiple times. If you do that, this would declare as many independent constants as the number of implementation files you include your headers into (this has evil side effects especially for pointers/objects, for exemple declaring an NSString* const that way -- for, say, using it as a notification name of error domain -- will create multiple string constants, with the same value but different addresses, which will probably not behave like you will expect)
When you need to declare a constant that you need to use / be accessible from multiple implementation files, so declaring this constant in a header file so that it is known to all implementation fiels that includes this header, you need to only declare it in the header, and not making it static (at this would run against the purpose of having the same constant for all files instead of multiple independent instances of the constant) but instead indicate extern to let the compiler know that its definition (value) is set elsewhere. Hence the solution given above.
There are two situations, that I am aware of, that cause the following errors:
ld: warning: instance method 'resetAudioSystem' in category from /opentok-ios-sdk/Opentok.framework/Opentok(OTPublisher+AudioSnoop.o) conflicts with same method from another category
ld: warning: instance method 'attachAudioSnoopBlock:' in category from /opentok-ios-sdk/Opentok.framework/Opentok(OTPublisher+AudioSnoop.o) conflicts with same method from another category
ld: warning: instance method 'setVideoSnoopDelegate:' in category from /opentok-ios-sdk/Opentok.framework/Opentok(OTPublisher+VideoSnoop.o) conflicts with same method from another category
Possibility 1: A category instance method is declared twice. (Described here: SO)
But, When I do a search of any of the 3 instance methods (in Xcode or Grep) I do not find it declared twice.
Possibility 2: Accidentally importing a .m file. (Described here: SO)
But, I checked all my imports and I only import the framework once in the whole project. Also, only shows up once in Build Phases.
My program runs without crashing, presumably because the last category definition added is used and it happens to be correct. (Source)
Question 1: Is there a way to tell, prior to its addition, where the two categories are located?
Question 2: Any other ideas about how to resolve this?
Note: The Opentok Framework is a binary
Occurs in :
xcode 4.6 & xcode 5
iOS6 & iOS7
I had this warnings because I accidentally imported the implementation file of a category instead of it's header file. So:
wrong: #import 'MyClass+MyCategory.m'
right: #import 'MyClass+MyCategory.h'
I had this error, because I literally pasted my methods' implementation to header file (*.h). It worked well however; the only symptom - warnings.
Check whether your implementation is in correct (*.m) file.
This look like a linker bug. Maybe the SDK got linked more than once, or -ObjC is missing/present where it should not be. Check linker flags. Try deleting all OpenTok frameworks, make sure its missing in your projects, then re-download it and install again.
In my case the header file containing the categories was imported twice for a class.
It wasn't obvious because another header file also imported the same header.
In other words
//Class_with_warnings.h:
#import "Offending_Header.h" // import #1
#import "Class_A.h" // import #2
//Whereas Class_A.h:
#import "Offending_Header.h" // import #2
I got this error when I press build+debug:
ld: duplicate symbol .objc_class_name_BlogTableItemCell in /Users/fabian/Development/Workspaces/iphone_experiments/xcode_build_output/MausLog.build/Debug-iphonesimulator/MausLog.build/Objects-normal/i386/BlogTableItemCell-3733583914888A7B.o and /Users/fabian/Development/Workspaces/iphone_experiments/xcode_build_output/MausLog.build/Debug-iphonesimulator/MausLog.build/Objects-normal/i386/BlogTableItemCell-3733583914888A7B.o
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
You could also get this error if you mistakenly let Xcode's auto-complete for #import statements specify the '.m" file for the 'duplicate' class instead of the '.h'.
It seems that you are compiling the same BlogTableItemCell class two times in different places of your code. This may happen in the following cases.
You have put the same class
implementation into two different
files;
You actually have just one
implementation of this class, however
you are also linking in your project a framework
or library containing a class whose
name is exactly the same of yours.
Try finding in the whole project your class and make sure only one copy is available within your project.
For me, changing 'No Common Blocks' from Yes to No ( under Targets->Build Settings->Apple LLVM - Code Generation )
I had a similar problem due to poor defining of consts.
I had defined a const in my header:
int const kCropLocationTop = 1;
This was presumably imported multiple times. To fix i changed the header def as follows:
extern int const kCropLocationTop;
and moved the assigning of the const to the .m file:
int const kCropLocationTop = 1;
Hope that helps anyone who's as ignorant of simple objective c concepts as I am!
iPhone: Duplicate Symbol Error? by user576924
answered it correctly for me. However to find the offending gremlin this ZSH snippet.
grep "import.*\.m" **/*.[hm]
Will immediately tell you where your error is.
By mistake the source file was included twice in the Project -> Build Phase -> Compile Sources. Removing one of them solved the problem.
The most common reason for this error is importing an xyz.m file instead of the xyz.h file.
Check if your imports contain something like
#import "----.m"
Just to add; Using Xcode to generate subclassed managed objects (Core Data) can sometimes duplicate the generated files. For me the fix was to delete the generated files and re-generate them.
I just ran into this problem myself. For the list, here's another possibility:
Duplicated linking line in the project file.
I caused this merging conflicts on a SVN update, when I accidentally duplicated a line.
It happened to me, too. In my case, one (just one) of my core data automatically generated classes was inserted twice. I spotted the duplication by looking at Build Phases...Compile Sources. Simply deleting one of the occurrences solved the problem.
Adding another possible cause to the list... you may have mistakingly created multiple constants in the implementation file, but outside of the implementation, with the same name.
In HeaderFileOne.m
NSString * const kCoolConstant = #"cool";
In HeaderFileTwo.m
NSString * const kCoolConstant = #"cool";
So changing one of those constant names would fix the compile error.
This may help someone
I got this error because I duplicate a ViewController and then renamed it. So when I compile I got this error. The reason was in both of the view controllers there was a "float" variable with same name i.e "float padding=10.0" which I had defined on class level.
Renaming the name of the above mentioned variable in One of the view controllers solved my problem.
I also faced to this problem. My solution was rename one of global variable, which has the same name as one in other class. Hope this helps
The same thing happened to me while I was playing with localizable xib files, accidentally I have created two implementation files and appereantly that caused the problem in my case. After deleting / recreating the implementation file without doing the same mistake, the error was fixed.
One of our developers left the "libSoomla*" project files in there twice. I removed the duplicate soomla files, re-built, and that fixed it!
Hope it helps.
In may case, I followed some instructions to build a newer version of Subversion which directed me to create this symbolic link:
ln -s /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/ /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain
Now I'm really a Windows guy so it wasn't immediately obvious to me - but removing the link fixed it for me (after restarting XCode):
rm /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain
Phew.
(The actual error I got was the one described here:
build error duplicate symbols arclite.o)
Make sure that you didn't import .m File . For me this happen I added #import "SchoolCommuterHome.m" instead of #import "SchoolCommuterHome.h"