current_user
ユーザーの配列からアプリのユーザーのペアを提示する方法を理解しようとしていcurrent_user
ますが、同じユーザーのペアを再度表示する必要はありません。数学的には、これが an choose 2
であることを理解しています。ここで、n はユーザー配列のサイズです。ただし、データ構造を設定する方法がわからないため、ランダムに、ペアのすべての組み合わせを に提示できcurrent_user
ます。ありがとう!
1 に答える
1
2 人のユーザーの ID で構成されるクラスを作成します。
@interface userPair: NSObject
@property (nonatomic, assign) NSInteger user1id;
@property (nonatomic, assign) NSInteger user2id;
@end
可能なすべてのペアの NSMutableArray を作成してから、この素晴らしいシャッフルメソッドを実行します:(この回答から取得)
- (void)shuffle
{
NSUInteger count = [self count];
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
NSInteger nElements = count - i;
NSInteger n = (arc4random() % nElements) + i;
[self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
于 2013-08-23T04:14:43.727 に答える