gpt4 book ai didi

ios - 检查是数组中的字符串

转载 作者:行者123 更新时间:2023-11-29 10:44:30 25 4
gpt4 key购买 nike

我有多个数组,我想检查从一个数组中随机选择的字符串是否在另一个数组中,如果是,则显示图像

我在 h 文件和 m 文件中全局设置了 plistarray1 和 plistarray2

最后一段代码是我无法根据从 plistarray 生成的随机字符串来显示图像的问题。

NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];






NSString *path = [[NSBundle mainBundle] pathForResource:
@"data" ofType:@"plist"];







if ([[defaults objectForKey:@"truthonoff"] isEqualToString:@"YES"] && [[defaults objectForKey:@"dareonoff"] isEqualToString:@"YES"] ) {

self.text.text =@"Are you ready for this?";
[Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];





NSDictionary *plistDict1 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray * plistArray1 = plistDict1[@"truth"];



NSDictionary *plistDict2 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray2 = plistDict2[@"dare"];

self.plistArray = [[plistArray1 arrayByAddingObjectsFromArray:plistArray2] mutableCopy];

}

else if ([[defaults objectForKey:@"truthonoff"] isEqualToString:@"YES"] ) {

self.text.text =@"I hope you are feeling brave!";
[Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];




NSDictionary *plistDict3 = [[NSDictionary alloc] initWithContentsOfFile:path];

NSArray *plistArray3 = plistDict3[@"truth"] ;

self.plistArray = [plistArray3 mutableCopy];


NSLog(@"%@", plistArray);


}

else if ([[defaults objectForKey:@"dareonoff"] isEqualToString:@"YES"] ) {

self.text.text =@"This could be interesting!";
[Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];



NSDictionary *plistDict4 = [[NSDictionary alloc] initWithContentsOfFile:path];

NSMutableArray *plistArray4 = plistDict4[@"dare"];

self.plistArray = [plistArray4 mutableCopy];


NSLog(@"%@", plistArray);


}
else {
self.text.text =@"Please turn on Truth or Dare";
[Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];



}



////display random quote from array

int randV = arc4random() % self.plistArray.count;
NSLog(@"%@", plistArray);


self.text.text = self.plistArray[randV];
[self.plistArray removeObjectAtIndex:randV];
[Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];

//display truth or dare image
if ([plistArray containsObject:plistArray1]) {
// selectedString is from the truth array
self.truthimage.hidden = NO;


}

最佳答案

您的代码检查 plistArray 是否包含 plistArray1 对象,它不包含(最初,它包含 plistArray1 中的每个对象,但是而不是 plistArray1 对象本身)。

您的代码应该检查文本是否存在,如下所示:

// Obtain the random quote that you plan to display
NSString* toDisplay = self.plistArray[randV];
// Display the quote
self.text.text = toDisplay;
// Remove that quote from the plistArray
[self.plistArray removeObjectAtIndex:randV];
[Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];
// Now's the fix: check plistArray1 for the toDisplay object
if ([plistArray1 containsObject:toDisplay]) {
// selectedString is from the truth array
self.truthimage.hidden = NO;
}

如果您将 plistArray1 替换为一个集合,则可以使此代码更高效,这将允许您检查摊销常数而不是线性时间的包含。

编辑:(作为对这条评论的回应:“plistArray1 被带回为 nil”)

这是因为除了具有同名的实例变量外,您还在方法中将 plistArray1 声明为局部变量。替换这一行

NSArray * plistArray1 = plistDict1[@"truth"];

有了这个

plistArray1 = plistDict1[@"truth"];

解决问题。

关于ios - 检查是数组中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22837618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com