Skip to content

Instantly share code, notes, and snippets.

View lfaoro's full-sized avatar

Leonardo Faoro lfaoro

View GitHub Profile
let pb = NSPasteboard.generalPasteboard()
func parsePB() {
if let data = pb.stringForType("public.utf8-plain-text") {
print(data)
}
if let data = pb.dataForType("public.rtf") {
let rtf = try! NSAttributedString(data: data, options: [:], documentAttributes: nil)
“Challenge
Use two instances of NSDate to figure out how many seconds you have been alive.
First, NSDate has an instance method timeIntervalSinceDate:. This method takes one argument – another instance of NSDate. It returns the number of seconds between the NSDate that received the message and the NSDate that was passed in as the argument.
It looks something like this:
double secondsSinceEarlierDate = [laterDate timeIntervalSinceDate:earlierDate];
Second, you will need to create a new date object that is set to a given year, month, etc. You will do this with the help of an NSDateComponents object and an NSCalendar object. Here is an example:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:1969];
[comps setMonth:4];
@lfaoro
lfaoro / gist:b1334514b5723c0a7337
Created August 29, 2014 18:54
Challenge complete
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:20];
[comps setMonth:01];
[comps setYear:1985];
//[comps setTimeZone:<#(NSTimeZone *)#>];
NSDate *now = [NSDate date];
NSDate *birthday = [NSDate comps];
double difference = [birthday timeIntervalSinceDate:now];
@lfaoro
lfaoro / content
Created August 31, 2014 13:21
test
NSDate *now = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"yyyy-MM-dd"];
NSString *nowDate = [format stringFromDate:now];
NSLog(@"test: %@",nowDate);
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:27];
[comps setMonth:9];
[comps setYear:1988];
NSCalendar *local = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
let letters = ["0", "a", "b", "c", "d", "e", "f", "g", "h"]
let numbers = ["0", "1","2","3","4","5","6","7","8"]
func position(column: Int, row: Int) -> String {
if (column < letters.startIndex || column > letters.count) {
return "The board accepts onlynumbers from 1 to 8"
}else if (row < numbers.startIndex || row > numbers.count){
return "The board accepts onlynumbers from 1 to 8"
}
var pieceAtColumn = letters[column]
@lfaoro
lfaoro / content
Created September 10, 2014 07:47
I have all these cases that assign a lengthy subview repetitive code:
case (1, 1), (1, 8):
var value = Piece(color: .w, shape: .R)
var pieceImg = NSImage(named: "wR")
var pieceView = NSImageView(frame: CGRect(x: squareX, y: squareY, width: cellLength, height: cellLength))
pieceView.image = pieceImg
boardView.addSubview(pieceView, positioned: .Above, relativeTo: nil)
What if I assign just the:
var value = Piece(color: .w, shape: .P)
@lfaoro
lfaoro / content
Created September 10, 2014 08:44
import Cocoa
import Swift
enum Shape {
case K, Q, B, R, N, P
static let allValues = [K, Q, B, R, N, P]
}
enum Color {
case w, b
@lfaoro
lfaoro / content
Created September 10, 2014 08:55
enum Shape {
case K, Q, B, R, N, P
static let allValues = [K, Q, B, R, N, P]
}
enum Color {
case w, b
static let allValues = [w, b]
@lfaoro
lfaoro / content
Created September 10, 2014 10:25
7) a function that lists all the pieces in the configuration of step 6
@lfaoro
lfaoro / content
Created September 10, 2014 10:26
CompileSwift normal x86_64 /Users/lfaoro/Development/iLichess Mac/iLichess Mac/Chessboard.swift
cd /Users/lfaoro/Development/iLichess Mac
/Applications/Xcode-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c "/Users/lfaoro/Development/iLichess Mac/iLichess Mac/AppDelegate.swift" "/Users/lfaoro/Development/iLichess Mac/iLichess Mac/ConnectionManager.swift" -primary-file "/Users/lfaoro/Development/iLichess Mac/iLichess Mac/Chessboard.swift" "/Users/lfaoro/Development/iLichess Mac/iLichess Mac/main.swift" -target x86_64-apple-macosx10.9 -target-cpu core2 -sdk /Applications/Xcode-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -I /Users/lfaoro/Library/Developer/Xcode/DerivedData/iLichess_Mac-ddufwfmsgvbzsccxtwxcvovriyqy/Build/Products/Debug -F /Users/lfaoro/Library/Developer/Xcode/DerivedData/iLichess_Mac-ddufwfmsgvbzsccxtwxcvovriyqy/Build/Products/Debug -g -module-cache-path /Users/lfaoro/Library/Developer/Xcode/DerivedData/Mod