Swift return string from Array

user3708761

I'm trying to get a random name from this Array as a string instead of a number.

I.E: The array normally returns a random number 0 through 9, I'd rather have it return the string that 0 through 9 represents like Preston or James instead of the number itself.

The code below is broken, but I hope it lets you see what I'm trying to do.

 var firstName : [String] = ["Preston", "Ally", "James", "Justin", "Dave", "Bacon", "Bossy",     "Edward", "Edweird" ]

var standardIdent = "First Name:\(firstName[random(0...9)]) Last Name:\(lastName[random(0...5)]) \n Age:\(rand())"
println(standardIdent)

Thank you for your help!

Dániel Nagy

You should use arc4random like:

let firstRandom = Int(arc4random() % 10)
let secondRandom = Int(arc4random() % 6)
var standardIdent = "First Name:\(firstName[firstRandom]) Last Name:\(lastName[secondRandom]) \n Age:\(rand())"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift: Return JSON string from NSURLSession task

From Dev

Swift UITableView Return Cell From Array of Arrays

From Java

In Swift, Array [String] slicing return type doesn't seem to be [String]

From Dev

Using a string key to return a value from an array

From Dev

Return array from object if contains string jquery

From Dev

Is it possible to generate enum from Array<String> in Swift?

From Dev

Generation of an array of substring from a string in swift

From Dev

How to use mongoose to return field from object array in string array

From Dev

Getting an Array from Return statement, deduce a string from that

From Dev

Swift Switch String Return

From Dev

Swift 2.0 - How to return tuple from a string input

From Dev

array return empty when trying to call from outside the function [swift]

From Dev

return an Array with swift2 from parse server

From Dev

array return empty when trying to call from outside the function [swift]

From Dev

how to return value from nested array - swift 3

From Dev

Swift - How to create an array of string from an object array

From Dev

return array of phrases by substracting each string from the previous one

From Dev

java: Trying to return a string value from an array of strings via a method

From Dev

Return String array from C++ DLL to VBA(Excel)

From Dev

Array Of Methods That Return A String

From Dev

PHP - Return Array As String

From Dev

PHP - Return Array As String

From Dev

string to array return array in parameter

From Dev

string to array return array in parameter

From Dev

Early return from a function which has a return type of [String] in Swift 3

From Dev

Can I use a string from an array as a selector in Swift?

From Dev

Separating key values from a string in Swift that looks like an object array

From Dev

Swift 3: Remove specific string from array without knowing the indexPath?

From Dev

Newline and / or Carriage Return in Swift String

Related Related

  1. 1

    Swift: Return JSON string from NSURLSession task

  2. 2

    Swift UITableView Return Cell From Array of Arrays

  3. 3

    In Swift, Array [String] slicing return type doesn't seem to be [String]

  4. 4

    Using a string key to return a value from an array

  5. 5

    Return array from object if contains string jquery

  6. 6

    Is it possible to generate enum from Array<String> in Swift?

  7. 7

    Generation of an array of substring from a string in swift

  8. 8

    How to use mongoose to return field from object array in string array

  9. 9

    Getting an Array from Return statement, deduce a string from that

  10. 10

    Swift Switch String Return

  11. 11

    Swift 2.0 - How to return tuple from a string input

  12. 12

    array return empty when trying to call from outside the function [swift]

  13. 13

    return an Array with swift2 from parse server

  14. 14

    array return empty when trying to call from outside the function [swift]

  15. 15

    how to return value from nested array - swift 3

  16. 16

    Swift - How to create an array of string from an object array

  17. 17

    return array of phrases by substracting each string from the previous one

  18. 18

    java: Trying to return a string value from an array of strings via a method

  19. 19

    Return String array from C++ DLL to VBA(Excel)

  20. 20

    Array Of Methods That Return A String

  21. 21

    PHP - Return Array As String

  22. 22

    PHP - Return Array As String

  23. 23

    string to array return array in parameter

  24. 24

    string to array return array in parameter

  25. 25

    Early return from a function which has a return type of [String] in Swift 3

  26. 26

    Can I use a string from an array as a selector in Swift?

  27. 27

    Separating key values from a string in Swift that looks like an object array

  28. 28

    Swift 3: Remove specific string from array without knowing the indexPath?

  29. 29

    Newline and / or Carriage Return in Swift String

HotTag

Archive