1

This is my first question and I feel like a fool not being able to find the answer since I strongly suspect something like this has already been answered, but my google-fu has failed me.

What I want to achieve is making a framework that I can add to my applications so I am able to access structs from every class I build. For example, CGRect can be accessed from any class that I build because (I think) every class includes the Foundation Framework, or when I create a view controller I am able to use UIButtons because they are located in the UIKit Framework, which subsequently pulls the Foundation Framework and the CoreGraphics Framework.

I want a file where I can stick structs or custom objects that I'll be interested in using over and over again. i.e. CGPoint from the CoreGraphics Framework:

struct CGPoint {
  CGFloat x;
  CGFloat y;
};
typedef struct CGPoint CGPoint;

I'd want a Framework where I could add stuff like a month typedef

typedef enum {
    Jan = 1,
    Feb = 2,
    Mar = 3,
    Apr = 4,
    May = 5,
    Jun = 6,
    Jul = 7,
    Aug = 8,
    Sep = 9,
    Oct = 10,
    Nov = 11,
    Dec = 12,
} SSMonth;

I think in C the way I'd accomplish this is by creating a header file and just including this header file in all of the other files which use the the above SSMonth example. Would it be better to achieve this goal a similar way for an iOS project, or would creating a Framework be the better method? Assuming I wanted to expand it beyond structs and typedefs and wanted to include actual classes; would the answer be the same?

Brad B.
  • 316
  • 2
  • 13
  • 1
    This seems like a heavy task. I'm sure there's a tool out there already. What kind of google-fu did you try? some Qs are just hard to ask in a way google will pull it up. (google image spears and get a pop star more than weapons...) – Plasmarob Dec 13 '13 at 23:50
  • If you want to build a static library, this [tutorial](http://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial) might be helpful (although possibly overkill) – Rich Tolley Dec 13 '13 at 23:51
  • Thank you very much for the link suggestion, Rich Tolley! At this stage it is overkill, but I've favorited that link so I can expand to that in the future. Plasmarob, I'm completely self taught when it comes to programming (know several languages!) so my google-fu is generally pretty good at finding stuff. Now that I know the key term I'm looking for "Static Libraries" I'm hoping that I'll be able to find some more tutorials and documentation. – Brad B. Dec 14 '13 at 00:07

2 Answers2

3

You can add the file into AppName-Prefix.pch file

//
//  Prefix header
//
//  The contents of this file are implicitly included at the beginning of every source file.
//

#import <Availability.h>

#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "CustomClass.h"
#endif

Then in CustomClass put the objects you want to be able to access.

Ríomhaire
  • 3,084
  • 4
  • 25
  • 40
  • 1
    Say I edit the AppName-Prefix.pch file to include BBStructs.h. Would I have to go back and edit all of the already existing classes I have to include BBStructs.h, or is that unnecessary because of the compilation order? – Brad B. Dec 14 '13 at 00:02
  • http://stackoverflow.com/questions/6462288/why-do-xcode-templates-have-imports-that-duplicate-prefix-pch – Ríomhaire Dec 14 '13 at 00:15
  • Riomhaire, you answered my question to herzbube below. Thanks! – Brad B. Dec 14 '13 at 00:19
3

I'd like to expand a bit on Riomhaire's answer: The file that he is talking about is a precompiled header file (the extension .pch is an acronym). Precompiled header files are processed by the compiler only once at the start of your project build, then they are imported automatically into every other file of your project, without you having to explicitly write

#import "Prefix.pch"

The main purpose of precompiled header files is that they speed up the build time of your project, because the compiler does not need to process the same - possibly large - header files over and over again. The fact that you don't have to import the .pch file explicitly everywhere is just a convenience provided to you by Xcode. In other programming environments (e.g. Microsoft Visual Studio) you may still need to explicitly include the precompiled header file.

Google for "precompiled header" to learn more about the concept (which is not limited to Objective-C). As a shortcut, here's the link to the Wikipedia article.

herzbube
  • 13,158
  • 9
  • 45
  • 87
  • Thank you very much, herzbube! This actually rasises a question. If it's a precompiled header file then why does Xcode import the same stuff the .pch file already has compiled when I create a new viewcontroller? i.e. The .pch file has #import and when I create a new view controller it ALSO has #import . Could you give some insight on the reason for the redundancy? – Brad B. Dec 14 '13 at 00:11
  • @BradB. To leave out the import statement, Xcode would have to check 1) whether your project has a precompiled header file (you are not required to have one); and 2) whether that file contains the `UIKit.h` import. It's much simpler to just add the import statement and then let you decide whether you want to remove that statement or not. You could say that probably it's a case of "better be safe than sorry" :-) – herzbube Dec 14 '13 at 00:17