【发布时间】:2016-04-01 00:24:16
【问题描述】:
所以我这里有函数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 <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'
我不明白为什么print(annotation?.photos![indexPath.row]) 会导致我的程序崩溃。我在网上阅读过,它说这是因为对象是 NSSet 但 annotation?.photo! 是 Array。所以我有点卡住了,有什么提示吗?
【问题讨论】:
-
photos是属性还是关系 - 它是如何在数据模型编辑器中定义的?
标签: ios core-data uicollectionview