ios - 有一个更好的方法吗?想使用循环,但它中断了

标签 ios xcode ipad

<分区>

我有以下代码块,将最多六个 Player 对象添加到六个 Seat UIView 对象。我使用存储在 NSMuteableDictionary 中的数据添加它们。

这是它目前的样子:

MoneyCentralAppDelegate *delegate = (MoneyCentralAppDelegate *) [[UIApplication sharedApplication] delegate];

//add player1
NSMutableDictionary *player1Dictionary = [[delegate appDataDictionary] valueForKey:@"Player1"];
Player *player1 = [[Player alloc] initWithFrame:CGRectMake(0,0,0,0)];
player1.playerName = [player1Dictionary valueForKey@"PlayerName"];
player1.avatar = [UIImage imageNamed:[player1Dictionary valueForKey:@"Avatar"]];
[seat1 setAlpha:1];
[seat1 addSubView:player1];

//add player2
NSMutableDictionary *player2Dictionary = [[delegate appDataDictionary] valueForKey:@"Player2"];
if ([player2Dictionary valueForKey:@"PlayerName"] != @"Enter your name") {
 Player *player2 = [[Player alloc] initWithFrame:CGRectMake(0,0,0,0)];
 player2.playerName = [player2Dictionary valueForKey@"PlayerName"];
 player2.avatar = [UIImage imageNamed:[player2Dictionary valueForKey:@"Avatar"]];
 [seat2 setAlpha:1];
 [seat2 addSubView:player2];
}

//And so on for another 4 more players...

有没有办法通过使用循环来简化这段代码?我尝试了以下但它不起作用:

for (int i=1; i=6, i++) {
 [self addPlayerToBoard:i];
}

- (void) addPlayerToBoard:(int)playerNumber {

MoneyCentralAppDelegate *delegate = (MoneyCentralAppDelegate *) [[UIApplication sharedApplication] delegate];
NSMutableDictionary *playerDictionary;
NSString *thePlayer;
Seat *seat;

switch (playerNumber) {
 case 1:
  thePlayer = @"Player1";
  seat = seat1;
  break;
 case 2:
  thePlayer = @"Player2";
  seat = seat2:
  break;
 case 3:
  //and so on to 6
}

playerDictionary = [[delegate appDataDictionary] valueForKey:thePlayer];
if ([playerDictionary valueForKey:@"PlayerName"] != @"Enter your name") {
 Player *newPlayer = [[Player alloc] initWithFrame:CGRectMake(0,0,0,0)];
 newPlayer.playerName = [playerDictionary valueForKey:@"PlayerName"];
 newPlayer.avatar = [UIImage imageName:[playerDictionary valueForKey:@"Avatar"]];
 [seat setAlpha:1];
 [seat addSubView:newPlayer];
}
}

据我所知,这段代码是好的。它编译时没有错误或警告,但当我尝试开始新游戏时,应用程序在尝试运行该代码时只是卡住。

另外,作为附带问题,if 语句检查 PlayerName != @"Enter your name"的值是否显然评估为 true 而不管是什么,因为我每次都会有 6 个玩家。这是在 nsmutabledictionary 中检查字符串值的错误方法吗?

我知道这有很多要看的地方,但我非常感谢这里的任何帮助或建议。

非常感谢。

最佳答案

问题在于您创建 for 循环的方式。正确的语法是:for(initialize; test; update)。要从 1 到 6 遍历变量 i,您应该使用:

for(int i = 1; i <= 6; i++) {
    [self addPlayerToBoard:i];
}

当您在对象上使用 !=== 运算符时,您是在比较对象的指针。这意味着两个对象只有在完全相同的情况下才是相同的,而不仅仅是等价的。要确定两个对象是否相同,您应该使用 [object1 isEqualTo:object2]。如果你知道一个对象将是一个 NSString,你应该使用 [string1 isEqualToString:string2],因为它会更快。

if (![[playerDictionary valueForKey:@"PlayerName"] isEqualToString:@"Enter your name"]) {
    ...
}

关于ios - 有一个更好的方法吗?想使用循环,但它中断了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6740648/

上一篇:iphone - 如何在 iPhone 应用程序中实现照片效果?

下一篇:objective-c - UINavigationBar 问题

相关文章:

IOS 应用程序在安装时不响应 IOS 模拟器上的触摸事件

ios - 无法验证适用于 iOS App Store 的应用程序,缺少 armv6 架构

ios - 从 UITabBarController 应用程序中的另一个 UIViewController 调用 UISplitViewController

javascript - 如何在ipad上模拟点击和拖动操作?

Xcode - 如何使主 Controller 和详细 Controller 同时显示?

iphone - 当模态视图存在时,iOS 父 View 不会在旋转时调整大小

ios - 为什么我们在声明自定义 View 时不必使用 "frame"?

ios - 如何在 iOS 中为预期的断言/断言失败编写单元测试?

ios - Xcode 8 应用程序在 iPhone 上安装失败

ios - 我收到错误提示 #import <TwitterKit/TwitterKit.h> 在 xcode 中找不到