Skip to content

Commit

Permalink
Merge pull request #14 from dasmer/daz/swift3
Browse files Browse the repository at this point in the history
Update for Swift 3
  • Loading branch information
dasmer authored Feb 26, 2017
2 parents 255d80a + 6d944a6 commit 59e12b7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion EmojiKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'EmojiKit'
s.version = '0.0.1'
s.version = '0.0.2'
s.license = 'MIT'
s.summary = 'Effortless emoji-querying in Swift'
s.homepage = 'https://github.com/dasmer/EmojiKit'
Expand Down
3 changes: 3 additions & 0 deletions EmojiKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
TargetAttributes = {
2FD740911C3440BA0059751B = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0820;
};
};
};
Expand Down Expand Up @@ -258,6 +259,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -277,6 +279,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.dastronics.EmojiKit;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
2 changes: 1 addition & 1 deletion EmojiKit/DictionarySerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

public typealias JSONDictionary = [String: AnyObject]
public typealias JSONDictionary = [String: Any]

public protocol DictionaryDeserializable {
init?(dictionary: JSONDictionary)
Expand Down
6 changes: 3 additions & 3 deletions EmojiKit/Emoji.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ extension Emoji: DictionaryDeserializable, DictionarySerializable {

public init?(dictionary: JSONDictionary) {
guard let name = dictionary["name"] as? String,
character = dictionary["character"] as? String,
aliases = dictionary["aliases"] as? [String],
groups = dictionary["groups"] as? [String] else { return nil }
let character = dictionary["character"] as? String,
let aliases = dictionary["aliases"] as? [String],
let groups = dictionary["groups"] as? [String] else { return nil }

self.name = name
self.character = character
Expand Down
44 changes: 22 additions & 22 deletions EmojiKit/EmojiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import Foundation

private let AllEmojiArray: [Emoji] = {
guard let path = NSBundle(forClass: EmojiFetchOperation.self).pathForResource("AllEmoji", ofType: "json"),
data = NSData(contentsOfFile: path),
jsonObject = try? NSJSONSerialization.JSONObjectWithData(data, options: []),
jsonDictionaries = jsonObject as? [JSONDictionary] else { return [] }
guard let path = Bundle(for: EmojiFetchOperation.self).path(forResource: "AllEmoji", ofType: "json"),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)),
let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []),
let jsonDictionaries = jsonObject as? [JSONDictionary] else { return [] }

return jsonDictionaries.flatMap { Emoji(dictionary: $0) }
}()
Expand All @@ -29,9 +29,9 @@ public struct EmojiFetcher {

// MARK: - Properties

private let backgroundQueue: NSOperationQueue = {
let queue = NSOperationQueue()
queue.qualityOfService = .UserInitiated
private let backgroundQueue: OperationQueue = {
let queue = OperationQueue()
queue.qualityOfService = .userInitiated
return queue
}()

Expand All @@ -42,18 +42,18 @@ public struct EmojiFetcher {

// MARK: - Functions

public func query(searchString: String, completion: ([Emoji] -> Void)) {
public func query(_ searchString: String, completion: @escaping (([Emoji]) -> Void)) {
cancelFetches()

let operation = EmojiFetchOperation(searchString: searchString)

operation.completionBlock = {

if operation.cancelled {
if operation.isCancelled {
return;
}

dispatch_async(dispatch_get_main_queue()) {
DispatchQueue.main.async {
completion(operation.results)
}
}
Expand All @@ -65,12 +65,12 @@ public struct EmojiFetcher {
backgroundQueue.cancelAllOperations()
}

public func isEmojiRepresentedByString(string: String) -> Bool {
public func isEmojiRepresentedByString(_ string: String) -> Bool {
return AllEmojiDictionary[string] != nil
}
}

private final class EmojiFetchOperation: NSOperation {
private final class EmojiFetchOperation: Operation {

// MARK: - Properties

Expand Down Expand Up @@ -100,19 +100,19 @@ private final class EmojiFetchOperation: NSOperation {

// MARK: - Functions

private func resultsForSearchString(searchString: String) -> [Emoji] {
let lowercaseSearchString = searchString.lowercaseString
guard !cancelled else { return [] }
private func resultsForSearchString(_ searchString: String) -> [Emoji] {
let lowercaseSearchString = searchString.lowercased()
guard !isCancelled else { return [] }

var results = [Emoji]()

// Matches of the full names of the emoji
results += AllEmojiArray.filter { $0.name.hasPrefix(lowercaseSearchString) }
guard !cancelled else { return [] }
guard !isCancelled else { return [] }

// Matches of individual words in the name
results += AllEmojiArray.filter { emoji in
guard results.indexOf(emoji) == nil else { return false }
guard results.index(of: emoji) == nil else { return false }

var validResult = false

Expand All @@ -126,11 +126,11 @@ private final class EmojiFetchOperation: NSOperation {
}
return validResult
}
guard !cancelled else { return [] }
guard !isCancelled else { return [] }

// Alias matches
results += AllEmojiArray.filter { emoji in
guard results.indexOf(emoji) == nil else { return false }
guard results.index(of: emoji) == nil else { return false }

var validResult = false

Expand All @@ -143,11 +143,11 @@ private final class EmojiFetchOperation: NSOperation {

return validResult
}
guard !cancelled else { return [] }
guard !isCancelled else { return [] }

// Group matches
results += AllEmojiArray.filter { emoji in
guard results.indexOf(emoji) == nil else { return false }
guard results.index(of: emoji) == nil else { return false }

var validResult = false

Expand All @@ -160,7 +160,7 @@ private final class EmojiFetchOperation: NSOperation {

return validResult
}
guard !cancelled else { return [] }
guard !isCancelled else { return [] }

return results
}
Expand Down

0 comments on commit 59e12b7

Please sign in to comment.