使用自我。设置但不访问原因问题

时间:2012-07-19 13:46:37

标签: iphone objective-c xcode self accessor

我现在感到困惑,为什么以下情况在某些情况下有效但在其他情况下无效。使用以下代码时:

NSString *currentLoc = [[NSString stringWithString:sceneName] retain];
self.currentLocation = currentLoc;
[currentLoc release];

这应该用新值覆盖我的NSString *currentLocation, of type (nonatomic, retain)。为什么我现在不能通过执行以下操作来访问此值:

NSString *test = currentLocation;

调试时我发现测试设置为随机数组,因此指针必须指向完全不同的东西。我已检查过代码,currentLocation变量仅在此处设置。如果我使用以下内容:

NSString *test = self.currentLocation;

然后显示正确的值,有人可以解释为什么currentLocation / self.currentLocation被视为不同的对象,我理解为self。使用访问者,但如果设置正确,他们应该指向相同的值吗?

编辑1 :(所有代码)

·H

NSString *currentLocation;
@property (nonatomic, retain) NSString *currentLocation;

的.m

@synthesize currentLocation;

NSString *currentLoc = [[NSString stringWithString:sceneName] retain];
    self.currentLocation = currentLoc;
    [currentLoc release];

if (currentLocation != nil && singleton.storyLocation != nil)
    {
        if (boolMoviePlayed == false && [singleton.storyLocation isEqualToString:currentLocation]) // crash here due to current location being set to an array
            [buttonPlayMovie setHidden:!textVisible];
    }

    self.currentLocation = nil; // viewDidUnload
    [currentLocation release]; // dealloc

编辑2 :(更新的代码,仍然崩溃)

所以这次我尝试按照设置的方式访问代码,如下所示:

if (self.currentLocation != nil && singleton.storyLocation != nil)
{
    if (boolMoviePlayed == false && [singleton.storyLocation isEqualToString:self.currentLocation])
        [buttonPlayMovie setHidden:!textVisible];
}

然而,这次我遇到崩溃,其中变量似乎未设置,即使在此过程中任何时候都没有更改(参见编辑1中的代码)。

编辑3:

现在我感到困惑,我以为我得到了答案,但我很清楚。我尝试使用Erics建议:

@synthesize currentLocation = _currentLocation;

然后我确保使用self.currentLocation = xyz设置对currentLocation的所有访问。现在谈到dealloc方法,我总是被警告不要使用self。在dealloc。但这意味着我的所有值都是零,因为currentLocation尚未设置。使用[self.currentLocation release]是安全的,或者如何解决我的问题?

编辑4:

据我所知,你在dealloc中使用[_currentLocation release],如果我错了请纠正我,如果不是的话我现在会沿着这条路走下去,因为它似乎有效。

5 个答案:

答案 0 :(得分:3)

我会看看你是如何合成的以及你的ivars。实际上,currentLocation直接引用iVar,而self.currentLocation可能指向一个不同的变量,如果你这样合成它,例如:

@synthesize myVar = _myVar

然后你也在你的界面

interface {
  myVar
}

在这种情况下,您会看到所描述的问题。

答案 1 :(得分:1)

currentLocation是变量,self.currentLocation是调用属性getter(方法)- (NSString*)currentLocation的一种奇特方式。

显然,您正在使用该属性来存储值,并且变量和您的属性之间没有连接集。

您应该检查合成属性currentLocation的行。

回答edit3:

init方法和dealloc是仅应直接访问属性ivar的两个位置。在dealloc中,您通常直接撰写[_currentLocation release]。然而,有一个很大的讨论。一般情况下,将self.currentLocation = nil放入dealloc是正确的,但您必须知道自己在做什么。

答案 2 :(得分:0)

使用@synthesize currentLocation = _currentLocation

答案 3 :(得分:0)

简化:

property = getter + setter + iver

<强>浓淡#1:

·H

NSString *yourName;  //iver
@property (nonatomic, retain) NSString *myName; //property

的.m

@synthesize myName;   

-(void)someMethod{
    self.myName = @"Mike";

    NSString * name = self.myName; // name will be "Mike"
    name = myName; // name will be "Mike"

    name = yourName;   // name will be null
}

这里你的名字,self.myName和三个不同的东西:

yourName是一个iver self.myName是两种不同情况下的getter或setter myName是一个iver

现在我想将yourName iver连接到myName属性。 让我们看看如何在Exp。 #2

浓淡#2

的.m

@synthesize myName = yourName;   

-(void)someMethod{
   self.myName = @"Mike";

    NSString * name = self.myName; // name will be "Mike"

    name = yourName;   // name will be "Mike"

    yourName = @"James";

   name = self.myName ; // name will be "James"

}

在这里你无法使用myName iver。

答案 4 :(得分:0)

您显示的代码没有任何问题。但是你已经展示了一些代码(甚至没有一个完整的方法或类),而且很难分辨出发生了什么。错误必须来自其他内容。所有关于更改变量名称的讨论完全无关紧要 - 使用相同的变量名称没有任何问题。