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

traversing tables with selenium/webdriverjs
Printing cell array
Print a 5by5 matrix in MIPS using 1 dimensional array
Running Time of built in functions
Sum of matrices in 3-dimensional object of variable dimesions without looping
Quickly select arrays which have at least k elements common with master array
In Swift, how efficient is it to append an array?
How to correctly split a signal in matlab?
Accessing array of numbers in ARM (with Komodo compilation)
Load, show, convert image from byte array (database) in Windows Phone 8.1
Assembly - Copying an array of bytes - MIPS
Deep Query using MongoDB
plot array from matrix matlab [closed]
Function similar to head() in Matlab
Matlab, Symbolic functions,
R .Fortran return reshaped array?

Categories

HOME
python
batch-processing
payment-gateway
jsviews
raspbian
uibutton
windows-store-apps
survival-analysis
h2
heap-memory
javamail
ll
defragmentation
value
java-home
data-science-experience
vsftpd
codeblocks
criteria
oclint
getorgchart
nixos
xul
informatica-powercenter
jacoco
sql-tuning
alfresco-share
arm-template
off-canvas-menu
aptana
linq-to-sql
propel2
jconsole
flexboxgrid
epicor
onsen-ui
nsmenuitem
k2
remove-method
rworldmap
shopping-cart
guile
lync-2013
spring-data-neo4j
thinking-sphinx
roundcube
geopy
data-extraction
redhat-datavirt
initializer
fltk
cross-entropy
bayesian-networks
nsexception
homekit
mod-fcgid
rdw
range-v3
data-integration
windows-firewall
google-maps-ios
jexl
fedora20
httplib2
infinite-scroll
mercury
sqldatareader
slam-algorithm
radians
selecteditem
fill
acm
test-class
plone-3.x
getrusage
persistence.xml
oai
fabric-twitter
js-cookie
anti-patterns
visual-studio-code
built-in
ubercart
bridge
cpu-cores
uos
stackframe
lexicographic
cloudpebble
microblaze
odftoolkit
livechat
firebug-lite
valence
picat
codeigniter-a3m
help-viewer
opensocial
svcutil.exe
dynamic-proxy
cling
bfd
convex-polygon
cdata
robospice
soa-suite
process-monitor
snapjs
lync-server-2010
android-4.0
dropdownlistfor
crocodoc
osi
supersized
webkit-transform
serp
adsl
nvelocity
communicationexception
graph-layout
odbc-sql-server-driver

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