diff --git a/CalculatorKeyboard.xcodeproj/project.pbxproj b/CalculatorKeyboard.xcodeproj/project.pbxproj index f331d01..907cd8a 100644 --- a/CalculatorKeyboard.xcodeproj/project.pbxproj +++ b/CalculatorKeyboard.xcodeproj/project.pbxproj @@ -155,7 +155,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0710; - LastUpgradeCheck = 0710; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Reefactor, Inc."; TargetAttributes = { 45C5244D1C120F4E00510CF7 = { @@ -240,13 +240,21 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; 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_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -269,11 +277,12 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.1; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -288,13 +297,21 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; 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_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -311,9 +328,11 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.1; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; @@ -325,6 +344,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_MODULES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -336,6 +356,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -343,6 +364,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_MODULES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -353,6 +375,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.reefactor.CalculatorKeyboard; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_VERSION = 4.0; }; name = Release; }; diff --git a/CalculatorKeyboard.xcodeproj/xcshareddata/xcschemes/CalculatorKeyboard.xcscheme b/CalculatorKeyboard.xcodeproj/xcshareddata/xcschemes/CalculatorKeyboard.xcscheme index d160922..bfd4d8d 100644 --- a/CalculatorKeyboard.xcodeproj/xcshareddata/xcschemes/CalculatorKeyboard.xcscheme +++ b/CalculatorKeyboard.xcodeproj/xcshareddata/xcschemes/CalculatorKeyboard.xcscheme @@ -1,6 +1,6 @@ UIView { - let bundle = NSBundle(forClass: self.dynamicType) + let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: "CalculatorKeyboard", bundle: bundle) - let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView - adjustButtonConstraint() + let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView return view } private func adjustLayout() { - if viewWithTag(CalculatorKey.Decimal.rawValue) != nil { - adjustButtonConstraint() - } - - for var i = 1; i <= CalculatorKey.Decimal.rawValue; i++ { + for i in 1...CalculatorKey.Decimal.rawValue { if let button = self.view.viewWithTag(i) as? UIButton { button.tintColor = numbersBackgroundColor - button.setTitleColor(numbersTextColor, forState: .Normal) + button.setTitleColor(numbersTextColor, for: .normal) } } - for var i = CalculatorKey.Clear.rawValue; i <= CalculatorKey.Add.rawValue; i++ { + for i in CalculatorKey.Clear.rawValue...CalculatorKey.Add.rawValue { if let button = self.view.viewWithTag(i) as? UIButton { button.tintColor = operationsBackgroundColor - button.setTitleColor(operationsTextColor, forState: .Normal) + button.setTitleColor(operationsTextColor, for: .normal) button.tintColor = operationsTextColor } } if let button = self.view.viewWithTag(CalculatorKey.Equal.rawValue) as? UIButton { button.tintColor = equalBackgroundColor - button.setTitleColor(equalTextColor, forState: .Normal) + button.setTitleColor(equalTextColor, for: .normal) } } - private func adjustButtonConstraint() { - let width = UIScreen.mainScreen().bounds.width / 4.0 - zeroDistanceConstraint.constant = showDecimal ? width + 2.0 : 1.0 - layoutIfNeeded() - } - - @IBAction func buttonPressed(sender: UIButton) { + @IBAction func buttonPressed(_ sender: UIButton) { switch (sender.tag) { + case CalculatorKey.Sign.rawValue: + let output = processor.toggleSign() + delegate?.calculator(self, didChangeValue: output) case (CalculatorKey.Zero.rawValue)...(CalculatorKey.Nine.rawValue): - let output = processor.storeOperand(sender.tag-1) + let output = processor.storeOperand(value: sender.tag - 1) delegate?.calculator(self, didChangeValue: output) case CalculatorKey.Decimal.rawValue: let output = processor.addDecimal() @@ -156,7 +153,7 @@ public class CalculatorKeyboard: UIView { let output = processor.deleteLastDigit() delegate?.calculator(self, didChangeValue: output) case (CalculatorKey.Multiply.rawValue)...(CalculatorKey.Add.rawValue): - let output = processor.storeOperator(sender.tag) + let output = processor.storeOperator(rawValue: sender.tag) delegate?.calculator(self, didChangeValue: output) case CalculatorKey.Equal.rawValue: let output = processor.computeFinalValue() diff --git a/CalculatorKeyboard/CalculatorKeyboard.xib b/CalculatorKeyboard/CalculatorKeyboard.xib index d5e5285..0c82a90 100644 --- a/CalculatorKeyboard/CalculatorKeyboard.xib +++ b/CalculatorKeyboard/CalculatorKeyboard.xib @@ -1,322 +1,319 @@ - - + + + + + - + + - - - - - + - + - - - + + - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/CalculatorKeyboard/CalculatorProcessor.swift b/CalculatorKeyboard/CalculatorProcessor.swift index 3b172d2..0fc0122 100644 --- a/CalculatorKeyboard/CalculatorProcessor.swift +++ b/CalculatorKeyboard/CalculatorProcessor.swift @@ -23,6 +23,18 @@ class CalculatorProcessor { var decimalDigit = 0 + func toggleSign() -> String { + if currentOperand != "0" { + if currentOperand.starts(with: "-") { + let startIndex = currentOperand.index(currentOperand.startIndex, offsetBy: 1) + currentOperand = String(currentOperand[startIndex...]) + } else { + currentOperand = "-" + currentOperand + } + } + return currentOperand + } + func storeOperand(value: Int) -> String { let operand = "\(value)" if currentOperand == "0" { @@ -41,12 +53,12 @@ class CalculatorProcessor { } if automaticDecimal { - currentOperand = currentOperand.stringByReplacingOccurrencesOfString(decimalSymbol(), withString: "") + currentOperand = currentOperand.replacingOccurrences(of: decimalSymbol(), with: "") if currentOperand[currentOperand.startIndex] == "0" { - currentOperand.removeAtIndex(currentOperand.startIndex) + currentOperand.remove(at: currentOperand.startIndex) } let char = decimalSymbol()[decimalSymbol().startIndex] - currentOperand.insert(char, atIndex: currentOperand.endIndex.advancedBy(-2)) + currentOperand.insert(char, at: currentOperand.index(currentOperand.startIndex, offsetBy: -2)) } return currentOperand @@ -54,15 +66,15 @@ class CalculatorProcessor { func deleteLastDigit() -> String { if currentOperand.characters.count > 1 { - currentOperand.removeAtIndex(currentOperand.endIndex.predecessor()) + currentOperand.remove(at: currentOperand.index(before: currentOperand.endIndex)) if automaticDecimal { - currentOperand = currentOperand.stringByReplacingOccurrencesOfString(decimalSymbol(), withString: "") + currentOperand = currentOperand.replacingOccurrences(of: decimalSymbol(), with: "") if currentOperand.characters.count < 3 { - currentOperand.insert("0", atIndex: currentOperand.startIndex) + currentOperand.insert("0", at: currentOperand.startIndex) } let char = decimalSymbol()[decimalSymbol().startIndex] - currentOperand.insert(char, atIndex: currentOperand.endIndex.advancedBy(-2)) + currentOperand.insert(char, at: currentOperand.index(currentOperand.startIndex, offsetBy: -2)) } } else { currentOperand = resetOperand() @@ -72,7 +84,7 @@ class CalculatorProcessor { } func addDecimal() -> String { - if currentOperand.rangeOfString(decimalSymbol()) == nil { + if currentOperand.range(of: decimalSymbol()) == nil { currentOperand += decimalSymbol() } return currentOperand @@ -110,7 +122,7 @@ class CalculatorProcessor { default: break } - currentOperand = formatValue(output) + currentOperand = formatValue(value: output) } previousOperand = resetOperand() storedOperator = .Equal @@ -131,7 +143,7 @@ class CalculatorProcessor { private func resetOperand() -> String { var operand = "0" if automaticDecimal { - operand = convertOperandToDecimals(operand) + operand = convertOperandToDecimals(operand: operand) } return operand } @@ -146,12 +158,12 @@ class CalculatorProcessor { raw = String(format: "%.2f", value) return raw } else { - var end = raw.endIndex.predecessor() + var end = raw.index(before: raw.endIndex) var foundDecimal = false - while end != raw.startIndex && (raw[end] == "0" || isDecimal(raw[end])) && !foundDecimal { - foundDecimal = isDecimal(raw[end]) - raw.removeAtIndex(end) - end = end.predecessor() + while end != raw.startIndex && (raw[end] == "0" || isDecimal(char: raw[end])) && !foundDecimal { + foundDecimal = isDecimal(char: raw[end]) + raw.remove(at: end) + end = raw.index(before: raw.endIndex) } return raw } diff --git a/CalculatorKeyboard/Info.plist b/CalculatorKeyboard/Info.plist index 09bc972..331a2da 100644 --- a/CalculatorKeyboard/Info.plist +++ b/CalculatorKeyboard/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 1.1.0 + 1.2.0 CFBundleSignature ???? CFBundleVersion diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index bd13831..d02c432 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -378,6 +378,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -414,6 +415,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.4; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_VERSION = 4.0; VALIDATE_PRODUCT = YES; }; name = Release; diff --git a/Example/Example/AppDelegate.swift b/Example/Example/AppDelegate.swift index e6d780b..5024eab 100644 --- a/Example/Example/AppDelegate.swift +++ b/Example/Example/AppDelegate.swift @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> 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:. } diff --git a/Example/Example/Base.lproj/Main.storyboard b/Example/Example/Base.lproj/Main.storyboard index 119e969..5b4415a 100644 --- a/Example/Example/Base.lproj/Main.storyboard +++ b/Example/Example/Base.lproj/Main.storyboard @@ -1,7 +1,12 @@ - - + + + + + - + + + @@ -13,12 +18,11 @@ - + - - + @@ -26,8 +30,7 @@ - - + diff --git a/Example/Example/Info.plist b/Example/Example/Info.plist index 7668538..7192165 100644 --- a/Example/Example/Info.plist +++ b/Example/Example/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.1.0 + 1.2.0 CFBundleSignature ???? CFBundleVersion diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index d48d34b..bec1a8d 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -15,14 +15,14 @@ class ViewController: UIViewController, CalculatorDelegate { override func viewDidLoad() { super.viewDidLoad() - let frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 300) + let frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 300) let keyboard = CalculatorKeyboard(frame: frame) keyboard.delegate = self keyboard.showDecimal = true inputTextField.inputView = keyboard } - override func viewDidAppear(animated: Bool) { + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) inputTextField.becomeFirstResponder() } @@ -32,7 +32,7 @@ class ViewController: UIViewController, CalculatorDelegate { } // MARK: - RFCalculatorKeyboard delegate - func calculator(calculator: CalculatorKeyboard, didChangeValue value: String) { + func calculator(_: CalculatorKeyboard, didChangeValue value: String) { inputTextField.text = value } }