xcode 使用单例创建可由多个视图访问的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6324732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a singleton to create an array accessible by multiple views
提问by Eric Brotto
It's a classic problem.
这是一个经典的问题。
I would like to access an array of objects from anywhere within my app. I would also like to do this using a singleton. My questions are:
我想从我的应用程序中的任何地方访问一组对象。我也想使用单例来做到这一点。我的问题是:
- Where do I instantiate my singleton object?
- Where do I instantiate my NSMutable array of objects?
- How do I refer to this array from anywhere within my project?
- 我在哪里实例化我的单例对象?
- 我在哪里实例化我的 NSMutable 对象数组?
- 我如何从我的项目中的任何地方引用这个数组?
All code and examples are greatly appreciated!
非常感谢所有代码和示例!
EDIT 1
编辑 1
This is what I have so far. I can't figure out though how to access this array of bananas correctly and consistently:
这就是我迄今为止所拥有的。我不知道如何正确且一致地访问这一系列香蕉:
#import <Foundation/Foundation.h>
@interface Singleton : NSObject {
NSMutableArray *bananas;
}
@property (nonatomic, retain) NSMutableArray *bananas;
@end
#import "Singleton.h"
static Singleton *mySingleton;
@implementation Singleton
@synthesize bananas;
#pragma mark SingletonDescption stuff
+ (Singleton *)mySingleton
{
if(!mySingleton){
mySingleton = [[Singleton alloc]init];
}
return mySingleton;
}
+ (id)allocWithZone:(NSZone *)zone
{
if (!mySingleton) {
mySingleton = [super allocWithZone:zone];
return mySingleton;
} else {
return nil;
}
}
- (id)copyWithZone:(NSZone*) zone
{
return self;
}
- (void)release
{
// NO OP
}
@end
EDIT 2
编辑 2
This is how I'm trying to use my singleton object to have an array of objects placed in a table cell. Nothing is happening and the table cell comes up blank :(
这就是我尝试使用我的单例对象将一组对象放置在表格单元格中的方式。什么都没有发生,表格单元格变成空白:(
- (id)init
{
[super initWithStyle:UITableViewStylePlain];
// bananas = [[NSMutableArray alloc] init];
Singleton *mySingleton = [[Singleton alloc]init];
mySingleton.bananas = [[NSMutableArray alloc]init];
UIImage *imageA = [UIImage imageNamed:@"A.png"];
UIImage *imageB = [UIImage imageNamed:@"B.png"];
UIImage *imageC = [UIImage imageNamed:@"C.png"];
Banana *yellowBanana = [[Banana alloc] initWithName:@"Yellow" description:@"Beautiful" weight:22.0 icon:imageA];
Banana *greenBanana = [[Banana alloc] initWithName:@"Green" description:@"Gorgeous" weight:12.0 icon:imageB];
Banana *rottenBanana = [[Banana alloc] initWithName:@"Rotten" description:@"Ugly" weight:8.0 icon:imageC];
[mySingleton.bananas addObject:yellowBanana];
[mySingleton.bananas addObject:greenBanana];
[mySingleton.bananas addObject:rottenBanana];
}
回答by Jano
Do your singleton like this:
像这样做你的单身人士:
@interface Singleton : NSObject
@property (nonatomic, retain) NSMutableArray *bananas;
+(Singleton*)singleton;
@end
@implementation Singleton
@synthesize bananas;
+(Singleton *)singleton {
static dispatch_once_t pred;
static Singleton *shared = nil;
dispatch_once(&pred, ^{
shared = [[Singleton alloc] init];
shared.bananas = [[NSMutableArray alloc]init];
});
return shared;
}
@end
The singleton is initialized the first time you use it. You can call it from anywhere at any time:
单例在您第一次使用时被初始化。您可以随时随地调用它:
NSLog(@"%@",[Singleton singleton].bananas);
回答by fzwo
You use lazy instantiation, that is, a class method that returns your singleton object. The first time this method is called, it creates the instance, all other times thereafter, it just returns the already available instance (retained in a class variable).
I thought the point of your singleton was to hold this array? You could either create it in the singleton's initializer, or create it lazily when it is needed the first time.
In your AppName-pefix.pch file, you #import its class. This global import will be available in your whole application.
您使用惰性实例化,即返回单例对象的类方法。第一次调用此方法时,它创建实例,此后所有其他时间,它只返回已经可用的实例(保留在类变量中)。
我认为你的单身人士的重点是保存这个数组?您可以在单例的初始化程序中创建它,也可以在第一次需要时懒惰地创建它。
在你的AppName-pefix.pch 文件中,你 #import 它的类。此全局导入将在您的整个应用程序中可用。