From 3bf34f33dcaabc241108a251d98aae0c97906730 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Wed, 19 Aug 2015 20:56:50 +0900 Subject: [PATCH] added utilities for ListMonitor to optionally extract objects with potentially invalid indexes/indexPaths --- CoreStore/Observing/ListMonitor.swift | 50 ++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/CoreStore/Observing/ListMonitor.swift b/CoreStore/Observing/ListMonitor.swift index f4aa4ad..0528fb1 100644 --- a/CoreStore/Observing/ListMonitor.swift +++ b/CoreStore/Observing/ListMonitor.swift @@ -72,36 +72,84 @@ public final class ListMonitor { // MARK: Public /** - Accesses the object at the given index within the first section. This subscript indexer is typically used for `ListMonitor`s created with `addObserver(_:)`. + Accesses the object at the given index within the first section. This subscript indexer is typically used for `ListMonitor`s created with `monitorList(_:)`. - parameter index: the index of the object. Using an index above the valid range will throw an exception. + - returns: the `NSManagedObject` at the specified index */ public subscript(index: Int) -> T { return self[0, index] } + /** + Returns the object at the given index, or `nil` if out of bounds. This subscript indexer is typically used for `ListMonitor`s created with `monitorList(_:)`. + + - parameter index: the index for the object. Using an index above the valid range will return `nil`. + - returns: the `NSManagedObject` at the specified index, or `nil` if out of bounds + */ + public subscript(safeIndex index: Int) -> T? { + + return self[safeSectionIndex: 0, safeItemIndex: index] + } + /** Accesses the object at the given `sectionIndex` and `itemIndex`. This subscript indexer is typically used for `ListMonitor`s created with `monitorSectionedList(_:)`. - parameter sectionIndex: the section index for the object. Using a `sectionIndex` with an invalid range will throw an exception. - parameter itemIndex: the index for the object within the section. Using an `itemIndex` with an invalid range will throw an exception. + - returns: the `NSManagedObject` at the specified section and item index */ public subscript(sectionIndex: Int, itemIndex: Int) -> T { return self[NSIndexPath(forItem: itemIndex, inSection: sectionIndex)] } + /** + Returns the object at the given section and item index, or `nil` if out of bounds. This subscript indexer is typically used for `ListMonitor`s created with `monitorSectionedList(_:)`. + + - parameter sectionIndex: the section index for the object. Using a `sectionIndex` with an invalid range will return `nil`. + - parameter itemIndex: the index for the object within the section. Using an `itemIndex` with an invalid range will return `nil`. + - returns: the `NSManagedObject` at the specified section and item index, or `nil` if out of bounds + */ + public subscript(safeSectionIndex sectionIndex: Int, safeItemIndex itemIndex: Int) -> T? { + + guard let sections = self.fetchedResultsController.sections + where sectionIndex < sections.count else { + + return nil + } + + let section = sections[sectionIndex] + guard itemIndex < section.numberOfObjects else { + + return nil + } + return sections[sectionIndex].objects?[itemIndex] as? T + } + /** Accesses the object at the given `NSIndexPath`. This subscript indexer is typically used for `ListMonitor`s created with `monitorSectionedList(_:)`. - parameter indexPath: the `NSIndexPath` for the object. Using an `indexPath` with an invalid range will throw an exception. + - returns: the `NSManagedObject` at the specified index path */ public subscript(indexPath: NSIndexPath) -> T { return self.fetchedResultsController.objectAtIndexPath(indexPath) as! T } + /** + Returns the object at the given `NSIndexPath`, or `nil` if out of bounds. This subscript indexer is typically used for `ListMonitor`s created with `monitorSectionedList(_:)`. + + - parameter indexPath: the `NSIndexPath` for the object. Using an `indexPath` with an invalid range will return `nil`. + - returns: the `NSManagedObject` at the specified index path, or `nil` if out of bounds + */ + public subscript(safeIndexPath indexPath: NSIndexPath) -> T? { + + return self[safeSectionIndex: indexPath.section, safeItemIndex: indexPath.item] + } + /** Returns the number of sections */