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 retrieve a VB Control instance, given its Name and Index?
Create arrays in chef attributes then fill in values in template .erb file
Why it is {“Index was outside the bounds of the array.”}with my code with no error in it
How to manually convert an integer into an IP address in Ruby
Taking last word of an string, variable not declared?
Separate string into array and then filter it [duplicate]
Protractor: How to store number text from div element
Weird results with parsing array from json in Swift3
How can I declare an array with diffrent type of data?
Trying to append content to numpy array
MongoDb $addFields inside arrays that $multiply two values within the array
bash: sed search and replace the path of array elements
Scala way for converting Long to ArrayByte
How to Assign Multidimensional Array Values to Tableview Cell swift 3
bring MC to stage with button
Extension file when called in not refreshing (swift3)

Categories

HOME
xbox-live
laravel-5
sas
plsql
ibm-watson-cognitive
jenkins-plugins
tinyos
vhdl
puzzle
wmic
uibutton
google-contacts
aem
formal-verification
playframework
docker-swarm
transparent
apollo
value
devstack
spring-cloud-contract
docker-windows
nodemailer
requirejs
eclipse-cdt
finite-group-theory
abcpdf
informatica-powercenter
webmethods
phoenix
rapidjson
bootstrap-popover
ng-show
internet-explorer-8
device-detection
esql
embedly
nmf
npm-install
croppic
branch
mef2
fetch-api
stocks
getline
ifstream
salesforce-chatter
node-horseman
jquery-cycle2
bing-maps-api
office365connectors
xamarin.uitest
android-download-manager
topic-modeling
concrete5-5.7
magiczoomplus
polyfills
stdclass
amd
timesten
walmart-electrode
drawingarea
jenkins-jira-trigger
mouseclick-event
text-classification
iphone-developer-program
search-box
gmt
qos
project-template
atlassian-crowd
iostat
hspec
django-filer
qregexp
gradle-eclipse
gmsmapview
picking
django-south
asynccallback
multinomial
asymptote
vips
abas
ipojo
calibration
ffserver
bridge
musl
orientation-changes
moai
temp-tables
planetary.js
wss
codeigniter-a3m
dvcs
logentries
angulartics
jqmobi
dig
entity-framework-4.1
carddav
mailcore
android-contextmenu
gdataxml
code-conversion
deploying
mpmovieplayer
shim
supersized
entity-framework-ctp5
qtembedded
authenticode
audiostreamer
pureftpd
aio
plinq
swfloader
revision
document-library

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile