Skip to content

Instantly share code, notes, and snippets.

@alexshafran
alexshafran / gist:62243a7d0e24dded7b53
Last active December 17, 2017 21:12
Collections in Swift

#Collections in Swift

Arrays and dictionaries are the most common data stores in many applications. Swift has brought us a new generation of these collections, so let's take a look at how they compare to the collections found in Objective-C.

##Greatest Differences

###Typing One of the most obvious differences between collections in Objective-C and Swift is that Swift collections are explicitly typed. Typed collections are useful for many reasons, primarily because the system requires you to be precise with the content type you are working with. Any type mismatches will throw errors, which will help you avoid common mistakes.

###Mutability

@alexshafran
alexshafran / gist:8693166
Last active August 29, 2015 13:55
Property Names
#import <Foundation/Foundation.h>
@interface NSObject (PropertyNames)
+ (NSArray *)propertyNames;
+ (NSArray *)propertyNamesRecursive:(BOOL)recursive;
@end
@alexshafran
alexshafran / gist:8585597
Last active January 4, 2016 06:59
Reload TableView preserving offset
@implementation UITableView (PreserveOffset)
- (void)reloadDataPreservingOffset
{
CGSize contentSize = self.contentSize;
CGPoint contentOffset = self.contentOffset;
[self reloadData];
CGSize newContentSize = self.contentSize;
@alexshafran
alexshafran / NSAttributedString+HTML
Created January 22, 2014 05:01
NSAttributedString+HTML
@implementation NSAttributedString (HTML)
+ (NSAttributedString *)attributedStringFromHTML:(NSString *)html
{
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
return [[NSAttributedString alloc] initWithData:data
options:options
documentAttributes:nil
error:nil];
@alexshafran
alexshafran / gist:8465336
Created January 16, 2014 23:13
Fetched Results Controller Helpers
@implementation NSFetchedResultsController (Helpers)
- (NSInteger)numberOfObjects
{
return [[self.sections valueForKeyPath:@"@sum.numberOfObjects"] integerValue];
}
- (id)firstObject
{
id<NSFetchedResultsSectionInfo> obj = [self.sections firstObject];
@alexshafran
alexshafran / gist:6002322
Created July 15, 2013 18:42
Line Spacing
CGFloat multiple = 0.75;
CFIndex theNumberOfSettings = 1;
CTParagraphStyleSetting theSettings[1] = {{ kCTParagraphStyleSpecifierLineHeightMultiple, sizeof(CGFloat), &multiple }};
CTParagraphStyleRef theParagraphRef = CTParagraphStyleCreate(theSettings, theNumberOfSettings);
[string addAttribute:(id)kCTParagraphStyleAttributeName value:(__bridge id)theParagraphRef range:NSMakeRange(0, [string length])];
@alexshafran
alexshafran / Da mit
Created December 18, 2012 22:34
A second Gist
real text