Skip to content

Instantly share code, notes, and snippets.

@niwatako
niwatako / CodePiece.swift
Created February 15, 2016 09:10
これはバグじゃないの!? swift の protocol extension による デフォルト実装と文脈による優先順位について #CodePiece
// A とそのサブクラス B が、"プロパティnameを持つ" SomeProtocolに準拠することを宣言。
class A {}
class B: A {}
protocol SomeProtocol { var whatYourName: Void { get } }
extension A: SomeProtocol {}
// Aにnameのデフォルト実装を用意
extension SomeProtocol where Self: A {
var whatYourName: Void { print("My name is A") }
}
// BはAを継承していますが、更にデフォルト実装を上書きします
@niwatako
niwatako / CodePiece.swift
Created February 15, 2016 05:24
extension で親子別々の実装にしたい: Playgroundでは行けた( `・ω´・) #CodePiece
protocol SomeProtocol {
var name: String { get }
}
class A {}
class B: A {}
extension A: SomeProtocol {}
extension SomeProtocol where Self: A {
var name: String { return "My name is A" }
}
@niwatako
niwatako / CodePiece.swift
Created February 15, 2016 05:23
extension で親子別々の実装にしたい: Playgroundでは行けた( `・ω´・) 実際のコードでB().name がAになってしまうのだけど、型の認識のされ方の問題? #CodePiece
protocol SomeProtocol {
var name: String { get }
}
class A {}
class B: A {}
extension A: SomeProtocol {}
extension SomeProtocol where Self: A {
var name: String { return "My name is A" }
}
@niwatako
niwatako / CodePiece.swift
Created December 1, 2015 03:02
Int なの、NSNotFoundなの? #CodePiece
class hoge {
var currentIndex: Int = NSNotFound
}
var statusCode = 200
if 400..<500 ~= statusCode {
print("400台ダヨ")
} else {
print("知らないヨ")
}
var statusCode = 200
switch statusCode {
case 400..<500 :
print("400台ダヨ")
default:
print("知らないヨ")
}
@niwatako
niwatako / CodePiece.m
Created August 5, 2015 14:59
WKWebViewのエラー処理は ユーザー操作によるキャンセルの error.code -999 を無視するべし。 #CodePiece
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
if (error && !([error.domain isEqualToString:NSURLErrorDomain] && error.code == NSURLErrorCancelled)) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Error : %ld",(long)error.code]
message:error.localizedDescription
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:nil]];
// 続けるボタン
@niwatako
niwatako / CodePiece.m
Created August 5, 2015 14:32
違った RT @niwatako : isEqual と == は別物? #CodePiece #CodePiece
NSLog(@"%d:%d:%d",(error.domain == NSURLErrorDomain), [error.domain isEqual:NSURLErrorDomain], [error.domain isEqualToString:NSURLErrorDomain] );
// 0:1:1
@niwatako
niwatako / CodePiece.m
Created August 5, 2015 13:34
つかってみた #CodePiece #CodePiece
NSLog(@"Hello!");
@niwatako
niwatako / 0_reuse_code.js
Last active August 29, 2015 14:13
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console