Persistent Data Structures for Objective-C持久化 Objective-C 的结构体
该项目是用于持久化 Objective-C 的结构体的工具,支持 Vector, Set 和 HashMap 数据结构。
下面代码演示如何序列化 Vector 结构:
#import "AAPersistentVector.h" #import "AATransientVector.h" #import "AAIIterator.h" /// Initialization AAPersistentVector *vector1 = [[AAPersistentVector alloc] init]; // But if you need a persistent empty vector, better use the pre-created empty one. AAPersistentVector *vector2 = [AAPersistentVector empty]; // Or you can load an array into it during initialization: AAPersistentVector *vector = [[AAPersistentVector alloc] initWithArray:@[@1, @2, @3]]; /// CRUD operations [vector objectAtIndex:1]; // => @2 vector = [vector addObject:@4]; // => vector with @1, @2, @3, @4 vector = [vector replaceObjectAtIndex:2 withObject:@5]; // => vector with @1, @2, @5, @4 vector = [vector removeLastObject]; // => vector with @1, @2, @5 /// Convert to NSArray [vector toArray]; // => @[@1, @2, @5] /// Iterating // With Fast Enumeration for (id value in vector) { // going to iterate through values with `value` = @1, @2 and @5 } // With Iterator id<AAIIterator> iterator = [vector iterator]; while (iterator) { [iterator first]; // => @1, @2 and @5 iterator = [iterator next]; } // With each (fastest way) [vector each:^(id value) { // going to iterate through values with `value` = @1, @2 and @5 }]; /// Transients // Efficiently add 100 objects to the vector. Will be ~10 times faster than // adding without transient [vector withTransient:^(AATransientVector *transient) { for (NSUInteger i = 0; i < 100; i += 1) { transient = [transient addObject:@(i)]; } return transient; }]; // You can also do that without a block, if you want AATransientVector *transientVector = [vector asTransient]; for (NSUInteger i = 0; i < 100; i += 1) { transientVector = [transientVector addObject:@(i)]; } vector = [transientVector asPersistent];
评论