iphone - 在iPhone应用程序中保留全局变量的最佳方式

标签 iphone objective-c global-variables

我是 iPhone 开发新手,所以想问一下,保留可由多个类访问的全局变量和常量的最佳方法是什么?

我应该将它们保留在应用程序委托(delegate)中还是有一个我不知道的更好的方法?

谢谢

最佳答案

将它们保留在应用程序委托(delegate)中是一种解决方案,尽管将所有内容都放入一个其目的实际上是响应与应用程序相关的事件的类中并不是特别优雅。

对于常量,您可以简单地创建头文件并使用#defineconst,然后在需要常量的地方包含头文件。

对于全局变量,您可以创建 singleton class带有静态变量。有很多宏可以为类合成单例。以下是来自 Mac 版 Google 工具箱的示例:

//
//  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)
///
#define GTMOBJECT_SINGLETON_BOILERPLATE(_object_name_, _shared_obj_name_) \
static _object_name_ *z##_shared_obj_name_ = nil;  \
+ (_object_name_ *)_shared_obj_name_ {             \
@synchronized(self) {                            \
if (z##_shared_obj_name_ == nil) {             \
/* Note that 'self' may not be the same as _object_name_ */                               \
/* first assignment done in allocWithZone but we must reassign in case init fails */      \
z##_shared_obj_name_ = [[self alloc] init];                                               \
_GTMDevAssert((z##_shared_obj_name_ != nil), @"didn't catch singleton allocation");       \
}                                              \
}                                                \
return z##_shared_obj_name_;                     \
}                                                  \
+ (id)allocWithZone:(NSZone *)zone {               \
@synchronized(self) {                            \
if (z##_shared_obj_name_ == nil) {             \
z##_shared_obj_name_ = [super allocWithZone:zone]; \
return z##_shared_obj_name_;                 \
}                                              \
}                                                \
\
/* We can't return the shared instance, because it's been init'd */ \
_GTMDevAssert(NO, @"use the singleton API, not alloc+init");        \
return nil;                                      \
}                                                  \
- (id)retain {                                     \
return self;                                     \
}                                                  \
- (NSUInteger)retainCount {                        \
return NSUIntegerMax;                            \
}                                                  \
- (void)release {                                  \
}                                                  \
- (id)autorelease {                                \
return self;                                     \
}                                                  \
- (id)copyWithZone:(NSZone *)zone {                \
return self;                                     \
}

关于iphone - 在iPhone应用程序中保留全局变量的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3456981/

相关文章:

Iphone OpenGl ES 1.1 : How to modify the camera center from (0, 0,0) 到 (x,y,z)?

iphone - 如何在 iOS5 中重新请求照片访问权限?

iphone - setKeepAliveTimeout 和 BackgroundTasks

objective-c - 在 Block 语句中分配/显示 UIAlertView

JavaScript如何将Fetch数据存储到全局变量

iphone - iOS 5 是否以不同方式处理低内存情况?

iphone - 后台运行

ios - 永久设置 UITableView 内容插入

Python 只为函数内部的函数共享全局变量

c - 回调如何工作和全局数据同步