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

Swift optional Array property is immutable?
How to convert unsigned char * to byte array in cli/c++
Row and Column Indices for 2-D array in Matlab
Perl reading file, printing unique value from a column
RISC String array
How do I increment my array counter to move to the next element in the array in this code?
Powershell: Add lines to a custom objects
Dimension-wise logical indexing in a 3D array
Referencing Ranges in Arrays
Matlab Cell Array Data
Rectangle.IntersetsWith() returning true whenever the two rectangles are on the same X axis
Array with string and number
For loop to take the value of the whole array each time
Match two files by column line by line - no key
Array resize on worksheet
find max values and change others to zero

Categories

HOME
google-cloud-bigtable
winapi
scipy
jasmine
smartphone
memory-leaks
amazon-product-api
transparent
wampserver
apple-numbers
webdav
defragmentation
spring-cloud-contract
xcode8.3
unity2d
aspell
phpbb
pyyaml
aws-cognito
cvs2svn
cruisecontrol.net
apple-tv
servicemix
csh
rhmap
spring-data-neo4j
pentaho-report-designer
nesc
hana-studio
pcre
devforce
percentage
postgresql-9.2
fractals
getline
ios-ui-automation
jquery-cycle2
soundjs
jupyter-console
distributed-transactions
filepath
vcf
timesten
gd
livefyre
sapui
webdatagrid
rollback
scd
photography
hpcc
launch
infinite-scroll
ingres
gulp-typescript
system.web.ui.webcontrols
c64
icefaces
easing
mongoose-populate
soda
gradle-eclipse
nssplitview
codesign
remoteapp
date-format
inet
asp.net-mvc-2
handlebars.java
altbeacon
portfolio
except
lemoon
jenkins-scriptler
application-loader
xjc
digits
mdm-zinc
picat
uipickerviewcontroller
osx-snow-leopard
sttwitter
dwolla
pascals-triangle
vt100
cbcentralmanager
socketstream
plone-funnelweb
xmlslurper
ninject-extensions
z-machine
selectonemenu
wchar
nemerle
datamember
posting
phpcrawl
nagle
drawtext
gwt-2.2-celltable
google-instant
grooveshark
asp.net-mvc-controller
callgrind
inline-if
evb
database-diagramming
document-library

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