Arc4Random_Uniform Not Available in Xcode 7.0 Beta (7A176X) on Osx 10.10.4

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



Leave a reply



Submit