Swift 3 - matching user input with dictionary values [closed] - swift

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
The user of my app will be tested on their english. They will select different words from a list by placing a check mark next to the word. They will want to ONLY select the words that are nouns. If, for example, they choose 5 out of 10 correctly, how do I show them a score of 50%. I think that I need to filter dictionary values based on the user's input. The user's input being an array. What is the best way to code this?

Try the following code:
let myDictionary : [String : Any] =
[ "Nouns":["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"],
"Verbs":["Eat","play","dance","walk","run","sing","read","write","go","come"]]
var myUserSelectedArray:[String] = ["One","come","Three","go","Five","x","Six","x","x","Ten"];
let myNounArray = myDictionary["Nouns"] as? [String];
let myVerbArray = myDictionary["Verbs"] as? [String];
let set1:Set<String> = Set(myUserSelectedArray);
let set2:Set<String> = Set(myNounArray!);
let set3:Set<String> = Set(myVerbArray!);
let scroeInNoun = set1.intersection(set2).count;
let scroeInVerb = set1.intersection(set3).count;
print ("score in Verb \((scroeInVerb * 100)/set2.count) %")
print ("score in noun \((scroeInNoun * 10)/set3.count ) %")
let finalScore = (scroeInNoun + scroeInVerb) * 100 / ((myNounArray?.count)! + (myVerbArray?.count)!)
print ("final score in noun \(finalScore) %")

Related

Swift (SwiftUI) programming syntax operator meaning? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I don't know how to word the question to find help in a search engine. Basically I'm trying to figure out the meaning of this operation
self.isFromCurrentUser = fromId == Auth.auth().currentUser?.uid
The variables are NOT the question because I already know about Firebase and whatnot. Rather the question is why is there an assignment operator = and an == in this line of code?
variable1 = variable2 == variable3
What does this mean? What is variable1 getting assigned to?
The statement
variable1 = variable2 == variable
assigns variable1 the value of the boolean (true or false) of the result of variable2 == variable3 (if they are equal).
It's equivalent to
if variable2 == variable3 {
variable1 = true
} else {
variable1 = false
}

Print repetition in Swift while statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In my while statement, I cannot understand why my output is printed twice ?
I would like to print i only one time, where is my error ?
func fetch2(){
var i: Int = 0
while i <= (self.returned-1) {
let itemLookUp = "https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemLookup?appid=\(self.appId)&itemcode=\(self.arrayCodeProduct[i])&responsegroup=large"
print(i)
i = i+1
}
}
Here is the output that I obtain :
0
1
2
3
0
1
2
3
Thank you in advance.
It looks like fetch2() is called twice.
Add a print(#function) before you var i and check that fetch2() is not called several times.

Round Double value in swift with addition before decimal [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i have double value 70514.94971385633, now I want it to round the value and make it 70515.00. I have tried command rounded(), but it only rounds value after the decimal. How I can round value after the decimal and add the nearest value to a number before decimal? this is my code for rounding the value,
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.rounded()
but it shows result 70514.95, i want it to add it before decimal value like 70515.00
Just small change in your code
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.round()
or
let roundedValue = totalBidValue.rounded(.up)
Use ceil(_:) to get that working,
let value = 70514.94971385633
let result = ceil(value)
print(result) //70515.0
Use round()
let myDoubleValue = 70514.94971385633
let roundedOffValue = round(myDoubleValue)
print(roundedOffValue) // 70515.0
Your code is almost perfect...
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.rounded(.toNearestOrAwayFromZero)
just add .toNearestOrAwayFromZero in your code

Display only some digits of an Int [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Let's say you have an Int.
let int = 12345
I want it to only display some of the digits.
For example: print(firstTwoDigits) --> 12
How do I do this and thank you in advance.
It depends on your specific requirements.
This prints the first two digits of an integer number
let intVal = 12345
print(String(intVal).prefix(2))
Output: 12
Another way which only prints certain ones in the number:
let intVal = 12345
let acceptableValues = ["1", "2"]
let result = String(intVal).filter {
acceptableValues.contains(String($0))
}
print(result)
Output: 12

insert array values into an empty dictionary [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have an array = [14, 1087, 28474]. how am I suppose to make it like [fantasy, action, animation] where 14 = fantasy , 1087 = action and so on. I want to show only the string representation of those value to my ios label. Is there any way i can do that. Should I make an empty dictionary and if so then how am i suppose to generate the keys for those values inside the arrays
You haven't said what you have already tried, but a dictionary is probably the way to go. If you make the dictionary a var, you can add to it later. If you don't need to change it, define it as let as #Leo suggests
var dict : [Int : String] = [
14 : "fantasy",
1087 : "action",
28474 : "animation"
]
let thisNumber = 1087
let thisString = dict[thisNumber]
You can create a dictionary literal if your final goal is just to have a mapping from known numbers to known genres.
let dictionary = [14: "fantasy", 1087: "action", 28474: "animation"]
if you need to build the dictionary and add to it at run time you can loop through the array and add values to the dictionary with this syntax:
dictionary[22] = "genre"