From 31da460a00fb764b880066fadc15b7c26f9ef3fa Mon Sep 17 00:00:00 2001 From: Ignacio Romero Zurbuchen Date: Mon, 3 Oct 2016 16:29:15 -0700 Subject: [PATCH] A bit cleaner implementation of primitive Swift Array filtering, to avoid many unwrapping and casting. --- .../MessageViewController.swift | 38 ++++++++++--------- Examples/Messenger.xcodeproj/project.pbxproj | 29 +++++++++----- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/Examples/Messenger-Swift/MessageViewController.swift b/Examples/Messenger-Swift/MessageViewController.swift index bcedb189..7dd7a7b1 100644 --- a/Examples/Messenger-Swift/MessageViewController.swift +++ b/Examples/Messenger-Swift/MessageViewController.swift @@ -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? @@ -480,44 +480,46 @@ extension MessageViewController { override func didChangeAutoCompletionPrefix(_ prefix: String, andWord word: String) { - var array: [AnyObject]? + var array:Array = [] + 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) @@ -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 } @@ -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 } diff --git a/Examples/Messenger.xcodeproj/project.pbxproj b/Examples/Messenger.xcodeproj/project.pbxproj index 49bd2c01..6d68c32b 100644 --- a/Examples/Messenger.xcodeproj/project.pbxproj +++ b/Examples/Messenger.xcodeproj/project.pbxproj @@ -422,7 +422,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Slack Technologies, Inc."; TargetAttributes = { 4F3EDB48199ED00F004C15D6 = { @@ -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", @@ -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"; @@ -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; }; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; @@ -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;