arrays


Changing a string into a variable in swift


I have an array is of the form
var array = ["cat1", "cat2", "cat3"]
I would like to be able to randomly choose one of these elements, where each element is the actual name of another array.
So I also have
var cat1 = ["Q1", "Q2", "Q3"] basically an array of questions.
this is a similar format for cat2 and cat3.
Now the reason I want this is to randomly choose a topic for questions, then select a question from the chosen array, my problem is I don't know how to programmatically use, say for example,
cat2[1]
I basically want to display the contents of the element that is chosen.
Is this at all possible? I have been trying to get this to play nice for a while now :(
Thank you!

type can either use a [[String]] or create a custom class for that.
To use a [[String]], first declare the arrays cat1, cat2 and cat3:
let cat1 = [...]
let cat2 = [...]
let cat3 = [...]
Then you simply do this:
let array = [cat1, cat2, cat3] // without the ""s!
Refer to this post for how to select a random item.
You basically select a random item from array, put the result into an array called randomTopic or whatever, then choose another random item from randomTopic.
Alternatively, you can create a type to store questions.
struct Topic {
let questions: [String]
}
Then you create an array of Topic i.e. [Topic] and do the same thing.

You can have an array of arrays and not Strings. For example:
var cat1 = ["Q1", "Q2", "Q3"]
var cat2 = ["Q1", "Q2", "Q3"]
var cat3 = ["Q1", "Q2", "Q3"]
var array = [cat1, cat2, cat3]
Or a two-dimensional array:
var array = [["Q1", "Q2", "Q3"], ["Q1", "Q2", "Q3"], ["Q1", "Q2", "Q3"]]
Then you can randomly choose a "line" and then the question from it:
var question = array[randomNumber][questionNumber]
Another alternative is to use a Dictionary. Then you will have this structure:
var dict = ["cat1" : ["Q1", "Q2", "Q3"],
"cat2" : ["Q1", "Q2", "Q3"],
"cat3" : ["Q1", "Q2", "Q3"]]

Why don't you use array of arrays, like
var cat1 = ["Q1", "Q2"]
var cat2 = ["Q10", "Q11"]
var cat3 = ["Q20", "Q21"]
var array = [cat1, cat2, cat3]
And chose randomly one object from array and that one object will be one of cat1, cat2 or cat3 and that represents the array of questions
Edit: Seems that these are your instance variable either initialize them in viweDidLoad if it's in view controller or use the below code
var array = [["Q1", "Q2"],
["Q10", "Q11"],
["Q20", "Q21"]]


Related Links

Enumerating in reverse with correct indices
what is “segmentation fault - invalid memory reference error” in FORTRAN
Why JSON to Array requires double JSON.parse?
Include string (the name of imported file) into numeric array in Matlab
Loop through column in Excel and add unique string to array
Looping through array contained in a hash
Why am i getting a test case wrong in coin change dynamic programming approach?
UnityScript treat Vector3 array as Collections.Array
Non contiguous ranges into monodimensional array
How to convert an array to a hash with specified common values
How can I find all the cells that have the same values in a multi-dimensional array in octave / matlab
Collecting Multiple Properties Of An Object To An Array
Sublime regex comma replace character
textscan returning empty cell array
Is there a way to initialise an array using an expression/function in R
How are these arrays being used in this Fortran algorithm?

Categories

HOME
sed
chaiscript
flyway
console
sequelize.js
avl-tree
google-spreadsheet-api
diagram
nixos
game-maker-studio-1.4
automata
symfony-forms
uiview
postmessage
grub2
movies
internet-explorer-8
statusbar
sensu
apple-tv
ghost-inspector
dspic
facebook4j
spring-test
logarithm
activepython
bus-error
datadog
invoke-command
vegan
crt
helix-3d-toolkit
smart-mobile-studio
division
body-parser
vsts-package-management
image-compression
android-preferences
common.logging
ical-dotnet
pytest-django
photon-controller
etherpad
hpcc
jcreator
emoticons
rstudio-server
mailcatcher
git-annex
radians
foxit
axes
fill
eml
sonarqube5.3
android-viewholder
risk-analysis
on-duplicate-key
rx-groovy
ytplayerview
graphical-logo
digits
crash-dumps
debian-based
robocode
scala-2.11
fragment-tab-host
java-collections-api
io.js
android-looper
kraken.js
wic
opendata
bfd
cdata
sslexception
robospice
va-list
stagefright
shiva3d
objcmongodb
lync-server-2010
enumerators
usn
tfs-power-tools
deploying
redirectstandardoutput
rubycas
audiostreamer
osx-leopard
callgrind
method-signature
dirty-data
system-codedom-compiler
database-diagramming
dotproject





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm