错误:找不到接受提供的参数的“标题”的重载 [英] Error: Could not find overload for 'title' that accepts the supplied arguments
问题描述
当前,我有一个名为 Folder
的NSManaged对象的子类,其属性为<$ c $ item $ c> NSSet 。我有一个返回 NSMutableArray
的函数,我试图在表视图中显示数据(然后重新排列表中显示的顺序)。
Currently I have a subclass of NSManaged object called Folder
with property called item
that is of type NSSet
. I have a function to return a NSMutableArray
and I am trying to display the data in a tableview (and then rearrange the order displayed in the table).
class Folder: NSManagedObject {
@NSManaged var title: String
@NSManaged var date: NSDate
@NSManaged var item: NSSet
func itemMutableArray() -> NSMutableArray {
return NSMutableArray(array: (item.allObjects as! [Checklist]).sorted{ $0.date.compare($1.date) == NSComparisonResult.OrderedAscending } )
}
TableViewController:
TableViewController:
var itemMutableArray: NSMutableArray!
itemMutableArray = folder.itemMutableArray()
cell.textLabel?.text = itemMutableArray[indexPath.row].title //Error
不幸的是,使用此功能时,这会在我的表视图中返回错误。
Unfortunately this returns an error in my tableview when using this function.
错误找不到接受提供的参数的'title'的重载
Error could not find overload for 'title' that accepts the supplied arguments
最终我正在尝试什么要实现的是显示数据并移动单元格以更改NSSet的顺序。
Ultimately what I am trying to achieve is to display the data and move the cells around to change the order of the NSSet.
table.didMoveCellFromIndexPathToIndexPathBlock = {(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) -> Void in
let delegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = delegate.managedObjectContext!
context.deleteObject(self.itemArray[indexPath!.row] as NSManagedObject )
self.itemArray.exchangeObjectAtIndex(toIndexPath.row, withObjectAtIndex: fromIndexPath.row)
}
PS:最初我有个函数可以返回对象数组但是我无法修改NSSet的顺序,因为它们没有顺序。
PS: Originally I had a function to return an array of the object but I was unable to modify the order of the NSSet as they are not ordered.
func itemArray() -> [Item] {
let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
return item.sortedArrayUsingDescriptors([sortDescriptor]) as! [Item]
}
有人对我目前的错误之处有任何建议吗?
Does anybody have any suggestions with where I am currently going wrong ?
推荐答案
表达式 itemMutableArray [indexPath.row] .title
是因为您已键入 itemMutableArray
作为NSMutableArray,所以 itemMutableArray [indexPath.row]
是AnyObject。因此,Swift没有理由相信此东西具有 title
属性。您需要将其强制转换为具有 title
属性的 (尽管如果可能的话,最好完全避免使用NSMutableArray,这样会更好) ,因为Swift数组是可变的。)
The problem with the expression itemMutableArray[indexPath.row].title
is that, because you have typed itemMutableArray
as an NSMutableArray, itemMutableArray[indexPath.row]
is an AnyObject. Thus, Swift has no reason to believe that this thing has a title
property. You need to cast it to something that does have a title
property (though it would be much better to avoid the use of NSMutableArray completely, if possible, since a Swift array is mutable).
这篇关于错误:找不到接受提供的参数的“标题”的重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!