所以我在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类具有反多对一关系的Photos对象
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> 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'
我不明白为什么会print(annotation?.photos![indexPath.row])
崩溃我的程序.我在网上看过,它说这是因为对象是一个NSSet
但是annotation?.photo!
是一个Array
.所以我有点卡住,有什么提示吗?
我已经解决了这个问题因为在我的数据模型中我选择了CustomAnnotation实体中的照片关系是无序的,NSSet
即使在类定义中photos
变量是一个,也总会返回Array
.
我需要做的就是将Arranged设置为Ordered:
一切正常,Array
而不是NSSet