对于..在objective-c

道奇蓝

我在Objective-C中有一个关于for..in(for循环)的问题

以下是我的标头和实现文件:

==标题==

#import <Foundation/Foundation.h>

@interface Card : NSObject

@property(strong, nonatomic) NSString *contents;
@property(nonatomic, getter = isMatched) BOOL matched;
@property(nonatomic, getter = isChosen) BOOL chosen;


-(int)match: (NSArray *)otherCards;

@end

==实现==

#import "Card.h"

@implementation Card

-(int)match: (NSArray *) otherCards{
    int score =0;

    for(Card* card in otherCards){
        if ([card.contents isEqualToString:self.contents]) {
            score =1;
        }
    }
    return score;
}
@end
  1. 在实现文件中(在for循环内),self.contents中的self指的是什么?

  2. for循环是否打算将阵列中的一个卡与阵列中的其他卡进行比较?

  3. 您将如何在常规迭代中编写循环for(int i=0, i<n; i++)假设n是循环的大小

谢谢

godel9

1)收到match消息的卡

2)不,它将一张卡(self)与一张卡(进行比较otherCards

3)这是使用索引的等效代码:

for(int i = 0; i < [otherCards count]; i++)
    if([otherCards[i].contents isEqualToString:self.contents]) {
        score = 1;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章