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

How to use an OR clause to filter an array in Swift
Why accessing this json sub elements by index raises this error?
How to apply WorksheetFunction.Average to a dynamic array in VBA
Getting array length in mongodb?
using vb.net and LibTiff to convert large tiff images
Storing the output of spap2 into an array then looping and graphing that output
Using intermediate variable to work with array (reference type)
Swift Creating tableView From JSON
How how convert normal string into multi-line string
How to convert a double to double array?
Use one index from array
ko.observableArray not UI not UPdating.
Creating collection of different enums
Determine Winter or Summertime due to month (UTC Timezone) [duplicate]
UDF if value is in array - Type Mismatch from a prior answer - IsInArray
matrix and vector to 3-d array R

Categories

HOME
python
security
symfony
app-inventor
project-management
oauth
amazon-swf
webrtc
uibutton
phaser-framework
xtext
codeblocks
wordpress-theming
internet-explorer-8
zend-framework-mvc
plupload
http-method
sumo
hanami
vertex-buffer
invoke-command
bxslider
dhtmlx-scheduler
devforce
referenceerror
helix-3d-toolkit
lego
mmdrawercontroller
mms
vmd
declare
mediawiki-extensions
html-agility-pack
paho
filepath
nsjsonserialization
recycle-bin
webdatagrid
cgcontext
udev
etherpad
cdk
rsqlite
verbose
sql-import-wizard
cjson
children
multiple-file-upload
boost-hana
pagekit
fps
asynccallback
cctray
throughput
schtasks.exe
file-diffs
datasnap
uialertview
team-build
jython-2.7
magento-1.12
word-2013
ctrlp
chaining
kallithea
left-recursion
hidden-field
libssh2
spdy
operations
visual-c++-2010-express
healthvault
sim900
cilk-plus
.net-cf-3.5
ftp4j
multiple-conditions
grunt-contrib-compass
sharpmap
qtgui
database-permissions
wpdb
gdataxml
trailing-slash
linkedhashset
web-safe-fonts
tfs-power-tools
die
hobo
ruby-debug
nagle
rijndael
out-of-browser
delegatecommand
microsoft.ink
outlook-form

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