Skip to content

Instantly share code, notes, and snippets.

View benvium's full-sized avatar

Ben Clayton benvium

  • www.calvium.com
  • Bristol, UK
View GitHub Profile
@benvium
benvium / update-build-number.sh
Created October 15, 2015 08:06
Script to update the build number stored in the plist file for an iOS project
#!/bin/sh
#
# SETUP INSTRUCTIONS
#-----------------------
#
# Add this file to a folder called 'scripts' at the same level as your xcodeproj file
# Open XCode
# Select Project on left-hand view
# TARGETS / your target
@benvium
benvium / indexPathAtEndOfScroll.m
Last active September 24, 2015 14:40
Which UICollectionViewCell will be visible once scrolling finishes? Below assumes a horizontally-oriented UICollectionView.
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(CGPoint *)targetContentOffset {
CGPoint point = *targetContentOffset;
// Get point in the Center of the view
CGPoint centerPoint = CGPointMake(point.x + CGRectGetWidth(scrollView.bounds) / 2, point.y);
NSIndexPath *path = [self.collectionView indexPathForItemAtPoint:centerPoint];
@benvium
benvium / generateAppIcon.sh
Last active January 12, 2021 10:52
Generate app icons and xcassets file from a single image. To use this, place script in `appname` folder inside your project (i.e. the folder that Xcode generates for you containing your source code, it's named after whatever you called the app). Create folder there called `RawImages`. Source icon should 1024x1024 and be called appIcon.png. If th…
#!/bin/bash -e
# --------------------------------------------------------
# Generate app icons and xcassets file from a single image
# Ben Clayton, Calvium Ltd.
# --------------------------------------------------------
# To use this, place script in `appname` folder inside your project (i.e. the folder that Xcode generates for you containing your source code, it's named after whatever you called the app).
# Create folder there called `RawImages`.
# Source icon should 1024x1024 and be called appIcon.png. If the icon changes, you can just run this again to regenerate everything.
# This script assumes that you have the default setup of an Images.xcassets file containing the AppIcon.appiconset.
@benvium
benvium / checkInstall.sh
Created September 22, 2015 09:15
Bash check app is installed (in this case imagemagick)
# Check imagemagick is installed
# http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script
command -v convert >/dev/null 2>&1 || { echo >&2 "I require imagemagick but it's not installed. Aborting."; exit 1; }
@benvium
benvium / sortedVisibleIndexPaths.m
Last active September 15, 2015 08:56
Get the NSIndexPaths for visible rows on a UICollectionView, sorted by row. For some reason [collectionView indexPathsForVisibleItems] returns unsorted items
// Get visible rows and sort them by row
- (NSArray *)sortedVisibleIndexPaths {
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
// visible items is NOT SORTED! lol
NSSortDescriptor *rowDescriptor = [[NSSortDescriptor alloc] initWithKey:@"row" ascending:YES];
NSMutableArray *sortedRows = [[visibleItems sortedArrayUsingDescriptors:@[rowDescriptor]] mutableCopy];
return sortedRows;
}
@benvium
benvium / podfile
Created June 2, 2015 19:57
cocoapods podfile with OCMockito that works when using a test target with 'host application' enabled (now default in XCode). The secret is (a) wrap all the app pods in the `target :App do` part (b) use pod
# ignore all warnings from all pods
inhibit_all_warnings!
target :App do
pod 'NSDate+Calendar'
pod 'MagicalRecord', '2.3.0'
pod 'Masonry'
end
target :AppTests, :exclusive => true do
@benvium
benvium / coreDataDebugging
Created June 1, 2015 20:35
Program arguments for Core Data SQLite debugging. In AppCode, edit your run configuration for your target, click the icon to the right of 'Program Arguments'. Paste in the following
-com.apple.CoreData.SQLiteIntegrityCheck
1
-com.apple.CoreData.ThreadingDebug
3
-com.apple.CoreData.SyntaxColoredLogging
1
-com.apple.CoreData.SQLDebug
1
@benvium
benvium / MagicalRecordTestCase.h
Last active August 29, 2015 14:22
Inherit TestCase classes from this to setup and tearDown a SQLite-backedMagicalRecord core data stack on each test.The SQLite file itself will be deleted between runs.Test cases should use the context provided on self.context. Note that this has taken many tries to get right! Just deleting the files sometimes resulted in the data being retained …
#import <XCTest/XCTest.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
/**
Inherit XCTest TestCase classes from this to setup and tearDown a SQLite-backed
MagicalRecord core data stack on each test.
The SQLite file itself will be deleted between runs.
@benvium
benvium / FetchResultsTableViewUpdater.h
Created May 26, 2015 14:11
Implements the boilerplate required to update a UITableView based on results from an NSFetchedResultsController
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@import CoreData;
typedef NS_ENUM(NSUInteger, FetchResultControllerMode) {
FetchResultsTableViewUpdaterModeMultipleSection,
FetchResultsTableViewUpdaterModeSingleFixedSection
};
@benvium
benvium / NSMutableDictionary+BVMNullReplacement.h
Last active August 29, 2015 14:21
In-place removal of [NSNull null] values in collections.Useful when importing JSON data into realm-cocoa. Note that the whole structure must be Mutable (e.g. all child collections are NSMutableArray or NSMutableDictionary)
@import Foundation;
// In-place removal of [NSNull null] values
// Useful when importing into Realm
@interface NSMutableDictionary (BVMNullReplacement)
- (void)bvm_removeNulls;
@end
// In-place removal of [NSNull null] values
// Useful when importing into Realm