所以我在这里有函数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",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(注释?.photos![indexPath.row])崩溃我的程序.我在线阅读,它说这是对象是NSSet但是注释的结果?.photo!是一个阵列.所以我有点卡住,有什么提示吗?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。