【问题标题】:Objective C - Fill a UIlabel with a random string after clicking a button [duplicate]Objective C - 单击按钮后用随机字符串填充 UIlabel [重复]
【发布时间】:2012-12-31 13:20:27
【问题描述】:

可能重复:
Generate a random alphanumeric string in cocoa

我是 Objective c 的新手,我想在我的应用中添加以下功能:

我有一个标签,该标签在应用启动后具有默认文本。 每次用户单击按钮时,我希望该标签更改为随机字符串。 字符串最终是否重复并不重要,只要它是随机的。

提前感谢您的帮助。

【问题讨论】:

标签: ios objective-c string random uibutton


【解决方案1】:

您可以使用以下代码:

[[NSProcessInfo processInfo] globallyUniqueString]; 

用于生成随机字符串。

您可以创建包含多个字符串的数组。假设您创建了 10 个字符串并将它们放入一个数组中:

int rand= arc4random()%10 
NSString *str=[array objectAtIndex:rand];

【讨论】:

  • 不要将 arc4random 与模运算符结合使用。你想要的是:uint_32_t rand = arc4random_uniform(10);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
相关资源
最近更新 更多