@interface NSMutableArray (Shuffling)
- (void)shuffle;
@end
// NSMutableArray_Shuffling.m
#import "NSMutableArray_Shuffling.h"
@implementation NSMutableArray (Shuffling)
- (void)shuffle
{
NSUInteger count = [self count];
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
NSInteger nElements = count - i;
NSInteger n = arc4random_uniform(nElements) + i;
[self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
@end
Thursday, May 22, 2014
Shuffling NSMutableArray
Wednesday, May 21, 2014
Key-Value Observation
Key-Value Observation:
====================
// MyClass1.h:
@interface MyClass1 : NSObject
@property (nonatomic, copy) NSString* value;
@end
// MyClass2.m:
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
NSLog(@"I heard about the change!");
}
// Somewhere else entirely:
MyClass1* objectA = [MyClass1 new];
MyClass2* objectB = [MyClass2 new];
// register for KVO
[objectA addObserver:objectB forKeyPath:@"value" options:0 context:nil];
// change the value in a KVO compliant way
objectA.value = @"Hello, world!";
// result: objectB's observeValueForKeyPath:... is called
--------------------------------------------------------------------
objectA.value = @"Hello";
[objectA addObserver:objectB forKeyPath:@"value" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context: nil];
objectA.value = @"Goodbye"; // notification is triggered
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
id newValue = change[NSKeyValueChangeNewKey];
id oldValue = change[NSKeyValueChangeOldKey];
NSLog(@"The key path %@ changed from %@ to %@",
keyPath, oldValue, newValue);
}
--------------------------------------------------------------------
====================
// MyClass1.h:
@interface MyClass1 : NSObject
@property (nonatomic, copy) NSString* value;
@end
// MyClass2.m:
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
NSLog(@"I heard about the change!");
}
// Somewhere else entirely:
MyClass1* objectA = [MyClass1 new];
MyClass2* objectB = [MyClass2 new];
// register for KVO
[objectA addObserver:objectB forKeyPath:@"value" options:0 context:nil];
// change the value in a KVO compliant way
objectA.value = @"Hello, world!";
// result: objectB's observeValueForKeyPath:... is called
--------------------------------------------------------------------
objectA.value = @"Hello";
[objectA addObserver:objectB forKeyPath:@"value" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context: nil];
objectA.value = @"Goodbye"; // notification is triggered
- (void) observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
id newValue = change[NSKeyValueChangeNewKey];
id oldValue = change[NSKeyValueChangeOldKey];
NSLog(@"The key path %@ changed from %@ to %@",
keyPath, oldValue, newValue);
}
--------------------------------------------------------------------
Wednesday, May 14, 2014
Device UDID
NSString* uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; // IOS 6+
NSLog(@"UDID:: %@", uniqueIdentifier);
Subscribe to:
Comments (Atom)