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: 0Optional <Array<Photo>> 2015-12-27 00:15:07.046 VirtualTourist[10413:1965722] -[_NSFaultingMutableSet objectAtIndex:]: unrecognized selector sent to instance 0x7f956d4d8cf02015-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!是一个阵列.所以我有点卡住,有什么提示吗?
解决方法 我已经解决了,因为在我的数据模型中我选择了CustomAnnotation实体中的照片关系是无序的,即使在类定义中照片变量是一个数组,也总是返回NSSet.我需要做的就是将Arranged设置为Ordered:
一切正常,返回一个Array而不是NSSet
总结以上是内存溢出为你收集整理的ios – 通过索引访问数组对象导致 – [_ NSFaultingMutableSet objectAtIndex:]:全部内容,希望文章能够帮你解决ios – 通过索引访问数组对象导致 – [_ NSFaultingMutableSet objectAtIndex:]:所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)