Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 3 #56

Merged
merged 7 commits into from
Oct 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ included: # paths to include during linting. `--path` is ignored if present.
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Carthage
- Pods
disabled_rules:
- type_name

# configurable rules can be customized from this configuration file
# binary rules can set their severity level
Expand Down
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
osx_image: xcode7.3
osx_image: xcode8
language: objective-c

before_install:
Expand All @@ -10,4 +10,7 @@ script:
- xcodebuild clean build -project Cache.xcodeproj -scheme "Cache-Mac" -sdk macosx
- xcodebuild test -project Cache.xcodeproj -scheme "Cache-Mac" -sdk macosx
- xcodebuild clean build -project Cache.xcodeproj -scheme "Cache-iOS" -sdk iphonesimulator
- xcodebuild test -project Cache.xcodeproj -scheme "Cache-iOS" -sdk iphonesimulator
- xcodebuild test -project Cache.xcodeproj -scheme "Cache-iOS" -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 6,OS=10.0'

notifications:
email: false
2 changes: 2 additions & 0 deletions Cache.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ Pod::Spec.new do |s|

s.frameworks = 'Foundation'
s.dependency 'CryptoSwift'

s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3.0' }
end
98 changes: 23 additions & 75 deletions Cache.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cache.xcodeproj/xcshareddata/xcschemes/Cache-Mac.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Cache.xcodeproj/xcshareddata/xcschemes/Cache-iOS.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
6 changes: 3 additions & 3 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github "krzyzanowskim/CryptoSwift" "3a0c3153717070a42971c30cd73d67f6f832f4bc"
github "Quick/Nimble" "v4.0.1"
github "Quick/Quick" "v0.9.2"
github "krzyzanowskim/CryptoSwift" "0.6.0"
github "Quick/Nimble" "v5.0.0"
github "Quick/Quick" "v0.10.0"
23 changes: 12 additions & 11 deletions Source/Mac/Extensions/NSImage+Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension NSImage: Cachable {
- Parameter data: Data to decode from
- Returns: Optional CacheType
*/
public static func decode(data: NSData) -> CacheType? {
public static func decode(_ data: Data) -> CacheType? {
let image = NSImage(data: data)
return image
}
Expand All @@ -25,14 +25,11 @@ extension NSImage: Cachable {
Encodes UIImage to NSData
- Returns: Optional NSData
*/
public func encode() -> NSData? {
guard let data = TIFFRepresentation else { return nil }
public func encode() -> Data? {
guard let data = tiffRepresentation else { return nil }

let imageFileType: NSBitmapImageFileType = hasAlpha
? .NSPNGFileType
: .NSJPEGFileType

return NSBitmapImageRep(data: data)?.representationUsingType(imageFileType, properties: [:])
let imageFileType: NSBitmapImageFileType = hasAlpha ? .PNG : .JPEG
return NSBitmapImageRep(data: data)?.representation(using: imageFileType, properties: [:])
}
}

Expand All @@ -48,12 +45,16 @@ extension NSImage {
*/
var hasAlpha: Bool {
var imageRect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let imageRef = CGImageForProposedRect(&imageRect, context: nil, hints: nil)

guard let imageRef = cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else {
return false
}

let result: Bool
let alpha = CGImageGetAlphaInfo(imageRef)
let alpha = imageRef.alphaInfo

switch alpha {
case .None, .NoneSkipFirst, .NoneSkipLast:
case .none, .noneSkipFirst, .noneSkipLast:
result = false
default:
result = true
Expand Down
8 changes: 4 additions & 4 deletions Source/Mac/HybridCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ public class HybridCache: BasicHybridCache {
public override init(name: String, config: Config = Config.defaultConfig) {
super.init(name: name, config: config)

let notificationCenter = NSNotificationCenter.defaultCenter()
let notificationCenter = NotificationCenter.default

notificationCenter.addObserver(self, selector: #selector(HybridCache.applicationWillTerminate),
name: NSApplicationWillTerminateNotification, object: nil)
name: NSNotification.Name.NSApplicationWillTerminate, object: nil)
notificationCenter.addObserver(self, selector: #selector(HybridCache.applicationDidResignActive),
name: NSApplicationDidResignActiveNotification, object: nil)
name: NSNotification.Name.NSApplicationDidResignActive, object: nil)
}

/**
Removes notification center observer.
*/
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
NotificationCenter.default.removeObserver(self)
}

// MARK: - Notifications
Expand Down
14 changes: 7 additions & 7 deletions Source/Shared/BasicHybridCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class BasicHybridCache: NSObject {
- Parameter expiry: Expiration date for the cached object
- Parameter completion: Completion closure to be called when the task is done
*/
public func add<T: Cachable>(key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) {
public func add<T: Cachable>(_ key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) {
let expiry = expiry ?? config.expiry

frontStorage.add(key, object: object, expiry: expiry) { [weak self] in
Expand All @@ -69,20 +69,20 @@ public class BasicHybridCache: NSObject {
- Parameter key: Unique key to identify the object in the cache
- Parameter completion: Completion closure returns object or nil
*/
public func object<T: Cachable>(key: String, completion: (object: T?) -> Void) {
public func object<T: Cachable>(_ key: String, completion: @escaping (_ object: T?) -> Void) {
frontStorage.object(key) { [weak self] (object: T?) in
if let object = object {
completion(object: object)
completion(object)
return
}

guard let weakSelf = self else {
completion(object: object)
completion(object)
return
}

weakSelf.backStorage.object(key) { (object: T?) in
completion(object: object)
completion(object)
}
}
}
Expand All @@ -93,7 +93,7 @@ public class BasicHybridCache: NSObject {
- Parameter key: Unique key to identify the object in the cache
- Parameter completion: Completion closure to be called when the task is done
*/
public func remove(key: String, completion: (() -> Void)? = nil) {
public func remove(_ key: String, completion: (() -> Void)? = nil) {
frontStorage.remove(key) { [weak self] in
guard let weakSelf = self else {
completion?()
Expand All @@ -111,7 +111,7 @@ public class BasicHybridCache: NSObject {

- Parameter completion: Completion closure to be called when the task is done
*/
public func clear(completion: (() -> Void)? = nil) {
public func clear(_ completion: (() -> Void)? = nil) {
frontStorage.clear() { [weak self] in
guard let weakSelf = self else {
completion?()
Expand Down
6 changes: 3 additions & 3 deletions Source/Shared/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Foundation
Cachable protocol. It's two layered cache (with front and back storages).
Subscribes to system notifications to clear expired cached objects.
*/
public class Cache<T: Cachable>: HybridCache {
public final class Cache<T: Cachable>: HybridCache {

// MARK: - Initialization

Expand All @@ -29,7 +29,7 @@ public class Cache<T: Cachable>: HybridCache {
- Parameter expiry: Expiration date for the cached object
- Parameter completion: Completion closure to be called when the task is done
*/
public override func add(key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) {
public override func add(_ key: String, object: T, expiry: Expiry? = nil, completion: (() -> Void)? = nil) {
super.add(key, object: object, expiry: expiry, completion: completion)
}

Expand All @@ -39,7 +39,7 @@ public class Cache<T: Cachable>: HybridCache {
- Parameter key: Unique key to identify the object in the cache
- Parameter completion: Completion closure returns object or nil
*/
public override func object(key: String, completion: (object: T?) -> Void) {
public override func object(_ key: String, completion: @escaping (_ object: T?) -> Void) {
super.object(key, completion: completion)
}
}
6 changes: 3 additions & 3 deletions Source/Shared/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public struct Config {
- Parameter maxSize: Maximum size of the cache storage
- Parameter maxObjects: Maximum amount of objects to be stored in memory
*/
public init(frontKind: StorageKind, backKind: StorageKind, expiry: Expiry = .Never, maxSize: UInt = 0, maxObjects: Int = 0) {
public init(frontKind: StorageKind, backKind: StorageKind, expiry: Expiry = .never, maxSize: UInt = 0, maxObjects: Int = 0) {
self.frontKind = frontKind
self.backKind = backKind
self.expiry = expiry
Expand All @@ -44,7 +44,7 @@ extension Config {
*/
public static var defaultConfig: Config {
return Config(
frontKind: .Memory,
backKind: .Disk)
frontKind: .memory,
backKind: .disk)
}
}
4 changes: 2 additions & 2 deletions Source/Shared/DataStructures/Cachable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public protocol Cachable {

- Parameter data: Data to decode from
*/
static func decode(data: NSData) -> CacheType?
static func decode(_ data: Data) -> CacheType?

/**
Encodes an instance to NSData
*/
func encode() -> NSData?
func encode() -> Data?
}
10 changes: 5 additions & 5 deletions Source/Shared/DataStructures/Capsule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import Foundation
Helper class to hold cached instance and expiry date.
Used in memory storage to work with NSCache.
*/
class Capsule: NSObject {
final class Capsule: NSObject {

/// Object to be cached
let value: Any
let object: Any
/// Expiration date
let expiryDate: NSDate
let expiryDate: Date

/// Checks if cached object is expired according to expiration date
var expired: Bool {
Expand All @@ -25,7 +25,7 @@ class Capsule: NSObject {
- Parameter expiry: Expiration date
*/
init(value: Any, expiry: Expiry) {
self.value = value
self.expiryDate = expiry.date
self.object = value
self.expiryDate = expiry.date as Date
}
}
20 changes: 10 additions & 10 deletions Source/Shared/DataStructures/Expiry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import Foundation
*/
public enum Expiry {
/// Object will be expired in the nearest future
case Never
case never
/// Object will be expired in the specified amount of seconds
case Seconds(NSTimeInterval)
case seconds(TimeInterval)
/// Object will be expired on the specified date
case Date(NSDate)
case date(Foundation.Date)

/// Returns the appropriate date object
public var date: NSDate {
let result: NSDate
public var date: Foundation.Date {
let result: Foundation.Date

switch self {
case .Never:
case .never:
// Ref: http://lists.apple.com/archives/cocoa-dev/2005/Apr/msg01833.html
result = NSDate(timeIntervalSince1970: 60 * 60 * 24 * 365 * 68)
case .Seconds(let seconds):
result = NSDate().dateByAddingTimeInterval(seconds)
case .Date(let date):
result = Foundation.Date(timeIntervalSince1970: 60 * 60 * 24 * 365 * 68)
case .seconds(let seconds):
result = Foundation.Date().addingTimeInterval(seconds)
case .date(let date):
result = date
}

Expand Down
16 changes: 8 additions & 8 deletions Source/Shared/DataStructures/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import Foundation
*/
public enum JSON {
/// JSON array
case Array([AnyObject])
case array([Any])
/// JSON dictionary
case Dictionary([String : AnyObject])
case dictionary([String : Any])

/// Converts value to AnyObject
public var object: AnyObject {
var result: AnyObject
public var object: Any {
var result: Any

switch self {
case .Array(let object):
result = object
case .Dictionary(let object):
result = object
case .array(let object):
result = object as Any
case .dictionary(let object):
result = object as Any
}

return result
Expand Down
12 changes: 6 additions & 6 deletions Source/Shared/DataStructures/StorageKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
*/
public enum StorageKind {
/// Memory storage
case Memory
case memory
/// Disk storage
case Disk
case disk
/// Custom kind of storage by the given name
case Custom(String)
case custom(String)

/// Converts value to appropriate string
public var name: String {
let result: String

switch self {
case .Memory:
case .memory:
result = "Memory"
case .Disk:
case .disk:
result = "Disk"
case .Custom(let name):
case .custom(let name):
result = name
}

Expand Down
16 changes: 8 additions & 8 deletions Source/Shared/Extensions/JSON+Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Foundation
/// A configuration struct
public struct CacheJSONOptions {
/// Options used when creating Foundation objects from JSON data
public static var readingOptions: NSJSONReadingOptions = NSJSONReadingOptions()
public static var readingOptions: JSONSerialization.ReadingOptions = JSONSerialization.ReadingOptions()
/// Options for writing JSON data.
public static var writeOptions: NSJSONWritingOptions = NSJSONWritingOptions()
public static var writeOptions: JSONSerialization.WritingOptions = JSONSerialization.WritingOptions()
}

// MARK: - Cachable
Expand All @@ -24,18 +24,18 @@ extension JSON: Cachable {
- Parameter data: Data to decode from
- Returns: An optional CacheType
*/
public static func decode(data: NSData) -> CacheType? {
public static func decode(_ data: Data) -> CacheType? {
var result: CacheType?

do {
let object = try NSJSONSerialization.JSONObjectWithData(data,
let object = try JSONSerialization.jsonObject(with: data,
options: CacheJSONOptions.readingOptions)

switch object {
case let dictionary as [String : AnyObject]:
result = JSON.Dictionary(dictionary)
result = JSON.dictionary(dictionary)
case let array as [AnyObject]:
result = JSON.Array(array)
result = JSON.array(array)
default:
result = nil
}
Expand All @@ -49,8 +49,8 @@ extension JSON: Cachable {

- Returns: Optional NSData
*/
public func encode() -> NSData? {
return try? NSJSONSerialization.dataWithJSONObject(object,
public func encode() -> Data? {
return try? JSONSerialization.data(withJSONObject: object,
options: CacheJSONOptions.writeOptions)
}
}
Loading