Replace substring of NSAttributedString with another NSAttributedString

Convert your attributed string into an instance of NSMutableAttributedString. The mutable attributed string has a mutableString property. According to the documentation: “The receiver tracks changes to this string and keeps its attribute mappings up to date.” So you can use the resulting mutable string to execute the replacement with replaceOccurrencesOfString:withString:options:range:.

Comparing NSDates without time component

Use this Calendar function to compare dates in iOS 8.0+ func compare(_ date1: Date, to date2: Date, toGranularity component: Calendar.Component) -> ComparisonResult passing .day as the unit Use this function as follows: let now = Date() // “Sep 23, 2015, 10:26 AM” let olderDate = Date(timeIntervalSinceNow: -10000) // “Sep 23, 2015, 7:40 AM” var order … Read more

Dictionary wrong order – JSON

To post what has already been said in comments: Dictionaries are “unordered collections”. They do not have any order at all to their key/value pairs. Period. If you want an ordered collection, use something other than a dictionary. (an array of single-item dictionaries is one way to do it.) You can also write code that … Read more

Catching NSException in Swift

Here is some code, that converts NSExceptions to Swift 2 errors. Now you can use do { try ObjC.catchException { /* calls that might throw an NSException */ } } catch { print(“An error ocurred: \(error)”) } ObjC.h: #import <Foundation/Foundation.h> @interface ObjC : NSObject + (BOOL)catchException:(void(^)(void))tryBlock error:(__autoreleasing NSError **)error; @end ObjC.m #import “ObjC.h” @implementation ObjC … Read more