视图对象突然变为零-xcode

视图对象突然变为零-xcode,xcode,reference,ios6,retaincount,Xcode,Reference,Ios6,Retaincount,在我的方法中,视图对象突然变为零 我没有使用弧 不涉及线程 发生的事情是,我第一次调用1stmethod方法时,一切正常,对LiveScore设置的引用被保留 接下来,当我调用2ndmethod方法时,livescoreSettings ref也会保留,但当委托方法被激活时,该变量的引用将丢失。。不知道为什么 @interface XY { LiveScoreSettingsView * livescoreSettings; // initialisation in .h file i

在我的方法中,视图对象突然变为零

我没有使用弧

不涉及线程


发生的事情是,我第一次调用1stmethod方法时,一切正常,对LiveScore设置的引用被保留

接下来,当我调用2ndmethod方法时,livescoreSettings ref也会保留,但当委托方法被激活时,该变量的引用将丢失。。不知道为什么

@interface XY {
    LiveScoreSettingsView * livescoreSettings; // initialisation in .h file inside    
}
@end

@implementation

// 1st method
- (void)1stmethod:(id) callingClass username:(NSString*)username {
    livescoreSettings=callingClass;   // retain count increases to 1 
    isLivescoresSettingsView = YES;

    //.... some code where the above livescoreSettings variables are not used ... //
}

// 2nd method  
- (void)2ndmethod:(id) callingClass username:(NSString*)username matchid:(NSString *)matchid  eventType:(NSString *) eventType  add:(NSString *) add {
    livescoreSettings=callingClass;
    isLivescoresSettingsView = YES;
    addEventToList = YES;

    //.... some code where the above livescoreSettings variables are not used ... //
}

// delegate method thats activated when the response comes 
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {
   // the block where the data is sent to a particular view to reload table 
   else if(isLivescoresSettingsView== YES || addEventToList == YES) {
    isLivescoresSettingsView=NO;
    addEventToList = NO;

     //.... some code where the above livescoreSettings variables are not used ... //

    if(success)
        NSLog(@"No Errors with retain count = %d ", [livescoreSettings retainCount]); 
    else
        NSLog(@"Error Error Error!!!");

    [livescoreSettings.tableView reloadData];

   // when **2ndmethod** is called there's no memory reference to  livescoreSettings, tableView delegate methods are not called which is obvious. But not sure why the retain count is reducing abruptly.
    }
}

@end

问题是您没有获得传递到
1stmethod
2ndmethod
livescoreSetting
的所有权。如果您没有使用ARC,那么您需要在这些方法中保留它,并在您的
dealloc
方法中释放它(只需将实例分配给
livescoreSetting
就可以增加使用MRR时的保留计数)

想象一下,如果以这种方式调用
1stmethod

LivescoreSettingsView *view = [[LivescoreSettingsView alloc] init];
[whateverItsCalled 1stmethod:view;         // (1)
[view release];                            // (2)
然后,
view
被分配到(1)处的
whateVeritScaled.livescoreSetting
,但保留计数为1。在(2)之后,保留计数为0,但是
whateVeritScaled。livescoreSetting
现在是一个悬空指针,我很惊讶您没有看到像“message sent to deallocated object”这样的消息,而没有看到您看到的错误(我不明白为什么在不涉及ARC的情况下将其分配给
nil

要解决这个问题,您需要通过为实例变量添加
@属性来合成该实例变量的setter/getter方法。我更喜欢使用一个前导下划线(
\uu
)来命名实例变量,以区别于setter/getter方法名称;因此:

.h文件:

@interface WhateverItsCalled : NSObject
{
    LiveScoreSettingsView *_livescoreSetting;
}

@property (retain, nonatomic, readwrite) LiveScoreSettingsView *livescoreSetting;
.m文件:

@implementation WhateverItsCalled
@synthesize livescoreSetting = _livescoreSetting;

- (void)dealloc
{
    self.livescoreSetting = nil;           // Release the object by assigning nil
    [super dealloc];
}

- (void)firstmethod:(id) callingClass username:(NSString*)username
{
    self.livescoreSettings = callingClass;   // Note the use of self!
    isLivescoresSettingsView = YES;
}

- (void)secondmethod:(id)callingClass username:(NSString*)username matchid:(NSString *) matchid  eventType:(NSString *) eventType  add:(NSString *) add

{
    self.livescoreSettings = callingClass;   // Note the use of self!
    isLivescoresSettingsView = YES;
    addEventToList = YES;
}

livescoreSettings
也是一个实例变量吗?没有弧。。。livescoreSettings不是实例变量。。它在.h文件号中声明。。没有线程涉及DOK,所以如果
lovescoreSettings
不是实例变量,它在哪里定义?这就像是20个问题,不应该是这样!如果你看到我在代码中的注释,我在其中提到了它在.h文件@interfaceI中声明的注释,我完全同意,但是:D--只是为了添加信息:你不再需要变量或@synthesis--只需要一个属性就够了(Xcode 4.5+)@Daij Djan同意;有了Xcode/clang的新特性,这是不需要的。也许我是老式的,因为我喜欢查看类定义并查看它使用的实例变量,而不必通过查看
@property
部分来计算。对我来说,这并不是苹果的进步;只是懒惰的借口…事实上我试过这个。。使之成为一种财产。。但是不起作用。。。当第二个方法被称为..@trojanfoe时,它将失去其引用-谢谢。你的回答帮助解决了这个问题