Skip to content

Commit

Permalink
Update to Swift 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirbyt committed Sep 20, 2016
1 parent 6dab38c commit 93ed59c
Show file tree
Hide file tree
Showing 25 changed files with 375 additions and 341 deletions.
12 changes: 6 additions & 6 deletions Examples/iOSExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
10 changes: 5 additions & 5 deletions Examples/iOSExample/WPSTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import WPSKit

class WPSTextViewController: UIViewController {

@IBOutlet private var textView: WPSTextView!
@IBOutlet private var borderedTextView: WPSTextView!
@IBOutlet fileprivate var textView: WPSTextView!
@IBOutlet fileprivate var borderedTextView: WPSTextView!

override func viewDidLoad() {
super.viewDidLoad()
applyTextViewStyle(borderedTextView)
}

private func applyTextViewStyle(textView: WPSTextView) {
textView.backgroundColor = UIColor.whiteColor()
textView.layer.borderColor = UIColor.blackColor().CGColor
fileprivate func applyTextViewStyle(_ textView: WPSTextView) {
textView.backgroundColor = UIColor.white
textView.layer.borderColor = UIColor.black.cgColor
textView.layer.borderWidth = 5
textView.textContainerInset = UIEdgeInsetsMake(8, 8, 8, 8)
}
Expand Down
24 changes: 12 additions & 12 deletions Sources/Foundation/NSError+WPSKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,30 @@ extension NSError {
- returns: An `NSError` containing the HTTP error.
*/
public static func HTTPError(responseURL: NSURL!, statusCode: Int, message: String!, data: NSData!) -> NSError {
public static func HTTPError(_ responseURL: URL!, statusCode: Int, message: String!, data: Data!) -> NSError {
var userInfo: [String: AnyObject] = [:]

userInfo[NSLocalizedFailureReasonErrorKey] = message
userInfo[NSLocalizedFailureReasonErrorKey] = message as AnyObject?

if data != nil && data.length > 0 {
if let httpBody: String = NSString(bytes: data.bytes, length: data.length, encoding: NSUTF8StringEncoding) as? String {
userInfo["HTTPBody"] = httpBody
if data != nil && data.count > 0 {
if let httpBody = String(data: data, encoding: .utf8) {
userInfo["HTTPBody"] = httpBody as AnyObject?
if message == nil {
userInfo[NSLocalizedFailureReasonErrorKey] = httpBody
userInfo[NSLocalizedFailureReasonErrorKey] = httpBody as AnyObject?
}
}
}

if responseURL != nil {
userInfo[NSURLErrorKey] = responseURL
userInfo["NSErrorFailingURLKey"] = responseURL
userInfo["NSErrorFailingURLStringKey"] = responseURL.absoluteString
userInfo[NSURLErrorKey] = responseURL as AnyObject?
userInfo["NSErrorFailingURLKey"] = responseURL as AnyObject?
userInfo["NSErrorFailingURLStringKey"] = responseURL.absoluteString as AnyObject?
}

userInfo[NSLocalizedDescriptionKey] = NSHTTPURLResponse.localizedStringForStatusCode(statusCode)
userInfo["HTTPStatusCode"] = statusCode;
userInfo[NSLocalizedDescriptionKey] = HTTPURLResponse.localizedString(forStatusCode: statusCode) as AnyObject?
userInfo["HTTPStatusCode"] = statusCode as AnyObject?

return NSError(domain: "HTTPErrorDomain", code: statusCode, userInfo: userInfo)
}

}
}
12 changes: 6 additions & 6 deletions Sources/Foundation/NSNotificationCenter+WPSKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import Foundation

extension NSNotificationCenter {
extension NotificationCenter {

/**
Post a notification on the main thread.
Expand All @@ -37,12 +37,12 @@ extension NSNotificationCenter {
- parameter notificationSender: The object posting the notification.
- parameter userInfo: Information about the the notification. May be nil.
*/
public func postOnMainThreadNotificationName(name: String, object: AnyObject?, userInfo: [String:AnyObject]? = nil) {
if NSThread.isMainThread() {
postNotificationName(name, object: object, userInfo: userInfo)
public func postOnMainThreadNotificationName(_ name: String, object: AnyObject?, userInfo: [String:AnyObject]? = nil) {
if Thread.isMainThread {
post(name: Notification.Name(rawValue: name), object: object, userInfo: userInfo)
} else {
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
self.postNotificationName(name, object: object, userInfo: userInfo)
DispatchQueue.main.async { [unowned self] in
self.post(name: Notification.Name(rawValue: name), object: object, userInfo: userInfo)
}
}
}
Expand Down
42 changes: 19 additions & 23 deletions Sources/Foundation/NSURL+WPSKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import Foundation

extension NSURL {
extension URL {

// -------------------------------------------------------------------
// MARK: - User Domain
Expand All @@ -43,27 +43,25 @@ extension NSURL {
- returns A new URL with pathComponent appended if provided.
*/
public static func userDomainURL(directory: NSSearchPathDirectory, pathComponent: String?, isDirectory: Bool = true) throws -> NSURL? {
let fm = NSFileManager.defaultManager()
guard var url = fm.URLsForDirectory(directory, inDomains: .UserDomainMask).last else {
public static func userDomainURL(_ directory: FileManager.SearchPathDirectory, pathComponent: String?, isDirectory: Bool = true) throws -> URL? {
let fm = FileManager.default
guard var url = fm.urls(for: directory, in: .userDomainMask).last else {
return nil
}

if isDirectory {
if let pathComponent = pathComponent {
url = url.URLByAppendingPathComponent(pathComponent)
url = url.appendingPathComponent(pathComponent)
}
}

// Create the directory.
if let path = url.path {
try NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
}
try FileManager.default.createDirectory(atPath: url.path, withIntermediateDirectories: true, attributes: nil)

// Add the path component if it is not a directory.
if isDirectory == false {
if let pathComponent = pathComponent {
url = url.URLByAppendingPathComponent(pathComponent)
url = url.appendingPathComponent(pathComponent)
}
}

Expand All @@ -79,7 +77,7 @@ extension NSURL {
- returns A URL containing the path.
*/
public static func documentDirectoryURL() -> NSURL? {
public static func documentDirectoryURL() -> URL? {
return try! self.documentDirectoryURL(nil, isDirectory: true)
}

Expand All @@ -93,8 +91,8 @@ extension NSURL {
- returns A URL containing the path with `pathComponent` appended.
*/
public static func documentDirectoryURL(pathComponent: String?, isDirectory: Bool = true) throws -> NSURL? {
return try self.userDomainURL(.DocumentDirectory, pathComponent: pathComponent, isDirectory: isDirectory)
public static func documentDirectoryURL(_ pathComponent: String?, isDirectory: Bool = true) throws -> URL? {
return try self.userDomainURL(.documentDirectory, pathComponent: pathComponent, isDirectory: isDirectory)
}

// -------------------------------------------------------------------
Expand All @@ -106,7 +104,7 @@ extension NSURL {
- returns A URL containing the path.
*/
public static func cacheDirectoryURL() -> NSURL? {
public static func cacheDirectoryURL() -> URL? {
return try! self.cacheDirectoryURL(nil, isDirectory: true)
}

Expand All @@ -120,8 +118,8 @@ extension NSURL {
- returns A URL containing the path with `pathComponent` appended.
*/
public static func cacheDirectoryURL(pathComponent: String?, isDirectory: Bool = true) throws -> NSURL? {
return try self.userDomainURL(.CachesDirectory, pathComponent: pathComponent, isDirectory: isDirectory)
public static func cacheDirectoryURL(_ pathComponent: String?, isDirectory: Bool = true) throws -> URL? {
return try self.userDomainURL(.cachesDirectory, pathComponent: pathComponent, isDirectory: isDirectory)
}

// -------------------------------------------------------------------
Expand All @@ -133,7 +131,7 @@ extension NSURL {
- returns A URL containing the path.
*/
public static func temporaryDirectoryURL() -> NSURL? {
public static func temporaryDirectoryURL() -> URL? {
return try! self.temporaryDirectoryURL(nil, isDirectory: true)
}

Expand All @@ -147,25 +145,23 @@ extension NSURL {
- returns A URL containing the path with `pathComponent` appended.
*/
public static func temporaryDirectoryURL(pathComponent: String?, isDirectory: Bool = true) throws -> NSURL? {
public static func temporaryDirectoryURL(_ pathComponent: String?, isDirectory: Bool = true) throws -> URL? {
let tmp = NSTemporaryDirectory()
var url = NSURL(fileURLWithPath: tmp)
var url = URL(fileURLWithPath: tmp)

if isDirectory {
if let pathComponent = pathComponent {
url = url.URLByAppendingPathComponent(pathComponent)
url = url.appendingPathComponent(pathComponent)
}
}

// Create the directory.
if let path = url.path {
try NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
}
try FileManager.default.createDirectory(atPath: url.path, withIntermediateDirectories: true, attributes: nil)

// Add the path component if it is not a directory.
if isDirectory == false {
if let pathComponent = pathComponent {
url = url.URLByAppendingPathComponent(pathComponent)
url = url.appendingPathComponent(pathComponent)
}
}

Expand Down
30 changes: 14 additions & 16 deletions Sources/Foundation/String+WPSKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ extension String {
- returns A `String` containing the path with pathComponent appended if provided.
*/
public static func userDomainPath(directory: NSSearchPathDirectory, pathComponent: String?, isDirectory: Bool = true) throws -> String? {
let url = try NSURL.userDomainURL(directory, pathComponent: pathComponent, isDirectory: isDirectory)
public static func userDomainPath(_ directory: FileManager.SearchPathDirectory, pathComponent: String?, isDirectory: Bool = true) throws -> String? {
let url = try URL.userDomainURL(directory, pathComponent: pathComponent, isDirectory: isDirectory)
return url?.path
}

Expand All @@ -71,8 +71,8 @@ extension String {
- returns A String containing the path with `pathComponent` appended.
*/
public static func documentDirectory(pathComponent: String?, isDirectory: Bool = true) throws -> String? {
return try self.userDomainPath(.DocumentDirectory, pathComponent: pathComponent, isDirectory: isDirectory)
public static func documentDirectory(_ pathComponent: String?, isDirectory: Bool = true) throws -> String? {
return try self.userDomainPath(.documentDirectory, pathComponent: pathComponent, isDirectory: isDirectory)
}

// -------------------------------------------------------------------
Expand All @@ -98,8 +98,8 @@ extension String {
- returns A String containing the path with `pathComponent` appended.
*/
public static func cacheDirectory(pathComponent: String?, isDirectory: Bool = true) throws -> String? {
return try self.userDomainPath(.CachesDirectory, pathComponent: pathComponent, isDirectory: isDirectory)
public static func cacheDirectory(_ pathComponent: String?, isDirectory: Bool = true) throws -> String? {
return try self.userDomainPath(.cachesDirectory, pathComponent: pathComponent, isDirectory: isDirectory)
}

// -------------------------------------------------------------------
Expand All @@ -125,25 +125,23 @@ extension String {
- returns A String containing the path with `pathComponent` appended.
*/
public static func temporaryDirectory(pathComponent: String?, isDirectory: Bool = true) throws -> String? {
public static func temporaryDirectory(_ pathComponent: String?, isDirectory: Bool = true) throws -> String? {
if let tmpDirectory = self.temporaryDirectory() {
if var url = NSURL(string: tmpDirectory) {
if var url = URL(string: tmpDirectory) {

if isDirectory {
if let pathComponent = pathComponent {
url = url.URLByAppendingPathComponent(pathComponent)
url = url.appendingPathComponent(pathComponent)
}
}

// Create the directory.
if let path = url.path {
try NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil)
}
try FileManager.default.createDirectory(atPath: url.path, withIntermediateDirectories: true, attributes: nil)

// Add the path component if it is not a directory.
if isDirectory == false {
if let pathComponent = pathComponent {
url = url.URLByAppendingPathComponent(pathComponent)
url = url.appendingPathComponent(pathComponent)
}
}

Expand All @@ -164,7 +162,7 @@ extension String {
- returns A string containing the app name.
*/
public static func appName() -> String? {
if let info = NSBundle.mainBundle().infoDictionary {
if let info = Bundle.main.infoDictionary {
if let appName = info["CFBundleDisplayName"] as? String {
return appName
}
Expand All @@ -181,7 +179,7 @@ extension String {
- returns A string containing the app version.
*/
public static func appVersion() -> String? {
if let info = NSBundle.mainBundle().infoDictionary {
if let info = Bundle.main.infoDictionary {
if let version = info["CFBundleShortVersionString"] as? String {
if let build = info["CFBundleVersion"] as? String {
return "Version \(version) (build \(build))"
Expand All @@ -200,7 +198,7 @@ extension String {
- returns A string containing the app version.
*/
public static func appVersionShort() -> String? {
if let info = NSBundle.mainBundle().infoDictionary {
if let info = Bundle.main.infoDictionary {
if let version = info["CFBundleShortVersionString"] as? String {
if let build = info["CFBundleVersion"] as? String {
return "\(version).\(build))"
Expand Down

0 comments on commit 93ed59c

Please sign in to comment.