0

My challenge is twofold:

  1. To pick individual strings from an array of similar strings, but only if a boolean test has been passed first.

  2. "Finally" I need to concatenate any/all of the strings generated into one complete text and the entire code must be in Swift.

Illustration: A back of the envelope code for illustration of logic:

    generatedText.text =

    case Int1 <= 50 && Int2 == 50
        return generatedParagraph1 = pick one string at RANDOM from a an array1 of strings

    case Int3 =< 100
        return generatedParagraph2 = pick one string at RANDOM from a an array2 of strings

    case Int4 == 100
        return generatedParagraph3 = pick one string at RANDOM from a an array3 of strings

    ...etc

    default
        return "Nothing to report"

and concatenate the individual generatedParagraphs

Attempt: Code picks a random element within stringArray1, 2 and 3. Example of what the code returns:

---> "Sentence1_c.Sentence2_a.Sentence3_b."

PROBLEM: I need the code to ONLY pick an element if it has first passed a boolean. It means that the final concatenated string (concastString) could be empty, just contain one element, or several depending on how many of the bools were True. Does anyone know how to do this?

import Foundation

var stringArray1 = ["","Sentence1_a.", "Sentence1_b.", "Sentence1_c."]
var stringArray2 = ["","Sentence2_a.", "Sentence2_b.", "Sentence2_c."]
var stringArray3 = ["","Sentence3_a.", "Sentence3_b.", "Sentence3_c."]


let count1 = UInt32(stringArray1.count)-1
let count2 = UInt32(stringArray2.count)-1
let count3 = UInt32(stringArray3.count)-1

var randomNumberOne = Int(arc4random_uniform(count1))+1
var randomNumberTwo = Int(arc4random_uniform(count2))+1
var randomNumberThree = Int(arc4random_uniform(count3))+1

let concatString = stringArray1[randomNumberOne] + stringArray2[randomNumberTwo] + stringArray3[randomNumberThree]
KML
  • 2,302
  • 5
  • 26
  • 47

1 Answers1

0

Okay, I didn't pass a Bool, but I show concatenating three random strings from a [String]. I ran this in a playground.

import Foundation

var stringArray = [String]()

for var i = 0; i < 100; i++ {
    stringArray.append("text" + "\(i)")
}


func concat (array: [String]) -> String {
    let count = UInt32(stringArray.count)
    let randomNumberOne = Int(arc4random_uniform(count))
    let randomNumberTwo = Int(arc4random_uniform(count))
    let randomNumberThree = Int(arc4random_uniform(count))

    let concatString = array[randomNumberOne] + array[randomNumberTwo] + array[randomNumberThree]

    return concatString
}

let finalString = concat(stringArray)
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
  • Thank you for the contribution. I noticed something strange about the code when I ran it in playground. With a string of 1 element --> Uint32(stringarray.count) = 4 , with 2 elements Uint32(stringarray.count) = 5, with 3 elements = 6 etc. It overcounts by 3 all the time. However, if I run just the var stringray = ["1","2","3" etc] and the count by itself in playground, the count is correct. Why is it different inside the function, from standing alone? Does Uint32 mess with it, my computer is 64bit. – KML Sep 20 '14 at 10:55
  • hmm, I don't get that problem. I am on a 64 bit machine also. I changed the loop counter to i < 1 and it works. Or I deleted the loop and just set stringArray = ["text"] and it worked. Can you post the code you put in Playground? – Steve Rosenberg Sep 20 '14 at 18:30
  • Here I posted it as a separate question: http://stackoverflow.com/questions/25948339/why-does-uint32stringarray-count-count-wrong-in-a-function-but-correct-alone-i – KML Sep 20 '14 at 19:39
  • I have been playing around with your code for 10 hours now, but I am still struggling to figure out how to do it. I am a beginner and it seems every time I generate one set of variables that I need, they are out of scope for the next "function" where I need to use it, Did that make any sense? I need the boolean to work and so it turns into a (I think it is called) nested function. – KML Sep 20 '14 at 19:40