In my past applications I have been #importing into my *.h files where needed. I have not really thought much about this before as I have not had any problems, but today I spotted something that got me to thinking that maybe I should be #import-ing into my .m files and using #class where needed in the headers (.h) Can anyone shine any light on the way its supposed to be done or best practice?
gary
In any source file, import only what you need to make that individual file valid for compilation. #class is also preferable to importing another class's headers, because the less you load the less you compile.
As a rule of thumb it is fine to use #class in your header file and an #import in your .m files. You'll get an error if you do it wrong from the compiler :)
Basically, if you are only making reference to the class you want to use, but not any specifics of the class then #class is all that is required. It tells the compiler "I'm going to be using this class here - you don't need to know much about it, other than that it is valid". (The compiler then knows to reserve a pointer for it).
If you were going to be referencing any properties/methods within the class, the compiler would start complaining (as it wouldn't have enough information about the class) so in those cases it wants you to import the file (#import xxx) in order to provide the class specifics to the compiler.
Hope this helps
Related
I have this code:
#import "vars.less";
// Desktop/Laptop Section
#import "mixins-d-1.less";
#import "positioning.less";
#import "containers-d-1.less";
Inside containers-d-1.less I reference a mixin from mixins-d-1.less. However, I get an error saying that it's undefined.
I'm able to pull my variables out of vars.less, why can't I pull my mixins out of mixins-d-1.less?
The problem wasn't the importing at all, really.
I made a mistake in how I was using media queries.
Both files had the same media query since they were both for the desktop CSS (hence the "d"). However, the second one, in containers-d-1.less was OVERWRITING the first one and blowing away all my mixins! I clearly need to wrap the import block in a single media query for that section instead of redeclaring it inside each less file.
I am trying to add FBConnect to my application which includes the SBJson framework. However, when I try to compile the project I get these two errors:
Duplicate interface definition for class 'SBJsonWriter'
Duplicate interface definition for class 'SBJsonParser'
What can I do to fix this error? Thanks for any help.
Delete
#import FacebookSDK/FacebookSDK.h
In your project
I start using FacebookSDK, but then I was disappointed with it's current state and tried to use the old "FBConnect", that's how I have got the error
There are two possibilities:
you have two interfaces with the same name. Use Xcode's find in project menu option to find instances of SBJsonWriter. Then rename one of the interfaces
somehow you have managed to import the .h file twice. Check to make sure you always use #import and not #include.
A bit more info on #import/#include:
include blindly includes the file at the location of the #include statement. This means that if you #include a file twice in your .m you will get two copies of the file. Almost all traditional C #include files have something like the following bracketing all the content:
// some_file.h
#if !defined SOME_FILE_H
#define SOME_FILE_H
// entire content of #include file
#endif
he above is sometimes referrwed to as an include guard macro.
In Objective-C, if you #import a file, a check is performed by the compiler to make sure it has not already been imported. Consequently the guards are usually omitted. So if you #include a file that was supposed to be #imported, neither check will be done and you will sometimes get duplicate definitions.
How can I suppress this compiler warning:
Meta method 'prefix' in category from '...soap+prefix.o' conflicts with same method from another category ?
here is the category soap+Prefix.h:
#interface Soap (Prefix)
+(NSString*)prefix;
#end
and soap+prefix.m:
#import "Soap.h"
#import "Soap+Prefix.h"
#implementation Soap (Prefix)
+(NSString*)prefix { return #"EInspector"; }
#end
and these two files by the way are automatically generated with SudZc wrapper for web services.
p.s. this warning is issued ONLY in XCode 4.4
thank you so much in advance.
This happened to me when I have accidentally imported an implementation file of a category (.m) instead a header file (.h).
Somewhere else in your project, +[Soap prefix] is being declared in a category. Try searching your project for other declarations of +prefix.
Another possibility is that during a large refactoring or complex merge of your project.pbxproj file, the project ended up with two references or copies of the same file, and both are being compiled. I've seen it happen, and these sorts of warnings or errors are usually what happens. Try searching in the Finder for other files with the same name to see if you have a duplicate file somewhere.
In my case the issue was due to a wrong merge of project.pbxproj (so similar to Nick Forge's case), which caused too many references to the same file (however the file was physically only once on disk). When I removed redundant references the warning stopped appearing.
You should reference a great answer by someone who also posted something similar:
Is there a way to suppress warnings in Xcode?
In my opinion the 2nd (highest voted) option is the best!
I've got a C++ class I want to use which has all the code in the header file, rather than the CPP file. I'm trying to call it from an objective-C file which inherits a UIViewController class. I've renamed the file to .mm and imported the header file for the C++ file. When I compile, I keep getting a compile-time error when I try and access a method from the C++ class saying Request for member '<method>' in '<objectName>' which is of non-class type '<C++ class name>'. I did a search and it seemed that the header was usually the issue, but I've included the header in my file. What else could it be? (I can include generic code if required, but the I'm not sure if I'm allowed to show the actual code since it belongs to a third party).
The problem is likely in the declaration of the object that gives you the error, not in the header file. Sometimes the problem is hard to spot, you'll have to share some code if you can't figure it out by yourself.
I'm not a C++ coder, so this was probably an obvious mistake, but if anyone else comes across a similar problem, I simply changed my code from:
myObject.method();
to
myObject->method();
I'm working on an app that uses a web service. This is a private service so I cannot post the WSDL or generated code here so this is more of a question for some general advice rather than specifics.
I fed the WSDL into Sudz-c. The WSDL originally had some imports of XSD's that sudz-c would overlook so I copied these into the types section of the WSDL.
I use the generated code to make a request to the service but I'm finding that the request is missing a name space for the complex type objects I copied into the types section of the WSDL.
Again I know this is very general information but if anyone else has had a similar issue with Sudz it would be great to get your advice.
I know the thread has ended, but I found solution to this problem..
If a WSDL has external XSDs included/imported then YES, you are right to have those XSDs types copied over into WSDL (i.e. Directly EMBED it into WSDL rather than including/importing).. The generated Code is almost perfect in any sense since it generates types for XSD types as well.. but you might receive an ERROR (same error in multiple files).. which will be a missing BASE-Class of few request/response types.. after analyzing the code I realized that that missing namespace is nothing but the same SOAPObject object so I replaced that missing namespace with SOAPObject.h and used SOAPObject interface/class as the base class.
e.g. in my case
#include "SOAP.h"
#include "sudz.h"
#class sudz;
#interface sudzAbstractRequestType : sudz
{
}
(Note: 'sudz' is the unique name that you use when generate code using SUDZ-C website/project, in your case it can be different).
I changed the code to (everywhere in the project where this error was encountered):
#include "SOAP.h"
#include "SOAPObject.h"
#class SOAPObject;
//#include "sudz.h"
//#class sudz;
#interface sudzAbstractRequestType : SOAPObject//sudz
{
}
I hope this will help others... I was stuck for days but later got it working OK..
Happy Coding :)
One thing you might try is to make sure you fix your namespace in the actual service itself.
here is a link help get that done:
http://alensiljak.blogspot.com/2009/06/removing-httptempuriorg-namespace-from.html
Couldn't find an answer to this so ended up just hand coding the web service messages :(