Skip to content

Instantly share code, notes, and snippets.

Craig Marvelley craigmarvelley

View GitHub Profile
View NSObject+RateLimiting.m
- (NSMutableDictionary *)throttleActionHash
{
NSMutableDictionary *throttleData = objc_getAssociatedObject(self, THROTTLE_ACTION_HASH_PROPERTY_KEY);
if (!throttleData) {
throttleData = [[NSMutableDictionary alloc] init];
objc_setAssociatedObject(self, THROTTLE_ACTION_HASH_PROPERTY_KEY, throttleData, OBJC_ASSOCIATION_RETAIN);
}
return throttleData;
View migration1.m
// Assume we have an NSPersistentStoreCoordinator which has been initialised with our entity model, and
// an NSURL which points to our store's location on disk
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeUrl options:options error:&error];
NSManagedObjectModel *destinationModel = coordinator.managedObjectModel;
BOOL storeIsCompatible = [destinationModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata];
@craigmarvelley
craigmarvelley / progress.m
Last active Apr 26, 2017
NSProgress/UIProgressView example
View progress.m
// Create a progress bar view
UIProgressView *progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progressView.progress = 0;
// Assume we have a progress object
NSProgress *progress = [self progressForOngoingTask];
// Observe the 'fractionCompleted' property and update the progress view as it changes
[self.KVOController observe:progress keyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew block:^(id _Nullable observer, id _Nonnull object, NSDictionary<NSString *, id> *_Nonnull change) {
dispatch_async(dispatch_get_main_queue(), ^{
View UIActivityControllerExample.m
NSURL *pathToPdfFile;
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[ result ] applicationActivities:nil];
NSMutableArray *excludedActivities = [NSMutableArray array];
NSNumber *emailOutEnabled = [self.userPreferences.featureFlags valueForKeyPath:@"research.share.email"];
if (!emailOutEnabled.boolValue) {
[excludedActivities addObject:UIActivityTypeMail];
}
View prepare-for-segue-inject.m
- (void)interceptSeguesToPerformDependencyInjection {
NSError *error;
id<AspectToken> DIAspectToken = [UIViewController aspect_hookSelector:@selector(prepareForSegue:sender:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> aspectInfo, UIStoryboardSegue *segue, id sender) {
UIViewController *destinationController = segue.destinationViewController;
[self.UIAssembly inject:destinationController];
View typhoon-example-3.m
- (void)showNoteSplitViewWithDataController:(DataController *)dataController searchController:(SearchController *)searchController {
NSParameterAssert(dataController);
NSParameterAssert(searchController);
NSString *authenticatedUserId = self.applicationSessionCredentialStore.userId;
NoteSplitViewAssembly *assembly = [self.assembly noteSplitViewAssemblyWithApplicationSession:self.applicationSession dataController:dataController searchController:searchController authenticatedUserId:authenticatedUserId];
[assembly activate];
View typhoon-example-2.m
@interface CoordinatorAssembly
@property (strong, nonatomic) DependencyOne *existingInjectedDependency;
- (DependencyTwo *)newDependency;
- (MyModel *)modelThatIsInjectedWithBothDependencies;
@end
View typhoon-example.m
@implementation ExampleAssembly
- (MyModel *)model {
return [TyphoonDefinition withClass:[MyModel class]
configuration:^(TyphoonDefinition* definition) {
[definition useInitializer:@selector(initWithDependency:)
parameters:^(TyphoonMethod *initializer) {
[initializer injectParameterWith:[self exampleDependency]];
View objection-example.m
JSObjectionInjector *injector = [JSObjection createInjector];
[injector bindBlock:^(JSObjectionInjector *context) {
MyModel *model = [[MyModel alloc] initWithDependency:context.getObject([Dependency class])];
return model;
} toClass:[MyModel class]];
id myModel = [injector getObject:[MyModel class]];
View blindside-example.m
@implementation MyViewController
+ (BSInitializer *)bsInitializer {
return [BSInitializer initializerWithClass:self
selector:@selector(initWithApi:)
argumentKeys:@"myApi", nil];
}
- (instancetype)initWithApi:(MyApi *)myApi {
...
You can’t perform that action at this time.