从nsarray中获取n个随机对象(例如4)。

时间:2022-09-25 10:19:29

I have a large NSArray of names, I need to get random 4 records (names) from that array, how can I do that?

我有一个大的NSArray的名字,我需要从那个数组中随机取4个记录(名字),我该怎么做呢?

3 个解决方案

#1


21  

#include <stdlib.h>

NSArray* names = ...;
NSMutableArray* pickedNames = [NSMutableArray new];

int remaining = 4;

if (names.count >= remaining) {
    while (remaining > 0) {
       id name = names[arc4random_uniform(names.count)];

       if (![pickedNames containsObject:name]) {
           [pickedNames addObject:name];
           remaining--;
       }
    }
}

#2


2  

I made a caregory called NSArray+RandomSelection. Just import this category into a project, and then just use

我做了一个叫做NSArray+随机选择的caregory。只需将此类别导入到项目中,然后使用即可。

NSArray *things = ...
...
NSArray *randomThings = [things randomSelectionWithCount:4];

Here's the implementation:

这是实现:

NSArray+RandomSelection.h

NSArray + RandomSelection.h

@interface NSArray (RandomSelection)
    - (NSArray *)randomSelectionWithCount:(NSUInteger)count;
@end

NSArray+RandomSelection.m

NSArray + RandomSelection.m

@implementation NSArray (RandomSelection)

- (NSArray *)randomSelectionWithCount:(NSUInteger)count {
    if ([self count] < count) {
        return nil;
    } else if ([self count] == count) {
        return self;
    }

    NSMutableSet* selection = [[NSMutableSet alloc] init];

    while ([selection count] < count) {
        id randomObject = [self objectAtIndex: arc4random() % [self count]];
        [selection addObject:randomObject];
    }

    return [selection allObjects];
}

@end

#3


2  

If you prefer a Swift Framework that also has some more handy features feel free to checkout HandySwift. You can add it to your project via Carthage then use it like this:

如果你更喜欢一个快速的框架,它也有一些更方便的特性,你可以免费去付款。您可以通过Carthage将其添加到您的项目中,然后使用它:

import HandySwift    

let names = ["Harry", "Hermione", "Ron", "Albus", "Severus"]
names.sample() // => "Hermione"

There is also an option to get multiple random elements at once:

还有一个选项可以同时获得多个随机元素:

names.sample(size: 3) // => ["Ron", "Albus", "Harry"]

I hope this helps!

我希望这可以帮助!

#1


21  

#include <stdlib.h>

NSArray* names = ...;
NSMutableArray* pickedNames = [NSMutableArray new];

int remaining = 4;

if (names.count >= remaining) {
    while (remaining > 0) {
       id name = names[arc4random_uniform(names.count)];

       if (![pickedNames containsObject:name]) {
           [pickedNames addObject:name];
           remaining--;
       }
    }
}

#2


2  

I made a caregory called NSArray+RandomSelection. Just import this category into a project, and then just use

我做了一个叫做NSArray+随机选择的caregory。只需将此类别导入到项目中,然后使用即可。

NSArray *things = ...
...
NSArray *randomThings = [things randomSelectionWithCount:4];

Here's the implementation:

这是实现:

NSArray+RandomSelection.h

NSArray + RandomSelection.h

@interface NSArray (RandomSelection)
    - (NSArray *)randomSelectionWithCount:(NSUInteger)count;
@end

NSArray+RandomSelection.m

NSArray + RandomSelection.m

@implementation NSArray (RandomSelection)

- (NSArray *)randomSelectionWithCount:(NSUInteger)count {
    if ([self count] < count) {
        return nil;
    } else if ([self count] == count) {
        return self;
    }

    NSMutableSet* selection = [[NSMutableSet alloc] init];

    while ([selection count] < count) {
        id randomObject = [self objectAtIndex: arc4random() % [self count]];
        [selection addObject:randomObject];
    }

    return [selection allObjects];
}

@end

#3


2  

If you prefer a Swift Framework that also has some more handy features feel free to checkout HandySwift. You can add it to your project via Carthage then use it like this:

如果你更喜欢一个快速的框架,它也有一些更方便的特性,你可以免费去付款。您可以通过Carthage将其添加到您的项目中,然后使用它:

import HandySwift    

let names = ["Harry", "Hermione", "Ron", "Albus", "Severus"]
names.sample() // => "Hermione"

There is also an option to get multiple random elements at once:

还有一个选项可以同时获得多个随机元素:

names.sample(size: 3) // => ["Ron", "Albus", "Harry"]

I hope this helps!

我希望这可以帮助!