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 make array distinct
Swap arrays in Ruby
Quick way to copy elements in a 2D array
It's better to use array or object to store structured data?
Morton Encoding Z-indexing Space Usage
How to Remove Duplicate values in array in c#
Actionscript, hasOwnProperty() of multidimensional arrays
Labview : numbering color box array
Replace values of one column of matrix size 1*n to another matrix of size n*n
How to query a single embedded document in an array in MongoDB?
Pushing Sound files to an Array with Swift
Alamofire : Updating array's values with JSON's data
Issues with arrays, how would i go about solving this?
Traverse through an array with missing elements and report those missing elements in shell
Is it possible to send an object to a Chunk instead of an array
Need help on a Twig issue with associative arrays

Categories

HOME
rest
websphere
antivirus
ruby-on-rails-3
travis-ci
zend-framework2
desktop
google-classroom
unity2d
cakephp-2.9
intellij-plugin
gwtp
jquery-terminal
dendrogram
plupload
cvs2svn
epicor
url.action
spring-test
auditing
opencart2.3
lync-2013
dpi
winscp
android-vpn-service
exponential
receipt
qtablewidget
mapguide
ios-ui-automation
boilerplate
inkscape
discord.net
active-model-serializers
s3cmd
brute-force
ical-dotnet
dojox.mobile
email-parsing
openweathermap
onmouseover
vcf
ajp
seamless-immutable
mouseclick-event
livescribe
spell-checking
struts-layout
redux-router
festival
vici
instruments
database-backups
shipitjs
clob
asynccallback
function-fitting
file-diffs
selendroid
p-np
textkit
js-cookie
google-hadoop
google-plus-one
magento-1.12
obfuscar
cpu-speed
node-inspector
collabnet
ogr2ogr
libssh2
aquafold
ifft
jquery-tabs
sim900
gamepad
layered
onselect
gridworld
multiple-conditions
xslkey
sametime
phpsh
android-4.0
wchar
handwriting
printing-web-page
phpcrawl
asp.net-routing
surefire
yajl
communicationexception
chdatastructures

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