Last active
August 21, 2017 06:05
-
-
Save kingcos/3279293fbf476506f2b9167223991958 to your computer and use it in GitHub Desktop.
Swifter Tips - 2 - Notes: http://www.jianshu.com/p/85dbf5a524e8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// 通过类型取出某个实例方法的签名,再传入实例调用相应的方法 | |
class ClassA { | |
func method(_ number: Int) -> Int { | |
return number + 1 | |
} | |
} | |
// func1: (ClassA) -> (Int) -> Int | |
let func1 = ClassA.method | |
// let func1 = { (obj: MyClass) in obj.method } | |
let obj1 = ClassA() | |
let rst1 = func1(obj1)(1) | |
print(rst1) | |
// OUTPUT: | |
// 2 | |
class ClassB { | |
func method(_ number: Int) -> Int { | |
return number + 1 | |
} | |
class func method(_ number: Int) -> Int { | |
return number | |
} | |
} | |
// func2: (Int) -> Int | |
let func2 = ClassB.method | |
let func3: (Int) -> Int = ClassB.method | |
let func4: (ClassB) -> (Int) -> Int = ClassB.method | |
print(func2(1)) | |
print(func3(1)) | |
let obj2 = ClassB() | |
print(func4(obj2)(1)) | |
// OUTPUT: | |
// 1 | |
// 1 | |
// 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@objc protocol OptionalProtocol { | |
@objc optional func optionalMethod() | |
func necessaryMethod() | |
} | |
protocol SwiftProtocol { | |
func methodOpA() | |
func methodNsB() | |
func methodOpC() | |
} | |
extension SwiftProtocol { | |
func methodOpA() { | |
print(#function) | |
} | |
func methodOpC() { | |
print(#function) | |
} | |
} | |
class ClassA: SwiftProtocol { | |
func methodOpA() { | |
print(#function) | |
} | |
func methodNsB() { | |
print(#function) | |
} | |
} | |
class ClassB: OptionalProtocol { | |
func necessaryMethod() { | |
print(#function) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
func method(_ num: UnsafePointer<CInt>) { | |
print(num.pointee) | |
} | |
func method(_ num: UnsafeMutablePointer<CDouble>) { | |
num.pointee = 1.5 | |
print(num.pointee) | |
} | |
var a: CInt = 123 | |
method(&a) | |
var b: CDouble = 3.14 | |
method(&b) | |
// 指针内容和实际值转换 - 不安全 | |
let arr = NSArray(object: "meow") | |
let str = unsafeBitCast(CFArrayGetValueAtIndex(arr, 0), to: CFString.self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class MyClass { | |
var a = 1 | |
deinit { | |
print(#function) | |
} | |
} | |
var pointer: UnsafeMutablePointer<MyClass>! | |
pointer = UnsafeMutablePointer<MyClass>.allocate(capacity: 1) | |
pointer.initialize(to: MyClass()) | |
print(pointer.pointee.a) | |
pointer.deinitialize() | |
pointer.deallocate(capacity: 1) | |
pointer = nil | |
// OUTPUT: | |
// 1 | |
// deinit | |
var x: UnsafeMutablePointer<tm>! | |
var t = time_t() | |
time(&t) | |
x = localtime(&t) | |
x = nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
let date = NSDate() | |
let name: AnyClass! = object_getClass(date) | |
print(name) | |
let type = type(of: date) | |
print(type) | |
// OUTPUT: | |
// __NSDate | |
// __NSDate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ClassA: NSObject {} | |
class ClassB: ClassA {} | |
let obj1: NSObject = ClassA() | |
let obj2: NSObject = ClassB() | |
obj1.isKind(of: ClassA.self) | |
obj2.isMember(of: ClassA.self) | |
class ClassC {} | |
class ClassD: ClassC {} | |
let obj3: AnyObject = ClassC() | |
let obj4: AnyObject = ClassD() | |
obj3.isKind(of: ClassC.self) | |
obj4.isMember(of: ClassC.self) | |
if obj4 is ClassC { | |
print("obj4 is ClassC") | |
} | |
if obj4 is ClassD { | |
print("obj4 is ClassD") | |
} | |
// OUTPUT: | |
// obj4 is ClassC | |
// obj4 is ClassD | |
// Warning | |
//let str = "" | |
//if str is String { | |
// print("str is a string.") | |
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
typealias Task = (_ cancel: Bool) -> Void | |
func delay(_ time: TimeInterval, task: @escaping () -> ()) -> Task? { | |
func dispatch_later(block: @escaping () -> ()) { | |
let t = DispatchTime.now() + time | |
DispatchQueue.main.asyncAfter(deadline: t, execute: block) | |
} | |
var closure: (() -> Void)? = task | |
var result: Task? | |
let delayedClosure: Task = { cancel in | |
if let internalClosure = closure { | |
DispatchQueue.main.async(execute: internalClosure) | |
} | |
closure = nil | |
result = nil | |
} | |
result = delayedClosure | |
dispatch_later { | |
if let delayedClosure = result { | |
delayedClosure(false) | |
} | |
} | |
return result | |
} | |
class ClassA: NSObject { | |
dynamic var date = Date() | |
} | |
private var myContext = 0 | |
class ClassB: NSObject { | |
var obj: ClassA! | |
override init() { | |
super.init() | |
obj = ClassA() | |
print("Init ClassA - Date: \(obj.date)") | |
obj.addObserver(self, | |
forKeyPath: "date", | |
options: .new, | |
context: &myContext) | |
_ = delay(3) { | |
self.obj.date = Date() | |
} | |
} | |
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | |
if let change = change, context == &myContext { | |
let newDate = change[.newKey] | |
print("Date changed - New Date: \(newDate ?? "nil")") | |
} | |
} | |
} | |
let obj = ClassB() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
func local(_ closure: () -> ()) { | |
closure() | |
} | |
local { | |
// Code goes here... | |
} | |
local { | |
// Code goes here... | |
} | |
do { | |
// Code goes here... | |
} | |
do { | |
// Code goes here... | |
} | |
let sth: UILabel = { | |
let label = UILabel() | |
// Setup label... | |
return label | |
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class Drinking { | |
typealias LiquidColor = UIColor | |
var color: LiquidColor { | |
return .clear | |
} | |
class func drinking(_ name: String) -> Drinking { | |
var drinking: Drinking | |
switch name { | |
case "Coke": | |
drinking = Coke() | |
case "Beer": | |
drinking = Beer() | |
default: | |
drinking = Drinking() | |
} | |
return drinking | |
} | |
} | |
class Coke: Drinking { | |
override var color: LiquidColor { | |
return .black | |
} | |
} | |
class Beer: Drinking { | |
override var color: LiquidColor { | |
return .yellow | |
} | |
} | |
let coke = Drinking.drinking("Coke") | |
coke.color | |
let beer = Drinking.drinking("Beer") | |
beer.color | |
print(NSStringFromClass(type(of: coke))) | |
print(NSStringFromClass(type(of: beer))) | |
// OUTPUT: | |
// __lldb_expr_5.Coke | |
// __lldb_expr_5.Beer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Demo-Bridging-Header.h | |
// #import <CommonCrypto/CommonCrypto.h> | |
// Swift | |
extension String { | |
var MD5: String { | |
var digest = Array<UInt8>(repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH)) | |
if let data = data(using: .utf8) { | |
data.withUnsafeBytes({ (bytes: UnsafePointer<UInt8>) -> Void in | |
CC_MD5(bytes, CC_LONG(data.count), &digest) | |
}) | |
} | |
var digestHex = "" | |
for index in 0..<Int(CC_MD5_DIGEST_LENGTH) { | |
digestHex += String(format: "%02x", digest[index]) | |
} | |
return digestHex | |
} | |
} | |
print("maimieng.com".MD5) | |
// OUTPUT: | |
// abd67f15825d199d8fe203e751647f64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
struct MyOption: OptionSet { | |
let rawValue: UInt | |
static let none = MyOption(rawValue: 0) | |
static let option1 = MyOption(rawValue: 1) | |
static let option2 = MyOption(rawValue: 1 << 1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
let int: Int = 0 | |
let float: Float = 0.0 | |
let double: Double = 0.0 | |
let intNumber: NSNumber = int as NSNumber | |
let floatNumber: NSNumber = float as NSNumber | |
let doubleNumber: NSNumber = double as NSNumber | |
print(String(validatingUTF8: intNumber.objCType)) | |
print(String(validatingUTF8: floatNumber.objCType)) | |
print(String(validatingUTF8: doubleNumber.objCType)) | |
let p = NSValue(cgPoint: CGPoint(x: 3.0, y: 3.0)) | |
print(String(validatingUTF8: p.objCType)) | |
let t = NSValue(cgAffineTransform: .identity) | |
print(String(validatingUTF8: t.objCType)) | |
// OUTPUT: | |
// Optional("q") | |
// Optional("f") | |
// Optional("d") | |
// Optional("{CGPoint=dd}") | |
// Optional("{CGAffineTransform=dddddd}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C | |
// int test(int a) { | |
// return a + 1; | |
// } | |
import UIKit | |
@_silgen_name("test") func c_test(_ a: Int32) -> Int32 | |
print(c_test(99)) | |
// OUTPUT: | |
// 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Class {} | |
private var key: Void? | |
extension Class { | |
var title: String? { | |
get { | |
return objc_getAssociatedObject(self, &key) as? String | |
} | |
set { | |
objc_setAssociatedObject(self, | |
&key, | |
newValue, | |
.OBJC_ASSOCIATION_COPY_NONATOMIC) | |
} | |
} | |
} | |
let a = Class() | |
a.title = "maimieng.com" | |
print(a.title ?? "nil") | |
// OUTPUT: | |
// maimieng.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func myMethodLock(_ obj: AnyObject!) { | |
func synchronized(_ lock: AnyObject!, closure: () -> ()) { | |
objc_sync_enter(lock) | |
closure() | |
objc_sync_exit(lock) | |
} | |
synchronized(obj) { | |
// do sth... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment