Should header files be included in .h or .m files? - xcode

If I have four .h/.m file pairs
file1.h
file2.h
file3.h
file4.h
where I am using file1.h, file2.h, and file3.h functionality in file4, should those header files be included in file4.h or file4.m?

In the .m file if possible. You may have to forward declare classes in the .h file, like:
#class AClassIImportInDotMFile;
If Class B is a subclass of Class A, you need to import Class A's .h file in Class B's .h file.

I assume file4.h is going to be used by other .m files. If so, only include in file4.h what is used in file4.h, and the rest in file4.cpp. This will reduce the overal number of inclusions and including file4.h will not pull useless additional headers.
When the number of required .h files starts to grow inconveniently, you can group them in topic headers. For instance, topic1.h may include file1.h and file2.h but not file3.h.

I Usually import the other headers into the current header file if I plan on using their classes in property declaration. However this can create some havoc if the classes reference each other.
If this is the case then you will want to include the headers in the .m file so they can be used inside the class, but for any external access to these items you would use id in the place of your custom classes. And cast them when you get them in the other referencing methods.
I run into this issue if I include the AppDelegate.h and then reference the other class in app delegate.
the reason for this would be that I want to include a reference to my current object in the app delegate, but the class also uses the app delegate to access other classes. In this case I would include the Classes in the AppDelegate.h and include AppDelegate.h in the m file of the class.
I hope that made sense.

Related

Custom message to be used in TracIDemo11p in OMNeT++, Veins project

In a project, I have to use a custom beacon message. The parameters it require, differ from the parameters that are in WaveShortMessage. I defined .msg file and was able to generate _m.cc and _m.h files. To use custom message in TraCIDemo11p, I thought of replacing WSM object parameter with that of custom messages's object parameter in onBeacon() function. But that won't work as BaseWaveApplLayer too have used WSM object only.
So I created another MyAppLayer.ned, whose content was same as that of TraCIDemo11p.ned, except "sendWhileParking" parameter (which I removed in MyAppLayer.ned) and I wrote corresponding .h and .cc files for MyAppLayer.ned by looking at the structure of TraCIDemo11p.h/.cc files. Also, I carefully included all the relevant header files, including custom message's _m.h file. But it has generated lot of errors like-
scheduleAt() not defined in the scope
"MyMsg" is not defined in the scope
.
.
.
and similar.
Please help me with how to use custom message in TraCIDemo11p in place of WSM. Thank you.
first of all, you should add following code into your file
#include omnetpp.h
and
using namespace omnetpp;
Finally you should inherit cSimpleModule class in order to recognize your methods such as scheduleAt().
For example:
class className : cSimpleModule{
...
}

Importing files in the header

I was wondering if I could import a header file, but not include any of that header file's included headers.
Suppose I have Class A that imports Class B. In Class B, I import Class C. Is there any way I can hide Class C from Class A?
No, you cannot do that: once you import a file, all its imports come in as well.
However, if you want to use only ClassA, you can forward-declare it in your own header, instead of importing ClassA's header:
#class ClassA;
Now you can make variables of type ClassA*, use ClassA* as return type or a parameter type, etc. At the same time, dependencies of ClassA's header will not be loaded.
In general, it is a good idea to reduce the number of headers that you import inside your header, for example by moving imports related to implementation (rather than the interface) into the .m file, and using class extensions.

Can I override Magento Varien classes?

I know how to override Mage classes (any class within app/code/core)
My question is how to override Varien classes? (classes within lib/Varien)
If I want to override Mage_Adminhtml_Block_Page_Menu
I create a class MyCompany_Adminhtml_Block_Page_Menu under app/code/local/MyCompany/Adminhtml/Block/Page/Menu.php
I name it like:
class MyCompany_Adminhtml_Block_Page_Menu extends Mage_Adminhtml_Block_Page_Menu
Now Magento uses my class MyCompany_Adminhtml_Block_Page_Menu instead of Mage_Adminhtml_Block_Page_Menu
My question is: where should I put the new class, and how to name it, to override, for example Varien_Date (lib/Varien/Date.php)
Thanks
If you must, copy the file and path to the local codepool and make the necessary changes. There is no configuration mapping to change the class name.
Explanation: see the bootstrapping in app/Mage.php. There is a load order set for the codepools and libraries in the following order:
app/code/local/
app/code/community/
app/code/core/
lib/
Typically, Varien_Autoload is responsible for mapping classnames such as Varien_Data_Collection_Db, Mage_Core_Model_Abstract, or Zend_Db_Select to relative filenames (Varien/Data/Collection/Db.php, Mage/Core/Model/Abstract.php, and Zend/Db/Select.php respectively). These file locations are then passed to include(), which internally uses the load order set in the bootstrap. Therefore, if the file Varien/Data/Collection/Db.php is present under one of the "earlier" locations, that version of the class definition will be used.
These type of modifications may be justified, but they should be well-considered and documented, as the entire definition will be owned by you and any upgrades will need to be merged in the future. It would be good to know what you would like to change, as someone else may have a slightly less invasive option.

How to declare methods in objective c outside the interface?

When a method is declared in .h file is detected by intelisense and the warnings are not raised, when the method is used in .m file.
When a method is declared only in .m file, the intelisense doesn't detect it if is declared below the method where is being used.
To avoid the warnings there is a flag in xcode, but I prefer don't disable it.
There is any way to declare the methods in .m in order to be detected by intelisense and without the warning?
Thanks.
Two ways to fix it:
Either: Use a class extension to declare private methods at the top of the .m file:
#interface Foo ()
- (void)privateMethod;
#end
Or: Upgrade to Xcode 4.3.1, which contains a more recent version of clang. This newer version of the compiler does not need previously declared methods to call them in the same compilation unit.
Class extensions are still good for compatibility or to declare private properties, though.
You can use a category to declare additional methods on a class.
For instance, adding this at the top of your .m file:
#interface MyClass (PrivateCategory)
-(void)foo;
-(void)bar;
#end
will let Xcode know that MyClass additionally responds to foo and bar. The (PrivateCategory) tells the compiler that you're adding methods that should be "grouped" under the category PrivateCategory. You can pick whatever name you want, or even no name at all (although an "anonymous category" has slightly different semantics).

Creating a NSData object from an included file

I have an application which creates strict folder structures.
I also have some template files which should be placed in specific folders within that structure. My problem is that I'm not sure how to point to those files (which I've included in the project) so that I can create an NSData object from them. The plan was then to call the createFileAtPath:contents:attributes: method of NSFileManager to save them to the folders.
How can I reference these included files? I tried using the NSData dataWithContentsOfFile: method and just setting the name of the file like so:
[NSData dataWithContentsOfFile:#"Storyboard.template"];
but that does not appear to work.
Is there perhaps a better way of doing this?
If by, "I've included in the project", you mean you have these files in your bundle, then you should use the NSBundle method, pathForResource:ofType:, or one of its siblings to get the path.

Resources