Saving them as an application delegate is one solution, although it is not particularly elegant to insert everything into a class whose purpose is to really respond to events related to the application.
For constants, you can simply create header files and use #define
or const
, and then include header files wherever you need constants.
For global variables, you can create a singleton class with static
variables. There are many macros that can synthesize singletones for classes. Here is an example from Google Toolbox for Mac:
// // GTMObjectSingleton.h // Macro to implement methods for a singleton // // Copyright 2005-2008 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy // of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations under // the License. // #define _GTMDevAssert(condition, ...) \ do { \ if (!(condition)) { \ [[NSAssertionHandler currentHandler] \ handleFailureInFunction:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \ file:[NSString stringWithUTF8String:__FILE__] \ lineNumber:__LINE__ \ description:__VA_ARGS__]; \ } \ } while(0) /// This macro implements the various methods needed to make a safe singleton. // /// This Singleton pattern was taken from: /// http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html /// /// Sample usage: /// /// GTMOBJECT_SINGLETON_BOILERPLATE(SomeUsefulManager, sharedSomeUsefulManager) /// (with no trailing semicolon) ///
source share