Skip to content

Instantly share code, notes, and snippets.

View oddmagne's full-sized avatar

Odd Magne Hågensen oddmagne

View GitHub Profile
@cprovatas
cprovatas / Data+PrettyPrint.swift
Created May 23, 2018 15:52
Pretty print JSON string from Data in Swift 4.1 (especially useful printing to Xcode console)
import Foundation
extension Data {
var prettyPrintedJSONString: NSString? { /// NSString gives us a nice sanitized debugDescription
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }
return prettyPrintedString
}
@ukiyoevega
ukiyoevega / ViewHierarchy.swift
Last active November 10, 2022 11:54
print view hierarchy in Swift
func printAllSubviews(of view: UIView, from layer: Int) {
for _ in 0..<layer {
print(" ", separator: "", terminator: "")
}
print("\(view): ")
let subView = view.subviews
if subView.count != 0 {
subView.forEach({ (view) in
printAllSubviews(of: view, from: layer + 1)
})
@simonbromberg
simonbromberg / ListFonts.m
Last active September 6, 2023 03:35
List all fonts available on iOS device in console
// List all fonts on iPhone
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
for (NSString *family in familyNames) {
NSLog(@"Family name: %@", family);
fontNames = [UIFont fontNamesForFamilyName: family];
for (NSString *font in fontNames) {
NSLog(@" Font name: %@", font);
}
@khanlou
khanlou / Fonts.swift
Created October 6, 2016 21:10
Print all fonts in Swift 3
UIFont.familyNames.forEach({ familyName in
let fontNames = UIFont.fontNames(forFamilyName: familyName)
print(familyName, fontNames)
})
func usesAMPM() -> Bool {
let locale = NSLocale.currentLocale()
let dateFormat = NSDateFormatter.dateFormatFromTemplate("j", options: 0, locale: locale)!
if dateFormat.rangeOfString("a") != nil {
return true
}
else {
return false
}
}
// Would you rather use this:
NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: button, attribute: .Trailing, multiplier: 1, constant: 0).active = true
// or this?
label.constrain(.Leading, .Equal, to: button, .Trailing, plus: 20)
@licvido
licvido / app.swift
Created March 9, 2015 20:57
SWIFT: UITableView sections example
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var itemsInSections: Array<Array<String>> = [["1A", "1B", "1C"], ["2A", "2B"], ["3A", "3B", "3C", "3D", "3E"]]
var sections: Array<String> = ["Section 1", "Section 2", "Section 3"]
@jbergen
jbergen / get-fonts
Created November 18, 2014 20:38
Print all available font families and font names (Swift)
for family in UIFont.familyNames() {
let sName: String = family as String
println("family: \(sName)")
for name in UIFont.fontNamesForFamilyName(sName) {
println("name: \(name as String)")
}
}
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
//how to set identifier for an unwind segue:
//1. in storyboard -> documento outline -> select your unwind segue
//2. then choose attribute inspector and insert the identifier name
if ([@"UnwindFromSecondView" isEqualToString:identifier]) {
return [[MyCustomUnwindSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
}else {
@Duraiamuthan
Duraiamuthan / Unique identifiers
Created April 5, 2014 09:31
UDID vs UUID vs GUID
UUID:
It is the acronym of Universally Unique Identifier.
A sequence of 128 bits that can guarantee uniqueness across space and time, defined by RFC 4122.
GUID:
It is the acronym of Globally Unique Identifier
It is Microsoft's implementation of the UUID specification; often used interchangeably with UUID.
In dot net framework its called as Plain GUID and in sql server its called as newid
UDID: