我是iPhone開發新手,所以想問一下,保存全局變量和常量可以被許多類訪問的最佳方法是什麼?在iPhone應用程序中保留全局變量的最佳方法
我應該讓他們在應用程序委託或有一個更好的方式,我不知道?
感謝
我是iPhone開發新手,所以想問一下,保存全局變量和常量可以被許多類訪問的最佳方法是什麼?在iPhone應用程序中保留全局變量的最佳方法
我應該讓他們在應用程序委託或有一個更好的方式,我不知道?
感謝
在應用程序委託保持他們是一個解決方案,但它不是特別優雅的一切推到一個類,其目的是真正對相關的應用程序響應事件。
對於常量,您可以簡單地創建頭文件並使用#define
或const
,然後在需要常量的地方包含頭文件。
對於全局變量,可以使用static
變量創建singleton class。有很多宏可以合成類的單例。下面是來自谷歌工具箱爲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)
///
#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; \
}
如果你喜歡的品種,這裏的一個又一個,這一個從CocoaWithLove - 他在談論爲什麼全局變量應該嚇唬你here;可能是一個好閱讀。
//
// SynthesizeSingleton.h
// CocoaWithLove
//
// Created by Matt Gallagher on 20/10/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file, free of charge, in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
\
static classname *shared##classname = nil; \
\
+ (classname *)shared##classname \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [[self alloc] init]; \
} \
} \
\
return shared##classname; \
} \
\
+ (id)allocWithZone:(NSZone *)zone \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [super allocWithZone:zone]; \
return shared##classname; \
} \
} \
\
return nil; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return self; \
} \
\
- (id)retain \
{ \
return self; \
} \
\
- (NSUInteger)retainCount \
{ \
return NSUIntegerMax; \
} \
\
- (void)release \
{ \
} \
\
- (id)autorelease \
{ \
return self; \
}
通常我會創建一個頭文件Resources.h,並保留我的所有定義。使用靜態變量也是一個好主意。
以及如何在那裏定義常量?喜歡這個? #define MY_CONST @「my constant」 – Burjua 2010-08-11 14:36:45