gpt4 book ai didi

ios - 通过索引访问数组对象会导致 -[_NSFaultingMutableSet objectAtIndex :]:

转载 作者:可可西里 更新时间:2023-11-01 03:48:49 30 4
gpt4 key购买 nike

所以我在这里有函数 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]) 会使我的程序崩溃。我在网上看到它说这是对象是 NSSetannotation?.photo!Array 的结果。所以我有点卡住了,有什么提示吗?

最佳答案

我已经解决了它,因为在我的数据模型中,我选择了 CustomAnnotation 实体中的照片关系是无序的,即使在类定义中 photos,也总是返回 NSSet 变量是一个数组

enter image description here

我需要做的就是将 Arranged 设置为 Ordered:

enter image description here

一切正常,返回一个 Array 而不是 NSSet

关于ios - 通过索引访问数组对象会导致 -[_NSFaultingMutableSet objectAtIndex :]:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34471735/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com