Edited at

swift 3.0 のメモ

More than 3 years have passed since last update.

delay

let _0_delay = 3.0 * Double(NSEC_PER_SEC)

let _0_time = DispatchTime.now() + Double(Int64(_0_delay)) / Double(NSEC_PER_SEC)
DispatchQueue.main.acyncAfter(deadline: _0_time, execute: {
})

DispatchQueue.main.acyncAfter(deadline: DispatchTime.now() + Double(Int64(3.0 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
})

main thread

DispatchQueue.main.async(execute: { [weak self] in

guard let _0_weakSelf = self else { return }
})

URLSession POST dataTask

NSMutableURLRequest を dataTask でキチッと as URLRequest しないとエラーになる

let req: NSMutableURLRequest = NSMutableURLRequest(url: url)

let config: URLSessionConfiguration = URLSessionConfiguration.default()
/* setup post data */
let data: NSMutableData = NSMutableData()
if let td: Dictionary = txtD {
for txtKey: String in td.keys {
if let txtVal: String = td[txtKey] {
data.append(NSString(format: "--%@\r\n", CommonSession._Boundary).data(using: String.Encoding.utf8.rawValue)!)
data.append(NSString(format: "Content-Disposition: form-data;").data(using: String.Encoding.utf8.rawValue)!)
data.append(NSString(format: "name=\"%@\"\r\n\r\n", txtKey).data(using: String.Encoding.utf8.rawValue)!)
data.append(NSString(format: "%@\r\n", txtVal).data(using: String.Encoding.utf8.rawValue)!)
}
}
}
/* add last boundary */
let d: Data = NSString(format: "--%@--\r\n", CommonSession._Boundary).data(using: String.Encoding.utf8.rawValue)!
data.append(d)

/* setup post request */
req.httpMethod = "POST"
req.httpBody = data as Data
let session: URLSession = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
let task: URLSessionDataTask = session.dataTask(with: req as URLRequest, completionHandler: { [weak self] (data, response, error) in
do {
} catch let e as NSError {
}
})

NSNotification

name の String は rawValue が必要になった

func SampleFunc(n: NSNotification) {

guard
let name: String = n.name.rawValue where name == "通知の名前",
let info: [NSObject : AnyObject] = n.userInfo
else {
return
}
}

送る側もちょっと変更なった

NotificationCenter.default.post(name: Notification.Name(rawValue: "通知の名前"), object: nil)

String の url encode

if let u: String = "http://www.tes.com/あいうえお".addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) {

return u
}

observeValueForKeyPath(名前も短くなった)

override func observeValue(forKeyPath keyPath: String?, of object: AnyObject?, change: [NSKeyValueChangeKey : AnyObject]?, context: UnsafeMutablePointer<Void>?) {

}

CGRectMake, CGRectZero, CGSizeZero... etc.

全部使えなくなった

CGRect(x: 0, y: 0, width: 0, height: 0)

CGSize(width: 0, height: 0)

とりあえずextensionで・・・

extension CGRect {

static func Make(_ x: CGFloat, _ y: CGFloat, _ w: CGFloat, _ h: CGFloat) -> CGRect {
return CGRect.init(x: x, y: y, width: w, height: h)
}
static var Zero: CGRect {
get {
return CGRect.init(x: 0, y: 0, width: 0, height: 0)
}
}
}

let a: CGRect = CGRect.Make(0, 0, 0, 0)
let b: CGRect = CGRect.Zero

CGAffineTransform関連

CGAffineTransform.identity

CGAffineTransform(scaleX: x, y: y)
CGAffineTransform(translationX: x, y: y)

#selectorはメソッドのパラメータの名前いるようになった

#selector(Class.method(_:))      // before

#selector(Class.method(name:)) // after

String分割

let s: [String] = str.componentsSeparatedBy("=")     // before

let s: [String] = str.components(separatedBy: "=") // after

func -> var (他にもちょいちょい変わってる)

classLayerとか、shouldAutorotateとか。。。

/// before

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.all
}
override func prefersStatusBarHidden() -> Bool {
return true
}
override func preferredStatusBarStyle:() -> UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
/// after
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.all
}
override var prefersStatusBarHidden: Bool {
return true
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}