【问题标题】:Accessing array object via index causes -[_NSFaultingMutableSet objectAtIndex:]:通过索引访问数组对象会导致 -[_NSFaultingMutableSet objectAtIndex:]:
【发布时间】: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]) 会导致我的程序崩溃。我在网上阅读过,它说这是因为对象是 NSSetannotation?.photo!Array。所以我有点卡住了,有什么提示吗?

【问题讨论】:

  • photos 是属性还是关系 - 它是如何在数据模型编辑器中定义的?

标签: ios core-data uicollectionview


【解决方案1】:

Core Data 中的关系是一个无序集合 - 您可以在错误消息 [_NSFaultingMutableSet objectAtIndex:]: unrecognized selector 中看到它。 _NSFaultingMutableSet 是一个内部类,属于NSSet 集群,没有方法objectAtIndex:

您应该首先将photos 排序为NSArray

【讨论】:

  • 我对 Swift 不是很熟悉,所以在控制台中打印对象类这样简单的任务可能会引发超过六个答案的 SO' 讨论,这对我来说是一个启示。可以直接查看annotation.photos 类吗?或者 Mirror() 是官方的做法吗?
  • 我认为这是我在 SO 上读到的最好的方法
【解决方案2】:

我已经解决了这个问题,因为在我的数据模型中,我选择了 CustomAnnotation 实体中的照片关系为无序,即使在类定义中 photos 变量是 Array,也始终返回 NSSet .

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2018-06-21
    • 2010-10-22
    相关资源
    最近更新 更多