Skip to content
Browse files

CWLinkedList object subscripting

  • Loading branch information...
1 parent 9263490 commit 7ad92668f29821a735cb79a3e4070bc812170080 @Machx committed Sep 16, 2012
Showing with 29 additions and 0 deletions.
  1. +6 −0 Source/CWLinkedList.h
  2. +23 −0 Source/CWLinkedList.m
View
6 Source/CWLinkedList.h
@@ -92,4 +92,10 @@ THE SOFTWARE.
*/
-(void)enumerateObjectsWithBlock:(void(^)(id object, BOOL *stop))block;
+//Objective-C Object Subscripting
+
+-(id)objectAtIndexedSubscript:(NSUInteger)index;
+
+-(void)setObject:(id)object atIndexedSubscript:(NSUInteger)idx;
+
@end
View
23 Source/CWLinkedList.m
@@ -243,6 +243,29 @@ -(BOOL)hasErrorForObjectAtIndex:(NSUInteger)index
return NO;
}
+-(id)objectAtIndexedSubscript:(NSUInteger)index
+{
+ return [self objectAtIndex:index];
+}
+
+-(void)setObject:(id)object atIndexedSubscript:(NSUInteger)idx
+{
+ if ([self hasErrorForObjectAtIndex:idx]) {
+ return;
+ }
+
+ NSUInteger current = 0;
+ CWLinkedListNode *currentNode = self.head;
+
+ while (current != idx) {
+ currentNode = currentNode.next;
+ current++;
+ }
+
+ currentNode.data = object;
+}
+
+
-(id)objectAtIndex:(NSUInteger)index
{
if ([self hasErrorForObjectAtIndex:index]) {

0 comments on commit 7ad9266

Please sign in to comment.
Something went wrong with that request. Please try again.