arc4random_uniform not available in Xcode 7.0 beta (7a176x) on OSX 10.10.4
It seems to be still available (I have been using it in 7A176x, but on El Capitan), it is just not shown in the suggestions.
How does one generate a random number in Swift?
Swift 4.2+
Swift 4.2 shipped with Xcode 10 introduces new easy-to-use random functions for many data types.
You can call the random()
method on numeric types.
let randomInt = Int.random(in: 0..<6)
let randomDouble = Double.random(in: 2.71828...3.14159)
let randomBool = Bool.random()
Swift 2 arch4random
Have an array of Names instead of a gigantic switch case:
var names = ["Kim.", "Phil.", "Tom", "Jeff", "Austin"] // and all your remaining names
let originalNames = names
func getRandomName() -> String {
if (names.count == 0) {
names = originalNames
}
let randomNumber = Int(arc4random_uniform(UInt32(names.count)))
return names.removeAtIndex(randomNumber)
}
This ensures every name gets printed before starting from the beginning again. The sample output is:
Tom, Kim., Austin, Phil., Jeff
and then it starts again
Austin, Jeff, Phil. ...
Finally put something like the following wherever it fits your need:
self.randomLabel.text = getRandomName()
bridgeToObjectiveC and makeObjectsPerformSelector in Swift beta 5
The bridgeToObjectiveC
and bridgeFromObjectiveC
functions are not available in Xcode 6.0 beta 5. Instead, cast to/from the appropriate Foundation type when you need to use that type's API on a Swift object. For example:
var arr = ["One", "Two"]
(arr as NSArray).indexOfObject("One")
Apple has warned against (or explicitly made unavailable) using performSelector
and related methods since the first Swift beta. Presumably any such API that were still available before beta 5 were unintentionally so.
As the question you cited notes, you can use map
to call a function/method on every element of an array. You can also use filter
, find
or a for
-in
loop, or after casting to NSArray
, one of the enumerateObjects
methods. Note that many consider it bad style to use the functional-programming constructs (map
, filter
, reduce
, find
) for tasks that aren't "functional" -- that is, to run code that has side effects. So a for
-in
loop might be the cleanest way to do what you're after.
Related Topics
How to Execute Some Code After a Segue Is Done
Uisearchcontroller Persisting After Segue
Why Would a 'Scheduledtimer' Fire Properly When Setup Outside a Block, But Not Within a Block
Error Itms-90086 Submitting App
Nsnotificationcenter Swift 3.0 on Keyboard Show and Hide
Perform a Deeplink from Swiftui Widget on Tap
Exception Type: Exc_Crash (Sigabrt)
How to Use Http Live Streaming Protocol in iPhone Sdk 3.0
Deferredlocationupdatesavailable Returns No in iOS 10
Uisearchdisplaycontroller Without Dimming
Get Dns Server Ip from iPhone Settings
Region Monitoring Glitch on iOS 7 - Multiple Notifications at the Same Time
Search Bar and Search Display Controller in Table View
Adding Unlimited Lines in a Text (Swiftui)
Passing Data Back from a Modal View in Watchkit
iOS Autolayout and Uitoolbar/Uibarbuttonitems