Arrays 无法使用类型为';的参数列表调用索引;(指:任何)及#x27;
我正在制作一个新闻应用程序,你可以在其中选择你想看的主题。我遇到的问题是取消选择主题的位置。所有选定的主题都添加到名为Arrays 无法使用类型为';的参数列表调用索引;(指:任何)及#x27;,arrays,swift,core-data,swift3,Arrays,Swift,Core Data,Swift3,我正在制作一个新闻应用程序,你可以在其中选择你想看的主题。我遇到的问题是取消选择主题的位置。所有选定的主题都添加到名为ArticleSource的实体中的CoreData中,该实体的属性为source。当我尝试使用字符串title在名为Results的数组中查找主题时,会发生错误。由于我不知道主题在数组中的位置,我尝试使用index(of:)方法查找它,该方法产生错误:无法使用类型为“(of:Any)”的参数列表调用index 谢谢你的帮助 do { let appDe
ArticleSource
的实体中的CoreData中,该实体的属性为source
。当我尝试使用字符串title
在名为Results
的数组中查找主题时,会发生错误。由于我不知道主题在数组中的位置,我尝试使用index(of:)
方法查找它,该方法产生错误:无法使用类型为“(of:Any)”的参数列表调用index
谢谢你的帮助
do {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "ArticleSource")
request.returnsObjectsAsFaults = false
var results = try context.fetch(request)
if results.count > 0 {
for result in results as! [NSManagedObject] {
if let source = result.value(forKey: "source") as? String {
if source == title {
print("it matches")
if let index = results.index(of: title) {
results.remove(at: index)
}
}
print("results = \(results)")
}
}
}
} catch {
print("error")
}
do {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
try context.save()
print("SAVED")
} catch {
// error
}
do{
让appDelegate=UIApplication.shared.delegate为!appDelegate
让上下文=appDelegate.persistentContainer.viewContext
let request=NSFetchRequest(entityName:“ArticleSource”)
request.returnsObjectsAsFaults=false
var results=try context.fetch(请求)
如果results.count>0{
对于结果,结果为![NSManagedObject]{
如果让source=result.value(forKey:“source”)作为?字符串{
如果源==标题{
打印(“它匹配”)
如果let index=results.index(of:title){
结果。删除(位于:索引)
}
}
打印(“结果=\(结果)”)
}
}
}
}抓住{
打印(“错误”)
}
做{
让appDelegate=UIApplication.shared.delegate为!appDelegate
让上下文=appDelegate.persistentContainer.viewContext
尝试context.save()
打印(“已保存”)
}抓住{
//错误
}
无需通过循环结果来获取索引。你可以试试这个
var results = context.fetch(request)
if let index = results.index(where: { (result) -> Bool in
result.value(forKey: "source") == title
})
{
results.remove(at: index)
}
不需要通过循环结果来获得索引。你可以试试这个
var results = context.fetch(request)
if let index = results.index(where: { (result) -> Bool in
result.value(forKey: "source") == title
})
{
results.remove(at: index)
}
无法使用类型为“(of:X)的参数列表调用索引的可能原因
是因为类型
X
不符合equalable
可能是无法使用类型为“(of:X)的参数列表调用索引的原因。
是因为类型X
不符合equalable
在arr.index(of:)
中,元素应符合equalable
,类型X
不符合equalable
:
对于[X]
数组,请使用arr.index(其中:)
将代码更新为:
if let index = results.index(where: { $0 as? String == title }) {
print(index)
}
在arr.index(of:)
中,元素应符合equalable
,类型X
不符合equalable
:
对于[X]
数组,请使用arr.index(其中:)
将代码更新为:
if let index = results.index(where: { $0 as? String == title }) {
print(index)
}
我尝试添加您的代码,但在
result.value(forKey:“source”)==title
行中出现以下错误。错误:类型为“any”的值没有成员“Value”
任何建议?我尝试添加您的代码,但在结果.Value(forKey:“source”)==title
行上出现以下错误。错误:类型为“any”的值没有成员“Value”
有任何建议吗?