Ios 通过索引访问数组对象会导致-[\n NSFaultingMutableSet objectAtIndex:]:
因此,我在这里有一个函数Ios 通过索引访问数组对象会导致-[\n NSFaultingMutableSet objectAtIndex:]:,ios,core-data,uicollectionview,Ios,Core Data,Uicollectionview,因此,我在这里有一个函数collectionView:cellForItemAtIndexPath: func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentif
collectionView:cellForItemAtIndexPath
:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellID", forIndexPath: indexPath)
print("Creating collection view number: \(indexPath.row)")
// Configure the cell
cell.backgroundColor = UIColor.lightGrayColor()
let thing = Mirror(reflecting: annotation?.photos!)
print(thing.subjectType)
print(annotation?.photos![indexPath.row])
return cell
}
这是我的注释对象和照片对象的声明
class CustomAnnotation : NSManagedObject, MKAnnotation {
@NSManaged var latitude: NSNumber
@NSManaged var longitude : NSNumber
@NSManaged var title : String?
@NSManaged var subtitle: String?
@NSManaged var photos : [Photo]?
var coordinate = CLLocationCoordinate2D()
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context : NSManagedObjectContext?) {
super.init(entity: entity, insertIntoManagedObjectContext: context)
}
init(coordinate : CLLocationCoordinate2D, context: NSManagedObjectContext) {
let entity = NSEntityDescription.entityForName("CustomAnnotation", inManagedObjectContext: context)!
super.init(entity: entity, insertIntoManagedObjectContext: context)
self.longitude = coordinate.longitude
self.latitude = coordinate.latitude
print(photos)
}
func setCoordinate() {
self.coordinate.longitude = CLLocationDegrees(self.longitude)
self.coordinate.latitude = CLLocationDegrees(self.latitude)
}
}
class Photo : NSManagedObject {
@NSManaged var imagePath : String
@NSManaged var customAnnotation : CustomAnnotation
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
super.init(entity: entity, insertIntoManagedObjectContext: context)
}
init(imagePath: String, context : NSManagedObjectContext) {
let entity = NSEntityDescription.entityForName("Photo", inManagedObjectContext: context)!
super.init(entity: entity, insertIntoManagedObjectContext: context)
self.imagePath = imagePath
}
}
这就是我保存照片对象的方式,该对象与CustomAnnotation类具有反向多1关系
if (annotation?.photos)!.isEmpty {
// Load photos
print("downloading photos")
dispatch_async(dispatch_get_main_queue()) {
FlickrClient.sharedInstance().getFlickrImages(self) {(result, success, errorString) in
if success {
print("loading photos")
// Use the inverse relationship to save the data
for photoURL in result as! [String] {
let photo = Photo(imagePath: photoURL, context: self.sharedContext)
photo.customAnnotation = self.annotation!
print(photo.imagePath)
}
dispatch_async(dispatch_get_main_queue()) {
self.collectionView!.reloadData()
}
CoreDataStackManager.sharedInstance().saveContext()
}
else {
self.showAlert("Error", message: errorString!, confirmButton: "OK")
}
}
}
}
以下是它崩溃时发生的情况的日志:
Creating collection view number: 0
Optional <Array<Photo>>
2015-12-27 00:15:07.046 VirtualTourist[10413:1965722] -[_NSFaultingMutableSet objectAtIndex:]: unrecognized selector sent to instance 0x7f956d4d8cf0
2015-12-27 00:15:07.058 VirtualTourist[10413:1965722] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSFaultingMutableSet objectAtIndex:]: unrecognized selector sent to instance 0x7f956d4d8cf0'
正在创建集合视图编号:0
可选的
2015-12-27 00:15:07.046 VirtualTourist[10413:1965722]-[\n故障可变表集对象索引:]:发送到实例0x7f956d4d8cf0的无法识别的选择器
2015-12-27 00:15:07.058 VirtualTourist[10413:1965722]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\n NSFaultingMutableSet objectAtIndex:::]:未识别的选择器发送到实例0x7f956d4d8cf0'
我不明白为什么
print(annotation?.photos![indexath.row])
会使我的程序崩溃。我在网上读到过,它说这是对象是NSSet
但注释?的结果。照片
是一个数组。所以我有点卡住了,有什么提示吗?核心数据中的关系是一个无序集-您可以在错误消息[\n NSFaultingMutableSet objectAtIndex:]:无法识别的选择器中看到它<代码>\u NSFaultingMutableSet
是一个内部类,属于NSSet
集群,没有方法对象索引:
您应该首先将照片
排序到NSArray
中。我已经解决了这个问题,因为在我的数据模型中,我选择CustomAnnotation实体中的照片关系进行无序排序,即使类定义中的photos
变量是一个数组,也始终会返回NSSet
我所需要做的就是将“已安排”设置为“已安排”:
一切正常,返回的是一个数组
,而不是NSSet
,我对Swift不是很熟悉,所以我发现,像在控制台中打印对象类这样的简单任务可以引起so的讨论,并给出六个以上的答案。你能直接查看注释.照片类吗?或者这是一种官方的方法吗?我认为这是最好的方法,从我在SOIs照片上读到的内容来看,这是一种属性,或者是一种关系-它是如何在数据模型编辑器中定义的?