Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top