Skip to content

Instantly share code, notes, and snippets.

//this takes a list of items in an array, then runs an observable on each of them, finally it combines
//all of the outputs into one observable which returns all of the media items at once
Observable.combineLatest(mediaItems.map({ $0.getMediaMetadataObservable() })).subscribe(onNext: { a in
for item in a {
print (item.type)
}
})
@MylesCaley
MylesCaley / get datasource element.swift
Created April 17, 2017 15:29
RxDataSources - get data
func collectionView(_ collectionView: UICollectionView, heightForAnnotationAtIndexPath indexPath: IndexPath, withWidth width: CGFloat) -> CGFloat {
let x = dataSource?[indexPath]
let font = UIFont(name: "OpenSans", size: 11)!
let rect = NSString(string: x?.brief ?? "").boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
// if multiple types
@MylesCaley
MylesCaley / blink-cursor.swift
Created March 15, 2017 20:40
blink cursor in textfield without showing keyboard
// there are cases where you just need to update the display and not the data
// for example if you change the constants on a constraint
self!.tableView?.beginUpdates()
self!.tableView?.endUpdates()
@MylesCaley
MylesCaley / find.swift
Created January 5, 2017 18:52
find an object with certain property in swift
let index = updates.index(where: {$0.id == self.prototypeUpdate?.id})
@MylesCaley
MylesCaley / Animation Tutorial Notes
Last active December 30, 2016 16:07
notes from animation tutorials
Fixes for animation tutorial
@MylesCaley
MylesCaley / weakclosure.swift
Created December 8, 2016 14:10
pattern for weak closure
{ [weak self] (_) in
guard let _ = self else { return }
self!.
}
@MylesCaley
MylesCaley / DynamicHeaderSizeForAutolayout.swift
Last active December 29, 2021 03:30
size the top header dynamically for autolayout after values have been set
// MAKE SURE YOU HAVE TOP TO BOTTOM CONSTRAINTS SET ON ALL OF THE OBJECTS IN THE HEADER VIEW!!!
// better as an extension
func sizeHeaderToFit() {
if let tableViewHeader = self.tableHeaderView {
let headerView = tableViewHeader
headerView.setNeedsLayout()
headerView.layoutIfNeeded()
@MylesCaley
MylesCaley / DynamicRxTableHeader.swift
Created November 29, 2016 14:53
Creating a dynamic header with multiple sections and multiple items
//section model file
import RxDataSources
enum CommentTableSectionModel {
case section(comment: Comment, items: [CommentTableSectionModelItem])
}
extension CommentTableSectionModel: SectionModelType {
@MylesCaley
MylesCaley / LoadViewFromXIB.swift
Created November 29, 2016 14:49
Load a view from an XIB
extension UIView {
class func loadFromNibNamed(nibNamed: String, bundle : Bundle? = nil) -> UIView? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiate(withOwner: nil, options: nil)[0] as? UIView
}
}