gpt4 book ai didi

ios - 从 NSArray 中检索对象并将每个值设置为单独的 NSString

转载 作者:行者123 更新时间:2023-11-29 03:42:24 24 4
gpt4 key购买 nike

我创建了一个 NSMutableSet,其中包含 7 个随机生成的数字。我希望能够将它们从集合中取出,这样我就可以使用 switch 语句将每个随机生成的数字设置为特定的字符串。我怎样才能做到这一点?

-(IBAction)goGenerate:(id)sender
{
NSMutableSet *eventNumbers = [[NSMutableSet alloc] initWithCapacity:7];

while ([eventNumbers count] < 7)
{
NSNumber *randomNumber = [NSNumber numberWithInt:(arc4random() % 13 + 1)];

[eventNumbers addObject:randomNumber];
}

//NSLog(@"eventNumbers : %@ \n\n", eventNumbers);


//NSArray *events = [eventNumbers allObjects];

最佳答案

so I can use a switch statement to set each randomly generated number to a specific string.

您无法将 NSNumber 转换为 NSString,但您可以删除一个对象并将其替换为另一个对象,或者只是创建一个新集合,如下所示:

NSMutableSet *eventStrings = [NSMutableSet set];
for (NSNumber *item in eventNumbers) {
switch (item.intValue) {
case 1: {
[eventStrings addObject:@"The string for number 1."];
break;
}
case 2: {
[eventStrings addObject:@"The string for number 2."];
break;
}
case 3: {
[eventStrings addObject:@"The string for number 3."];
break;
}
default: {
NSLog(@"Oops, got a number I wasn't expecting!"
break;
}
}
}
NSLog(@"The set of randomly selected strings is: %@", eventStrings);

关于ios - 从 NSArray 中检索对象并将每个值设置为单独的 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18216661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com