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

Splitting a list of words from a text file into an array in BASH
How to read text file to matrix with specific column size and rows size in matlab
AngularJS double Array ng-repeat
SoapUI: Count Nodes Returned in JSON Array Response
Getting custom array contents back from Parse and sending them back to Parse
How to pass &:key as an argument to map instead of a block with ruby?
Unbounded array of CGfloat
Looking for a good alternative to arrays of controls
How can I split string by same sequence of symbols using REGEX?
Saving output in the form of an array
Integrating an array using scipy.integrate.quad and nquad “Does not return valid float”
counting names in a list
ArrayHelper with labels for specific data from query
Rectangle intersection in Ruby
Check if value exists in postgres array for partitioning via check constraint
How to process large text files more efficiently with Perl [duplicate]

Categories

HOME
converter
batch-processing
symfony
answer-set-programming
sed
ubuntu-16.04
decorator
visualization
windows-store-apps
chaiscript
rfc
specflow
branch.io
xtext
ext.net
spring-cloud-contract
onenote-api
goutte
desktop
diagram
click
dryioc
ctypes
event-log
netezza
uicollectionview
movies
ios10.3
ghost-inspector
maximo
fetch-api
mangodb
percentage
consumer
geomesa
google-guava-cache
text-files
redhat-datavirt
socketpair
issue-tracking
topic-modeling
socketscan
sendgrid-templates
intersystems-ensemble
resuming-training
prototypejs
istorage
jgraph
shutdown
dwarf
slidesjs
selenium-firefoxdriver
polar-coordinates
festival
createobject
iostat
rstudio-server
dts
unsatisfiedlinkerror
qregexp
boost-hana
contour
fps
financial
qt-linguist
asp.net-mvc-2
xmp
function-fitting
unity5.3
js-cookie
achartengine
vine
sparse-file
git-checkout
code-first-migrations
cpu-speed
fragment-tab-host
ghostdoc
firebug-lite
emma
argument-passing
unison
visual-c++-2010-express
gamepad
confusion-matrix
sql-server-2012-web
swrl
mstsc
grunt-contrib-compass
fieldset
arbor.js
abnf
socketstream
gdt
text-services-framework
visual-web-gui
opengl-es-lighting
smooth
carbide
oggvorbis
asp.net-mvc-controller
integer-promotion

Resources

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
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App