Cannot Invoke 'Filter' with an Argument List of Type '((_) -> _)'

Swift: Cannot invoke 'filter' with an argument list of type '((AnyObject) throws - Bool)'

Digesting the problem down to it's minimal level, the following code, which you can run in playground, illustrates the error:

var newSimHistoryArr = [] as NSArray
var oldHistory = ["book", "bath", "table"]

newSimHistoryArr = oldHistory.filter { $0.hasPrefix("b") }

Error message is:

Playground execution failed: error: MyPlayground.playground:2:32:
error: cannot invoke 'filter' with an argument list of type '((String)
throws -> Bool)' newSimHistoryArr = oldHistory.filter {
$0.hasPrefix("b") }

Curiously the error occurs, not because of the filter expression itself, but because of the data type it is being cast to i.e. the data type of the LEFT HAND SIDE of the assignment. This is very misleading from the compiler.

Change the data type for newSimHistoryArr to a compatible type and it all works.

var newSimHistoryArr:[String]
var oldHistory = ["book", "bath", "table"]

newSimHistoryArr = oldHistory.filter { $0.hasPrefix("b") }

or even the more cumbersome form:

var newSimHistoryArr = [] as NSArray
var oldHistory = ["book", "bath", "table"]

newSimHistoryArr = (oldHistory.filter { $0.hasPrefix("b") }) as NSArray

Using filter on Swift Array gives cannot invoke filter with an argument list of type (Object) throws - Bool, but the argument doesn't throw

The error message might be misleading. The result of filter is always an array

var books = [Book]()

let fetchRequest: NSFetchRequest<Book> = Book.fetchRequest()
if let fetchResults = try? managedObjectContext.fetch(fetchRequest) {
books = fetchResults.filter { $0.title == defaultBookTitle }
}

But why not let Core Data filter the records:

var books = [Book]()

let fetchRequest: NSFetchRequest<Book> = Book.fetchRequest()
fetchRequest.predicate = NSPredicate(format:"title = %@", defaultBookTitle)
do {
books = try managedObjectContext.fetch(fetchRequest)
} catch {
print(error)
}

Cannot invoke 'filter' with an argument list of type '((_) - _)'

You can get that error if you didn't make ScriptRunner conform to Equatable:

class ScriptRunner : Equatable {
// the rest of your implementation here
}

func ==(lhs: ScriptRunner, rhs: ScriptRunner) -> Bool {
return ... // change this to whatever test that satisfies that lhs and rhs are equal
}

Cannot invoke 'stride' with an argument list of type '(from: T, to: T, by: T)' Swift

step must of type T.Stride

func generateList<T: SignedNumeric>(from: T, to: T, step: T.Stride, addLastValue: Bool = true) -> [T] where T: Comparable & Strideable {
var items = [T]()

if step == 0 || from == to {
return [from]
}

for i in stride(from: from, to: to, by: step) {
items.append(i)
}

if addLastValue && to > items.last ?? to {
items.append(to)
}

return items
}

Swift: use filter function on array of dictionaries? Error: Cannot invoke 'filter' with an argument list of type

Filtering [[String:AnyObject]] (a.k.a. Array>) results in another [[String:AnyObject]]. You're trying to assign this to a var of type AnyObject, which is not allowed in Swift 3, since arrays are structs, not objects.

Make a type-safe struct or object to hold this data, rather than a dict.

For example:

let dictArray = networkData["dicts"] as! [[String:AnyObject]]
let filteredDicts = dictArray.filter{ ($0["id"] as! String) != sample.getId() }
localData["dicts"] = filteredDicts

Cannot invoke 'CGRect.Type.init' with an argument list of type '(x: Int, y: Int, width: Float, height: Float)'

CGRect.init has three different versions, for the three types of arguments it accepts - Int, Double, and CGFloat. Whatever values you're passing into x, y, width, and height must be the same type. To fix this you might try casting your values to Double by wrapping them in Double().


Regarding your comment, there's no version of CGRect.init() that takes Float parameters. Cast your Floats to Double and it should work.

Realm: Cannot invoke 'add(_:update:)' with an argument list of type '(String)'

You are using Realm incorrectly. Realm saves object instances, each of which are of type Object. Here is reference for the class.

If you wish to save you MyClass, you could do the following,

import RealmSwift

class MyClass: Object {
@objc dynamic var id = String()
}

And, use Realm object to store the object.

let realm = try! Realm()
realm.write {
realm.add(MyClass())
}

I suggest you to go through the Getting Started section in realm.io page. And if you wish to see documentation for classes, go here



Related Topics



Leave a reply



Submit