Objective c 为什么在一个方法中其值已更改的属性在另一个方法中重置?
我声明属性Objective c 为什么在一个方法中其值已更改的属性在另一个方法中重置?,objective-c,xcode,Objective C,Xcode,我声明属性int indx。在实现文件中,我有以下两种方法: -(IBAction)nextHaiku { { if (!self.indx) { self.indx=0; } NSString *cat = self.selectedCategory; //For now (adjust later so that, according to UISegmentedControl, it will also show only the u
int indx头文件中的代码>。在实现文件中,我有以下两种方法:
-(IBAction)nextHaiku
{
{
if (!self.indx)
{
self.indx=0;
}
NSString *cat = self.selectedCategory;
//For now (adjust later so that, according to UISegmentedControl, it will also show only the user's haiku or all haiku):
cat = @"Derfner";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category == %@", cat];
NSArray *filteredArray = [self.gayHaiku filteredArrayUsingPredicate:predicate];
int array_tot = [filteredArray count];
int sortingHat;
if (array_tot > 0)
if (self.indx == self.theseAreDone.count)
{
while (true)
{
sortingHat = (arc4random() % array_tot);
if (![theseAreDone containsObject:[filteredArray objectAtIndex:sortingHat]]) break;
}
self.haiku_text.text = [[filteredArray objectAtIndex:sortingHat] valueForKey:@"quote"];
if (!self.theseAreDone || self.theseAreDone.count==array_tot)
{
self.theseAreDone = [[NSMutableArray alloc] init];
}
[theseAreDone addObject:[filteredArray objectAtIndex:sortingHat]];
self.indx = self.theseAreDone.count;
}
else
{
self.haiku_text.text = [[self.theseAreDone objectAtIndex:indx] valueForKey:@"quote"];
self.indx += self.indx;
}
}
}
及
NSLog
显示每次调用newHaiku
时,self.indx
以1的增量递增。但是NSLog
也显示self.indx
从0开始previousHaiku
,无论上次调用newHaiku
时是什么
我错过了什么?我错过的是我是个白痴这一事实。我使用的是self.indx-=self.indx
而不是self.indx-=1
。别问我为什么。我不能告诉你。self.indx-=self.indx
相当于self.indx=0代码>。也许你的意思是self.indx-=1代码>?有时会发生。你看了又看一段代码,因为你离它太近了,你似乎看不到你面前的是什么。。。第二双眼睛会发现它的速度快得让人难堪!你现在感觉多么愚蠢的一个积极的副作用是你不太可能再犯同样的错误;-)
-(IBAction)previousHaiku
{
self.haiku_text.text = [[self.theseAreDone objectAtIndex:self.indx-1] valueForKey:@"quote"];
self.indx -= self.indx;
}