当前位置:  开发笔记 > IOS > 正文

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

如何解决《通过索引访问数组对象会导致-[_NSFaultingMutableSetobjectAtIndex:]:》经验,为你挑选了1个好方法。

所以我在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 > 

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.所以我有点卡住,有什么提示吗?



1> YellowPillow..:

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

在此输入图像描述

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

在此输入图像描述

一切正常,Array而不是NSSet

推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有