Skip to content

Instantly share code, notes, and snippets.

@The0racle
Last active September 3, 2021 22:42
Show Gist options
  • Save The0racle/3b6c0e9d8c73c4b1727605117af5e213 to your computer and use it in GitHub Desktop.
Save The0racle/3b6c0e9d8c73c4b1727605117af5e213 to your computer and use it in GitHub Desktop.
NSFetchedResultsController extension for safely retrieving objects without raising 'NSInternalInconsistencyException'.
//
// NSFetchedResultsController+SafeObjectAtIndexPath.swift
// This extension prevents the 'no object at index in section at index' exception.
//
// Copyright (c) 2016 Thiago Pereira
// MIT License, http://www.opensource.org/licenses/mit-license.php
import Foundation
extension NSFetchedResultsController{
/// Check whether provided indexPath is valid.
///
/// - note: This method checks if self.section is greater than indexPath.section
/// and if number of object in indexPath.section is greater than indexPath.row
///
/// - parameter indexPath: indexPath to be checked.
///
/// - returns: Whether indexPath has a value associated to it.
private func hasObject(at indexPath : NSIndexPath) -> Bool{
guard let sections = self.sections where sections.count > indexPath.section else {
return false
}
let sectionInfo = sections[indexPath.section]
guard sectionInfo.numberOfObjects > indexPath.row else {
return false
}
return true
}
/// Check whether the object exists and returns it if true.
///
/// - parameter indexPath: Indexpath of object.
///
/// - returns: Object at indexPath if exists. Nil otherwise.
func exceptionFreeObject(at indexPath : NSIndexPath) -> AnyObject?{
guard self.hasObject(at: indexPath) else {
return nil
}
return self.objectAtIndexPath(indexPath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment