singleton.h
#import <Foundation/Foundation.h>
@interface CrestronControllerValues : NSObject {
NSString* ipAddress;
NSString* portNumber;
NSString* phoneAddress;
NSString* cameleonVersion;
NSString* systemName;
NSString* iPID;
NSString* systemFeedBackName;
NSString* dJoinConnectedFB;
NSString* dJoinLow;
NSString* dJoinHigh;
NSString* aJoinLow;
NSString* aJoinHigh;
NSString* sJoinLow;
NSString* sJoinHigh;
NSMutableArray *currentPhonebookEntriesTelepresence;
NSMutableArray *currentPhonebookEntriesVideoChat;
NSMutableArray *currentPhonebookEntriesAudioChat;
}
@property (nonatomic, retain) NSString* ipAddress;
@property (nonatomic, retain) NSString* portNumber;
@property (nonatomic, retain) NSString* phoneAddress;
@property (nonatomic, retain) NSString* cameleonVersion;
@property (nonatomic, retain) NSMutableArray *currentPhonebookEntriesTelepresence;
@property (nonatomic, retain) NSMutableArray *currentPhonebookEntriesVideoChat;
@property (nonatomic, retain) NSMutableArray *currentPhonebookEntriesAudioChat;
@property (nonatomic, retain) NSString* systemName;
@property (nonatomic, retain) NSString* iPID;
@property (nonatomic, retain) NSString* systemFeedBackName;
@property (nonatomic, retain) NSString* dJoinConnectedFB;
@property (nonatomic, retain) NSString* dJoinLow;
@property (nonatomic, retain) NSString* dJoinHigh;
@property (nonatomic, retain) NSString* aJoinLow;
@property (nonatomic, retain) NSString* aJoinHigh;
@property (nonatomic, retain) NSString* sJoinLow;
@property (nonatomic, retain) NSString* sJoinHigh;
+ (id)sharedManager;
@end
我有我的singleton.m:
static CrestronControllerValues *sharedMyManager= nil;
@implementation CrestronControllerValues
@synthesize ipAddress, portNumber ,systemName, iPID, systemFeedBackName, dJoinConnectedFB, dJoinLow, dJoinHigh, aJoinLow, aJoinHigh, sJoinLow, sJoinHigh, cameleonVersion, currentPhonebookEntriesAudioChat, currentPhonebookEntriesTelepresence, currentPhonebookEntriesVideoChat, phoneAddress;
+(CrestronControllerValues*)sharedManager
{
@synchronized(self) {
if(!sharedMyManager) {
sharedMyManager = [CrestronControllerValues alloc];
sharedMyManager = [sharedMyManager init];
}
}
}
+(id)alloc
{
@synchronized(self)
{
NSAssert(sharedMyManager == nil, @"Attempted to allocate a second instance of a singleton.");
sharedMyManager = [super alloc];
return sharedMyManager;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
// initialize stuff here
self.ipAddress = @"10.8.40.64";
self.portNumber = 41794;
self.systemName = @"";
self.iPID = 3;
self.cameleonVersion = nil;
self.currentPhonebookEntriesAudioChat = [[NSMutableArray alloc]initWithObjects:nil];
self.currentPhonebookEntriesTelepresence = [[NSMutableArray alloc]initWithObjects:nil];
self.currentPhonebookEntriesVideoChat = [[NSMutableArray alloc]initWithObjects:nil];
self.phoneAddress = nil;
self.systemFeedBackName = @"";
self.dJoinConnectedFB = 5000;
self.dJoinLow = 1;
self.dJoinHigh = 1000;
self.aJoinLow = 1;
self.aJoinHigh = 1000;
self.sJoinLow = 1;
self.sJoinHigh = 1000;
}
return self;
}
return self;
}
-(void)setPhoneAddress:(NSString *)phoneaddress
{
@synchronized(self) {
if (phoneAddress != phoneaddress)
{
[phoneAddress release];
phoneAddress = [phoneaddress retain];
}
}
}
-(NSString*)getPhoneAddress
{
return phoneAddress;
}
-(void)setCurrentPhonebookEntriesAudioChat:(NSMutableArray *)entries
{
@synchronized(self) {
if (currentPhonebookEntriesAudioChat != entries)
{
[currentPhonebookEntriesAudioChat release];
currentPhonebookEntriesAudioChat = [entries retain];
}
}
}
-(NSMutableArray*)getCurrentPhonebookEntriesAudioChat
{
return currentPhonebookEntriesAudioChat;
}
-(void)setCurrentPhonebookEntriesTelepresence:(NSMutableArray *)entries
{
@synchronized(self) {
if (currentPhonebookEntriesTelepresence != entries)
{
[currentPhonebookEntriesTelepresence release];
currentPhonebookEntriesTelepresence = [entries retain];
}
}
}
-(NSMutableArray*)getCurrentPhonebookEntriesTelepresence
{
return currentPhonebookEntriesTelepresence;
}
-(void)setCurrentPhonebookEntriesVideoChat:(NSMutableArray *)entries
{
@synchronized(self) {
if (currentPhonebookEntriesVideoChat != entries)
{
[currentPhonebookEntriesVideoChat release];
currentPhonebookEntriesVideoChat = [entries retain];
}
}
}
-(NSMutableArray*)getCurrentPhonebookEntriesVideoChatLocal
{
return currentPhonebookEntriesVideoChat;
}
-(void)setCameleonVersion:(NSString *)cameleonversion
{
cameleonVersion = cameleonversion;
}
-(NSString*)getCameleonVersion
{
return cameleonVersion;
}
-(void)setIPaddress:(NSString *)ipaddress
{
ipAddress = ipaddress;
}
-(NSString*)getIPaddress
{
return ipAddress;
}
-(void)setPortNumber:(NSString *)portnumber
{
portNumber = portnumber;
}
-(NSString*)getPortNumber
{
return portNumber;
}
-(void)setSystemName:(NSString *)systemname
{
systemName = systemname;
}
-(NSString*)getSystemName
{
return systemName;
}
-(void)setIPID:(NSString *)ipid
{
iPID=ipid;
}
-(NSString*)getIpid
{
return iPID;
}
-(void)setSystemFeedBackName:(NSString *)systemfeedbackname
{
systemFeedBackName=systemfeedbackname;
}
-(NSString*)getSystemFeedBackName
{
return systemFeedBackName;
}
-(void)setDJoinConnectedFB:(NSString *)djoinconnectedfb
{
dJoinConnectedFB = djoinconnectedfb;
}
-(NSString*)getDJoinConnectedFB
{
return dJoinConnectedFB;
}
-(void)setDJoinLow:(NSString *)djoinlow
{
dJoinLow=djoinlow;
}
-(NSString*)getDJoinLow
{
return dJoinLow;
}
-(void)setDJoinHigh:(NSString *)djoinhigh
{
dJoinHigh = djoinhigh;
}
-(NSString*)getDJoinHigh
{
return dJoinHigh;
}
-(void)setAJoinLow:(NSString *)ajoinlow
{
aJoinLow = ajoinlow;
}
-(NSString*)getAJoinLow
{
return aJoinLow;
}
-(void)setAJoinHigh:(NSString *)ajoinhigh
{
aJoinHigh = ajoinhigh;
}
-(NSString*)getAJoinHigh
{
return aJoinHigh;
}
-(void)setSJoinLow:(NSString *)sjoinlow
{
sJoinLow = sjoinlow;
}
-(NSString*)getSJoinLow
{
return sJoinLow;
}
-(void)setSJoinHigh:(NSString *)sjoinhigh
{
sJoinHigh = sjoinhigh;
}
-(NSString*)getSJoinHigh
{
return sJoinHigh;
}
- (void)dealloc
{
[self.ipAddress release];
[self.iPID release];
[self.portNumber release];
[self.currentPhonebookEntriesVideoChat release];
[self.currentPhonebookEntriesTelepresence release];
[self.currentPhonebookEntriesAudioChat release];
[self.aJoinHigh release];
[self.aJoinLow release];
[self.cameleonVersion release];
[self.sJoinHigh release];
[self.sJoinLow release];
[self.dJoinHigh release];
[self.dJoinLow release];
[self.dJoinConnectedFB release];
[super dealloc];
}
@end
然后我总共使用它3个班级 在一个我设定的价值观: 如果我从CCV(共享对象)读取值,我会得到正确的值。但这与从
设置的类相同CCV = [CrestronControllerValues sharedManager];
CCV.currentPhonebookEntriesAudioChat = currentPhonebookEntriesAudioChat;
和另一个我读取的值: (这些显示/读为零)
switch (viewOptions) {
case 1:
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
break;
case 2:
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
break;
case 3:
[self setTableArray:CCV.currentPhonebookEntriesTelepresence];
break;
case 4:
[self setTableArray:CCV.currentPhonebookEntriesAudioChat];
break;
default:
[self setTableArray:CCV.currentPhonebookEntriesVideoChat];
break;
}
但除了我实际在i中设置值的类,当我从另一个类访问它时,我没有得到填充数组 我已经完成了NSLOG(@“%@”,CCV),从我可以看到所有三个类都有相同的指针,所以共享实例似乎正在工作
答案 0 :(得分:2)
这是一个更简单的单例模式,更少的代码更多:
@implementation MySingleton
static MySingleton* _sharedMySingleton = nil;
+(MySingleton*)sharedMySingleton
{
@synchronized([MySingleton class])
{
if (!_sharedMySingleton)
_sharedSingleton = [[MySingleton alloc] init];
}
return _sharedMySingleton;
}
答案 1 :(得分:1)
sharedMyManager
。
在init中,最好直接设置ivars,即不使用由@synthesize
创建的setter,该类尚未完全建立,因此调用它不是一个好主意。< / p>
单身就像任何其他类一样,有一个例外,只有一个。此外,保证单身人士的所有额外方法实际上只是最不存在的噪音 - 但这是一个品味问题。
答案 2 :(得分:1)
考虑:
sharedMyManager = [[super allocWithZone:NULL] init];
将其重写为:
id x = [super allocWithZone:NULL];
id y = [x init];
sharedMyManager = y;
执行init
时,尚未评估sharedMyManager
的分配。因此,sharedMyManager
为nil
,您的init
方法中的所有作业均为无操作。
在init
方法中,总是通过self
引用您的实例变量;通过直接分配给他们(实际上是对自我的引用)或直接使用setter方法(即self.foo = 442;
)。
(这是@CocoaFu所说的,但澄清了)
仔细看一下代码,它有很多问题。
NSString
属性应为copy
,而不是retain
。
您泄漏了所有currentPhonebookEntries*
可变阵列。
Getter方法不应包含前缀get*
使用@synthesize
时无需实现任何getter / setter方法(并且实际上为每个方法创建了两个getter方法;一个有一个,一个没有get
前缀)。
dealloc
方法应该直接释放实例变量,或者应该将属性设置为nil
;不鼓励使用[self.ivar release]
。
我上面展示的代码仅仅是说明性的。如果您的init
仍然通过sharedMyManager
分配,则说明您没有解决问题。
答案 3 :(得分:1)
所以我最终能做的就是道歉。你们没有人拥有你需要的代码来看看发生了什么 这是保存的数组(上面是删节(坏主意))
if ([phonebookEntriesAudioChat count] >=8) {
[CCV setCurrentPhonebookEntriesAudioChat:phonebookEntriesAudioChat];
[phonebookEntriesAudioChat removeAllObjects];
}
基本上我是想从套接字返回向数组中添加一个项目。每个返回/消息最多可获得一个地址。所以我填充了一个临时数组(phonebookEntriesAudioChat),并为每条消息添加了一个,一旦它到8,就将它保存到我的单例(CCV)。但有些如何(我仍然试图弄明白)它会得到8,被保存,临时数组被清除,然后将数组(一个空的)重新保存到单身。
感谢所有的帮助和方向,我知道如果你们中的一个人想要一些简单的点,只是回答一个简单的描述,我就不会为自己的答案得到分数,并且给你支票。否则我只是打算对你的评论进行投票,并在一两天内将其标记为答案。