无法在单例中访问可变数组 [英] cannot access mutable array in singleton

查看:51
本文介绍了无法在单例中访问可变数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

singleton.h

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:

i have my 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(共享对象)读取值,我会得到正确的值.但这是在同一类中设置的

and then i use it in 3 classes total in one i set values: if i read values from the CCV (sharedobject) i get the correct values. but this is in the same class as they are set from

CCV = [CrestronControllerValues sharedManager];
CCV.currentPhonebookEntriesAudioChat = currentPhonebookEntriesAudioChat;

另一个我读了值:(这些显示/读取为零)

and another i read the values: (these show/read as nil)

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;
}

但是除了我实际设置值的类之外,当我从另一个类访问它时,我没有得到填充的数组我已经完成了 NSLOG(@"%@", CCV) 并且从我可以看到的所有三个类都具有相同的指针,因此共享实例似乎正在工作

but besides the class that i actually set the values in i do not get the filled array when i access it from another class i have done NSLOG(@"%@", CCV) and from what i can see all three classes have the same pointer so the shared instance seems to be working

推荐答案

所以最后我能做的就是道歉.你们中没有人拥有查看正在发生的事情所需的代码.
这是正在保存的数组(上面被删节了(坏主意))

so in the end all i can do is apologize. none of you had the code that you would have needed to see what was going on.
here is the array being saved (aboved was abridged (bad idea))

            if ([phonebookEntriesAudioChat count] >=8) {                

                [CCV setCurrentPhonebookEntriesAudioChat:phonebookEntriesAudioChat];
                [phonebookEntriesAudioChat removeAllObjects];

            }

基本上我试图从套接字返回向数组添加一个项目.为每个返回/消息获取一个地址,最多 8 个.所以我填充了一个临时数组(phonebookEntriesAudioChat)并为每条消息添加了一个,一旦达到8,将其保存到我的单身人士(CCV)中.但有些方法(我仍在尝试解决这个问题)它会达到 8,被保存,临时数组被清除,然后将数组(一个空数组)重新保存到单例中.

basically i was tring to add an item to the array from a socket return. getting one address up to 8 for each return/message. so i populated a temporary array (phonebookEntriesAudioChat) and added one to it for each message and once it got to 8 saved it to my singleton (CCV). but some how (and im still trying to figure this out) it would get to 8, be saved, temporary array cleared, then resaved the array (an empty one) to the singleton.

感谢所有的帮助和指导,我知道如果你们中的一个人想要一些简单的点,我自己的答案不会得到积分,只需用类似的描述重新回答,我就会给你支票.否则我只会投票支持你的评论,并在一两天内将其标记为答案.

thanks for all the help and direction, i know i dont get points for my own answer if one of you wants some easy points just re answer with a simliar description as this and ill give u the check. otherwise im just going to vote up ur comments and mark this as the answer in a day or two.

这篇关于无法在单例中访问可变数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆