0

current_userユーザーの配列からアプリのユーザーのペアを提示する方法を理解しようとしていcurrent_userますが、同じユーザーのペアを再度表示する必要はありません。数学的には、これが an choose 2であることを理解しています。ここで、n はユーザー配列のサイズです。ただし、データ構造を設定する方法がわからないため、ランダムに、ペアのすべての組み合わせを に提示できcurrent_userます。ありがとう!

4

1 に答える 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 に答える