0

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

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

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

NSString *test = currentLocation;

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

NSString *test = self.currentLocation;

然后现在显示正确的值,有人可以解释为什么 currentLocation / self.currentLocation 被视为不同的对象,我理解那个自我。使用访问器,但如果设置正确,它们肯定都应该指向相同的值?

编辑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];
}

但是,这一次我遇到了一个变量似乎未设置的崩溃,即使它在此过程中的任何时候都没有更改(参见 EDIT 1 中的代码)。

编辑 3:

现在我很困惑,我以为我有答案,但我只是没有。我尝试使用 Erics 的建议:

@synthesize currentLocation = _currentLocation;

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

编辑4:

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

4

5 回答 5

3

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

@synthesize myVar = _myVar

然后你的界面中也有

interface {
  myVar
}

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

于 2012-07-19T13:49:57.487 回答
1

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

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

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

对edit3的回答:

init方法 和dealloc是应该直接访问属性 ivar 的唯一两个地方。在dealloc你通常直接写[_currentLocation release]。然而,关于它的讨论很大。一般来说,只是投入是正确的,self.currentLocation = nildealloc你必须知道你在做什么。

于 2012-07-19T13:51:06.417 回答
0

使用@synthesize currentLocation = _currentLocation.

于 2012-07-19T13:50:06.847 回答
0

您显示的代码没有任何问题。但是您已经展示了代码的点点滴滴(甚至没有完整的方法或类),而且很难说出发生了什么。错误必须来自其他东西。所有这些关于更改变量名的讨论都完全无关紧要——使用相同的变量名并没有错。

于 2012-07-21T03:03:29.923 回答
0

为了简单起见:

属性 = 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
}

这里 yourName、self.myName 和是三个不同的东西:

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

现在我想将 yourName 连接到 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 。

于 2012-07-19T17:13:27.370 回答