I'm using the MongoKitten library to fetch documents from mongoDB.
I have the following document in my mongoDB:
{
...
foo: ["A", "B"]
...
}
I can query the db, but I can't loop through the foo array of the returned documents. For instance, let's say that I save the results of my query into mongoDocs.
for Doc in mongoDocs {
print(Doc["foo"] as Any) // prints ["A", "B"]
var tempFoos = [String]()
for foo in Doc["foo"] { // gives error: Type 'Value' does not conform to protocol "Sequence"
print("Foo: " + foo)
tempFoos.append(foo)
}
}
I understand the error. Basically, my foo array doesn't conform to the Sequence protocol that allows me to loop over it. But how do I fix that?
Edit - Here's the code I'm using to fetch the mongoDocs. I've printed the results and used other properties from them. I just can't seem to iterate through this array.
mongoDocs = try self.geographiesCollection!.find(matching: q, projecting: projection, limitedTo: 100)
Here's the relevant function in the MongoKitten source code. The function returns Cursor<Document>
Here you can check out how a framework dev explained handling this situation. MongoKitten closed issue 27
here are some quotes from his explanation incase the link becomes invalid.
"MongoKitten BSON library always returns an enum (Value) when subscripting a document."
"A BSON array is really just a document with keys from 0 to x, so the enum case for array has a document as it's associated value. Because Value can also be, say, a double or a date, it doesn't conform to the Sequence protocol.
The easiest way to iterate over the array is by using the document convenience accessor on Value. This returns the underlying document if Value is either an array or document, or an empty document if it's something else. You can then iterate like this:"
for (key, val) in doc["vals"].document {
print("Value is \(val)")
}
Convert it into an array:
for Doc in mongoDocs {
guard let arr = Doc["foo"] as? [String] else { continue }
for foo in arr {
// do your things
}
}
I think you're using BSON 3. Value is an enum with multiple cases, which you can see here. Here's my stab at it:
for doc in mongoDocs {
guard case Value.array(let fooArray)? = doc["foo"] {
fatalError("doc[\"foo\"] is not an array")
}
print(fooArray)
}
Related
I've looked at other subscript issues here and I don't think they match my problem. I have a dictionary of dictionaries - Dictionary[String:Dictionary[String:String]]
In an extension I want to loop through all the values (Dictionary[String:String] and retrieve one of the values.
So I wrote this:
for dictNEO in Array(self.values) {
print(dictNEO)
print(type(of: dictNEO))
print(dictNEO["approachDate"])
}
and am getting this error on the last print line: Value of type 'Value' has no subscripts
Here's the first two print lines:
["nominalDist": "\"13.58 ", "approachDate": "\"2020-Feb-01 08:18 ± < 00:01\"", "minimumDist": "\"13.58 ", "diameter": "\"92 m - 210 m\"", "name": "\"(2017 AE5)\""]
Dictionary<String, String>
So I am confused as to why it is telling me it has no subscripts when it sees the type of as a Dictionary.
You have written this as an extension to Dictionary if I understand you correctly and that means that self is generic and defined as Dictionary<Key, Value> and not to you specific type so in your for loop you are looping over an array of [Value].
So you need to typecast Value before accessing it as a dictionary
if let dictionary = dictNEO as? [String: String] {
print(dictNEO["approachDate"])
}
but since it makes little sense to have an extension to Dictionary where you access a specific key it would be better to write it as a function. Since the dictionary is well defined now there is no issue with the last print
func printValuesForSubKey(_ key: String, _ dict: [String: [String: String]]) {
for (dictNEO) in dict.values {
print(dictNEO)
print(type(of: dictNEO))
print(dictNEO[key])
}
}
Note, I don't have an explanation why type(of:) recognises it as [String: String]
The code snippet doesn't work because values property is a collection of collections and with Array(values) you create a collection of collection of collections. In short, instead going down the code goes up and creates new collection level.
Solution with a Higher order function map:
self.values.map { print(type(of: $0)); $0["approachDate"] }
Solution with For-In Loop
for dictNEO in self.values {
print(dictNEO)
print(type(of: dictNEO))
print(dictNEO["approachDate"])
}
I am new to coding and I needed some help with answering this challenge.
Instructions:
// Given the two arrays below, write a function that takes a String as an input parameter and returns a Boolean value. The function should return true if the String input is in either array and it should return false if the String input is in neither array.
//
// Examples:
// Call your function and pass in the String "cat" as the input. Your function should return true
// Call your function and pass in the String "cow" as the input. Your function should return false
let array1 = ["dog", "cat", "bird", "pig"]
let array2 = ["turtle", "snake", "lizard", "shark"]
// Write your function below:
Here is where I have written my function:
func test(Animal:String){
let list = array1 + array2
for animal in list {
if list == animal {
print("true")
} else {
print("false")
}
}
}
test(Animal: "dog")
The error I am getting is:
Binary operator '==' cannot be applied to operands of type '[String]' and 'String under the if statement.
Help if you can and I apologize in advance if this is not formatted correctly.
Thanks!
Just a tip, in the future you should try adding a more descriptive question title, that is super vague and could mean anything.
Anyways your issue is with the line:
if list == animal {
The error you got is quite specific and tells you exactly what's going wrong. [String] is an array of strings while String is just a single item. So when you write for animal in list your taking one animal in that list at a time.
Comparing an animal to a list is not allowed. They are not the same type ([String] and String are different types, as the compiler error told you). You can only compare variables of the same type. So in your case a list (String array aka [String]) to a another list, or an animal (String) to an animal.
Now what you want to do is see if a string you got is in either array. There's actually a built in method in arrays that lets you do exactly this: .contains
func test(animal:String){
let list = array1 + array2
if list.contains(animal) {
print("true")
} else {
print("false")
}
}
or if you want to be extra concise with some good code you can try
func test(animal:String){
let list = array1 + array2
print(list.contains(animal)) // will be false if it doesn't so prints false, true if it does so prints true.
}
And another side note.. You want to be very careful doing an if else inside of a for loop where both the if and the else return. You shouldn't be making decisions after looking at only ONE element.
Because typically you want to check EVERY value before printing false, but when you have an if else, you'll return/print after checking ONLY the first value.
if you wanted to do it your way (no built in methods, iterating through the array) you would want to do something like this:
func test(animal:String){
let list = array1 + array2
for candidate in list {
if animal == candidate { // there's a match! we can return true
print("true ")
return // return says to exit the function since we no longer need to keep going this is appropriate
}
} // for loop ends
// we only return false once the ENTIRE for loop has run, because we know if it did exist, we would never get to this point in the code
// this part only runs once EVERY item has been checked and none were a match
print("false")
}
And finally as bilal says, never start variable names with capital letters. Typically capital letters mean its a type (like String). You'll notice I renamed Animal -> animal for you in my examples
This question already has answers here:
For-in loop and type casting only for objects which match type
(2 answers)
Closed 6 years ago.
I'd like to know if it is possible to iterate over an array is swift and check if the current element has a certain type.
A use case would be : I have an array with NSDictionarys and NSArrays and I ony want to take care of the NSDictionarys.
I'd like to do this without the condition inside the loop :
for entity in array
{
if entity is NSDictionary
{
// Do something
}
}
I'm looking for something like :
for (entity in array) as? NSDictionary
{
// Do something
}
Of course I know that what I have just written doesn't work, but I guess you can get the idea of what I'm asking for.
Any idea would be really appreciated !
Solution 1
You can filter your array
let things = [NSDictionary(), NSArray(), NSDictionary(), NSArray()]
let dicts = things.flatMap { $0 as? NSDictionary }
Now dicts is defined as [NSDictionary] an contains only the 2 dictionaries.
Solution 2
You can also perform a for loop only on the values that are dictionaries
let things = [NSDictionary(), NSArray(), NSDictionary(), NSArray()]
for dict in things.flatMap( { $0 as? NSDictionary }) {
}
Using filter with the type check operator is
As an alternative to the flatMap solutions, you may use filter along with the type check operator is:
let arr: [AnyObject] = [NSDictionary(), ["foo", "bar"], NSDictionary(), "foo"]
for dict in arr.filter({ $0 is NSDictionary }) {
// do something with dictionary
}
Use the type check operator (is) to check whether an instance is of
a certain subclass type. The type check operator returns true if the
instance is of that subclass type and false if it is not.
From Swift Language Guide - Type Casting. Since you don't have to worry about subclass matches (you know the elements to be either NSDictionary or NSArray) the is filter approach can be an appropriate alternative here.
Note that when using the flatMap and type cast operator (as), the iterate element in the for loop (dict) will be of type NSDictionary, whereas the filter approach above will perform no downcasting, hence preserving the NSDictionary in its wrapped form, as AnyObject. Probably the flatMap approach is to prefer here.
If you don't care for elements to have the same indexes as in original array, you can flatMap to the required type.
for element in array.flatMap({ $0 as? NSDictionary }) {
// do something
}
I would like to learn and use more functional programming in Swift. So, I've been trying various things in playground. I don't understand Reduce, though. The basic textbook examples work, but I can't get my head around this problem.
I have an array of strings called "toDoItems". I would like to get the longest string in this array. What is the best practice for handling the initial nil value in such cases? I think this probably happens often. I thought of writing a custom function and use it.
func optionalMax(maxSofar: Int?, newElement: Int) -> Int {
if let definiteMaxSofar = maxSofar {
return max(definiteMaxSofar, newElement)
}
return newElement
}
// Just testing - nums is an array of Ints. Works.
var maxValueOfInts = nums.reduce(0) { optionalMax($0, $1) }
// ERROR: cannot invoke 'reduce' with an argument list of type ‘(nil, (_,_)->_)'
var longestOfStrings = toDoItems.reduce(nil) { optionalMax(count($0), count($1)) }
It might just be that Swift does not automatically infer the type of your initial value. Try making it clear by explicitly declaring it:
var longestOfStrings = toDoItems.reduce(nil as Int?) { optionalMax($0, count($1)) }
By the way notice that I do not count on $0 (your accumulator) since it is not a String but an optional Int Int?
Generally to avoid confusion reading the code later, I explicitly label the accumulator as a and the element coming in from the serie as x:
var longestOfStrings = toDoItems.reduce(nil as Int?) { a, x in optionalMax(a, count(x)) }
This way should be clearer than $0 and $1 in code when the accumulator or the single element are used.
Hope this helps
Initialise it with an empty string "" rather than nil. Or you could even initialise it with the first element of the array, but an empty string seems better.
Second go at this after writing some wrong code, this will return the longest string if you are happy with an empty string being returned for an empty array:
toDoItems.reduce("") { count($0) > count($1) ? $0 : $1 }
Or if you want nil, use
toDoItems.reduce(nil as String?) { count($0!) > count($1) ? $0 : $1 }
The problem is that the compiler cannot infer the types you are using for your seed and accumulator closure if you seed with nil, and you also need to get the optional type correct when using the optional string as $0.
I want to check if there is a value in a array and if so assign to a String using a if-left statement:
if let scoreValue = scoreValueArray[element!]{
// do something with scoreValue
}
Error: Bound value in a conditional binding must be of optional type
So tried changing the ! to ? but error persists.
Any input appreciated.
scoreValueArray is an array of strings, where a String value is appended to array if a condition is met, then array is saved to NSUserdefaults.
So element is a int which corresponds to a index in the array, bt only if the index is occupied with a String, so
scoreValueArray[element!]
could return an 'Index out of bounds', hence want to use the if-let.
Although the accepted answer clearly puts why optional binding is not available in the current implementation, it doesn't provide with a solution.
As it is shown in this answer, protocols provide an elegant way of safely checking the bounds of an array. Here's the Swift 2.0 version:
extension Array {
subscript (safe index: Int) -> Element? {
return indices ~= index ? self[index] : nil
}
}
Which you can use like this:
let fruits = ["Apple", "Banana", "Cherry"]
if let fruit = fruits[safe: 4] {
// Do something with the fruit
}
It's not clear what type your scoreValueArray is, but for the sake of this answer, I'm going to assume it's an array of Int.
var scoreValueArray: Array<Int>
Now, if we look the definition of the Array struct, we'll find this:
struct Array<T> : MutableCollectionType, Sliceable {
// other stuff...
subscript (index: Int) -> T
// more stuff
}
So, calling the subscript method on our array (which is what we do when we say scoreValueArray) returns a non-optional. And non-optionals cannot be used in the conditional binding if let/if var statements.
We can duplicate this error message in a more simple example:
let foo: Int = 3
if let bar = foo {
// same error
}
This produces the same error. If we instead do something more like the following, we can avoid the error:
let foo: Int? = 3
if let bar = foo {
// perfectly valid
}
This is different from a dictionary, whose subscript method does return an optional (T?). A dictionary will return a value if the key passed in the subscript is found or nil if there is no value for the passed key.
We must avoid array-index-out-of-bounds exceptions in the same way we always do... by checking the array's length:
if element < scoreValueArray.count {
scoreValue = scoreValueArray[element]
}