Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
A bit cleaner implementation of primitive Swift Array filtering, to a…
Browse files Browse the repository at this point in the history
…void many unwrapping and casting.
  • Loading branch information
Ignacio Romero Zurbuchen committed Oct 3, 2016
1 parent d96b159 commit 31da460
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
38 changes: 20 additions & 18 deletions Examples/Messenger-Swift/MessageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class MessageViewController: SLKTextViewController {

var messages = [Message]()

var users = ["Allen", "Anna", "Alicia", "Arnold", "Armando", "Antonio", "Brad", "Catalaya", "Christoph", "Emerson", "Eric", "Everyone", "Steve"]
var channels = ["General", "Random", "iOS", "Bugs", "Sports", "Android", "UI", "SSB"]
var emojis = ["-1", "m", "man", "machine", "block-a", "block-b", "bowtie", "boar", "boat", "book", "bookmark", "neckbeard", "metal", "fu", "feelsgood"]
var commands = ["msg", "call", "text", "skype", "kick", "invite"]
var users: Array = ["Allen", "Anna", "Alicia", "Arnold", "Armando", "Antonio", "Brad", "Catalaya", "Christoph", "Emerson", "Eric", "Everyone", "Steve"]
var channels: Array = ["General", "Random", "iOS", "Bugs", "Sports", "Android", "UI", "SSB"]
var emojis: Array = ["-1", "m", "man", "machine", "block-a", "block-b", "bowtie", "boar", "boat", "book", "bookmark", "neckbeard", "metal", "fu", "feelsgood"]
var commands: Array = ["msg", "call", "text", "skype", "kick", "invite"]

var searchResult: [AnyObject]?
var searchResult: [String]?

var pipWindow: UIWindow?

Expand Down Expand Up @@ -480,44 +480,46 @@ extension MessageViewController {

override func didChangeAutoCompletionPrefix(_ prefix: String, andWord word: String) {

var array: [AnyObject]?
var array:Array<String> = []
let wordPredicate = NSPredicate(format: "self BEGINSWITH[c] %@", word);

self.searchResult = nil

if prefix == "@" {
if word.characters.count > 0 {
array = (self.users as NSArray).filtered(using: NSPredicate(format: "self BEGINSWITH[c] %@", word)) as [AnyObject]?
array = self.users.filter { wordPredicate.evaluate(with: $0) };
}
else {
array = self.users as [AnyObject]?
array = self.users
}
}
else if prefix == "#" {

if word.characters.count > 0 {
array = (self.channels as NSArray).filtered(using: NSPredicate(format: "self BEGINSWITH[c] %@", word)) as [AnyObject]?
array = self.channels.filter { wordPredicate.evaluate(with: $0) };
}
else {
array = self.channels as [AnyObject]?
array = self.channels
}
}
else if (prefix == ":" || prefix == "+:") && word.characters.count > 0 {
array = (self.emojis as NSArray).filtered(using: NSPredicate(format: "self BEGINSWITH[c] %@", word)) as [AnyObject]?
array = self.emojis.filter { wordPredicate.evaluate(with: $0) };
}
else if prefix == "/" && self.foundPrefixRange.location == 0 {
if word.characters.count > 0 {
array = (self.commands as NSArray).filtered(using: NSPredicate(format: "self BEGINSWITH[c] %@", word)) as [AnyObject]?
array = self.commands.filter { wordPredicate.evaluate(with: $0) };
}
else {
array = self.commands as [AnyObject]?
array = self.commands
}
}

var show = false

if (array?.count)! > 0 {
self.searchResult = (array! as NSArray).sortedArray(using: #selector(NSString.localizedCaseInsensitiveCompare(_:))) as [AnyObject]
show = ((self.searchResult?.count)! > 0)
if array.count > 0 {
let sortedArray = array.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending }
self.searchResult = sortedArray
show = sortedArray.count > 0
}

self.showAutoCompletionView(show)
Expand Down Expand Up @@ -599,7 +601,7 @@ extension MessageViewController {
cell.indexPath = indexPath
cell.selectionStyle = .default

guard let searchResult = self.searchResult as? [String] else {
guard let searchResult = self.searchResult else {
return cell
}

Expand Down Expand Up @@ -668,7 +670,7 @@ extension MessageViewController {

if tableView == self.autoCompletionView {

guard let searchResult = self.searchResult as? [String] else {
guard let searchResult = self.searchResult else {
return
}

Expand Down
29 changes: 20 additions & 9 deletions Examples/Messenger.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0700;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Slack Technologies, Inc.";
TargetAttributes = {
4F3EDB48199ED00F004C15D6 = {
Expand Down Expand Up @@ -748,14 +748,19 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
COPY_PHASE_STRIP = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand All @@ -768,7 +773,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
Expand All @@ -788,21 +793,27 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand Down Expand Up @@ -868,7 +879,7 @@
"$(inherited)",
);
INFOPLIST_FILE = "Messenger-Storyboard/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.slack.Messenger;
Expand All @@ -888,7 +899,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
ENABLE_STRICT_OBJC_MSGSEND = YES;
INFOPLIST_FILE = "Messenger-Storyboard/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.slack.Messenger;
Expand All @@ -912,7 +923,7 @@
"$(inherited)",
);
INFOPLIST_FILE = "Messenger-Swift/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.slack.Messenger;
Expand All @@ -935,7 +946,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
ENABLE_STRICT_OBJC_MSGSEND = YES;
INFOPLIST_FILE = "Messenger-Swift/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.slack.Messenger;
Expand All @@ -958,7 +969,7 @@
"$(inherited)",
);
INFOPLIST_FILE = "$(SRCROOT)/Messenger-iPad-Sheet/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.slack.Messenger;
Expand All @@ -975,7 +986,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
INFOPLIST_FILE = "$(SRCROOT)/Messenger-iPad-Sheet/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MTL_ENABLE_DEBUG_INFO = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.slack.Messenger;
Expand Down

0 comments on commit 31da460

Please sign in to comment.