Do how to know if variable return function is an array or not ? Example: In my Presenter I have this function:
func filterGnomosForName(name:String) -> [Gnomo]{
let res = listGnomos.filter { ($0.name?.lowercased().contains(name.lowercased()))!}
return res
}
And MyTestClass I have this test function:
func testFilterGnomo(){
let result = listPresenter.filterGnomosForName(name: "Nam")
XCTAssert(result == [Gnomo])
}
Gnomo is a type object in array , but only want know if is array or not for know if the funcion is correctly or not , help me?
When I am asserting an objects type I do this:
XCTAssert((object as Any) is Array)
I've not asserted an array before but I think the above would work.
Here is an example I just created in a Swift Playground:
import UIKit
import XCTest
class MyTestClass: XCTestCase {
func testSomething() {
let myArray: [String] = ["foo", "bar"]
XCTAssert((myArray as Any) is String) // Fails
XCTAssert((myArray as Any) is [String]) // Passes
}
}
MyTestClass.defaultTestSuite().run()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With