Skip to content

Commit

Permalink
Merge pull request #2 from SandroMachado/feature/swift-3
Browse files Browse the repository at this point in the history
Update to Swift 3
  • Loading branch information
SandroMachado authored Sep 18, 2017
2 parents d7704ee + 8a5e873 commit 8a6f4d8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 44 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: objective-c
osx_image: xcode7.3
osx_image: xcode8.3

script:
- open -b com.apple.iphonesimulator
- xcodebuild clean build test -project BitcoinPaymentURI.xcodeproj -scheme BitcoinPaymentURI -sdk iphonesimulator CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO
- xcodebuild clean build test -project BitcoinPaymentURI.xcodeproj -scheme BitcoinPaymentURI -destination "platform=iOS Simulator,name=iPhone 7" CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO
15 changes: 14 additions & 1 deletion BitcoinPaymentURI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,16 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0730;
LastUpgradeCheck = 0830;
ORGANIZATIONNAME = Sandro;
TargetAttributes = {
2AC1541A1D3461170055114B = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0830;
};
2AC154241D3461170055114B = {
CreatedOnToolsVersion = 7.3.1;
LastSwiftMigration = 0830;
};
};
};
Expand Down Expand Up @@ -258,8 +260,10 @@
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[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -307,8 +311,10 @@
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[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -328,6 +334,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -339,6 +346,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -350,13 +358,15 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
2AC154311D3461170055114B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -367,6 +377,7 @@
PRODUCT_BUNDLE_IDENTIFIER = sandro.BitcoinPaymentURI;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -377,6 +388,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = sandro.BitcoinPaymentURITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -387,6 +399,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = sandro.BitcoinPaymentURITests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0830"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
68 changes: 34 additions & 34 deletions BitcoinPaymentURI/BitcoinPaymentURI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@
import Foundation

/// The Bitcoin Payment URI.
public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {
open class BitcoinPaymentURI: BitcoinPaymentURIProtocol {

/// Closure to do the builder.
typealias buildBitcoinPaymentURIClosure = (BitcoinPaymentURI) -> Void

private static let SCHEME = "bitcoin:"
private static let PARAMETER_AMOUNT = "amount"
private static let PARAMETER_LABEL = "label"
private static let PARAMETER_MESSAGE = "message"
private static let PARAMETER_REQUIRED_PREFIX = "req-"
fileprivate static let SCHEME = "bitcoin:"
fileprivate static let PARAMETER_AMOUNT = "amount"
fileprivate static let PARAMETER_LABEL = "label"
fileprivate static let PARAMETER_MESSAGE = "message"
fileprivate static let PARAMETER_REQUIRED_PREFIX = "req-"

private var allParameters: [String: Parameter]?
fileprivate var allParameters: [String: Parameter]?

/// The address.
public var address: String?
open var address: String?

/// The amount.
public var amount: Double? {
open var amount: Double? {
set(newValue) {
guard let newValue = newValue else {
return
Expand All @@ -36,7 +36,7 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {
}

get {
guard let parameters = self.allParameters, amount = parameters[BitcoinPaymentURI.PARAMETER_AMOUNT]?.value else {
guard let parameters = self.allParameters, let amount = parameters[BitcoinPaymentURI.PARAMETER_AMOUNT]?.value else {
return nil
}

Expand All @@ -45,7 +45,7 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {
}

/// The label.
public var label: String? {
open var label: String? {
set(newValue) {
guard let newValue = newValue else {
return
Expand All @@ -55,7 +55,7 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {
}

get {
guard let parameters = self.allParameters, label = parameters[BitcoinPaymentURI.PARAMETER_LABEL]?.value else {
guard let parameters = self.allParameters, let label = parameters[BitcoinPaymentURI.PARAMETER_LABEL]?.value else {
return nil
}

Expand All @@ -64,7 +64,7 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {
}

/// The message.
public var message: String? {
open var message: String? {
set(newValue) {
guard let newValue = newValue else {
return
Expand All @@ -74,7 +74,7 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {
}

get {
guard let parameters = self.allParameters, label = parameters[BitcoinPaymentURI.PARAMETER_MESSAGE]?.value else {
guard let parameters = self.allParameters, let label = parameters[BitcoinPaymentURI.PARAMETER_MESSAGE]?.value else {
return nil
}

Expand All @@ -83,11 +83,11 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {
}

/// The parameters.
public var parameters: [String: Parameter]? {
open var parameters: [String: Parameter]? {
set(newValue) {
var newParameters: [String: Parameter] = [:]

guard let allParameters = self.allParameters, newValue = newValue else {
guard let allParameters = self.allParameters, let newValue = newValue else {
return
}

Expand All @@ -107,20 +107,20 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {
return nil
}

parametersFiltered.removeValueForKey(BitcoinPaymentURI.PARAMETER_AMOUNT)
parametersFiltered.removeValueForKey(BitcoinPaymentURI.PARAMETER_LABEL)
parametersFiltered.removeValueForKey(BitcoinPaymentURI.PARAMETER_MESSAGE)
parametersFiltered.removeValue(forKey: BitcoinPaymentURI.PARAMETER_AMOUNT)
parametersFiltered.removeValue(forKey: BitcoinPaymentURI.PARAMETER_LABEL)
parametersFiltered.removeValue(forKey: BitcoinPaymentURI.PARAMETER_MESSAGE)

return parametersFiltered
}
}

// The uri.
public var uri: String? {
open var uri: String? {
get {
let urlComponents = NSURLComponents()
urlComponents.scheme = BitcoinPaymentURI.SCHEME.stringByReplacingOccurrencesOfString(":", withString: "");
urlComponents.path = self.address;
var urlComponents = URLComponents()
urlComponents.scheme = BitcoinPaymentURI.SCHEME.replacingOccurrences(of: ":", with: "");
urlComponents.path = self.address!;
urlComponents.queryItems = []

guard let allParameters = self.allParameters else {
Expand All @@ -129,12 +129,12 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {

for (key, value) in allParameters {
if (value.required) {
urlComponents.queryItems?.append(NSURLQueryItem(name: "\(BitcoinPaymentURI.PARAMETER_REQUIRED_PREFIX)\(key)", value: value.value))
urlComponents.queryItems?.append(URLQueryItem(name: "\(BitcoinPaymentURI.PARAMETER_REQUIRED_PREFIX)\(key)", value: value.value))

continue
}

urlComponents.queryItems?.append(NSURLQueryItem(name: key, value: value.value))
urlComponents.queryItems?.append(URLQueryItem(name: key, value: value.value))
}

return urlComponents.string
Expand All @@ -159,17 +159,17 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {

- returns: a BitcoinPaymentURI.
*/
public static func parse(bitcoinPaymentURI: String) -> BitcoinPaymentURI? {
let schemeRange = Range<String.Index>(bitcoinPaymentURI.startIndex.advancedBy(0)..<bitcoinPaymentURI.startIndex.advancedBy(SCHEME.characters.count))
let paramReqRange = Range<String.Index>(bitcoinPaymentURI.startIndex.advancedBy(0)..<bitcoinPaymentURI.startIndex.advancedBy(PARAMETER_REQUIRED_PREFIX.characters.count))
open static func parse(_ bitcoinPaymentURI: String) -> BitcoinPaymentURI? {
let schemeRange = Range<String.Index>(bitcoinPaymentURI.characters.index(bitcoinPaymentURI.startIndex, offsetBy: 0)..<bitcoinPaymentURI.characters.index(bitcoinPaymentURI.startIndex, offsetBy: SCHEME.characters.count))
let paramReqRange = Range<String.Index>(bitcoinPaymentURI.characters.index(bitcoinPaymentURI.startIndex, offsetBy: 0)..<bitcoinPaymentURI.characters.index(bitcoinPaymentURI.startIndex, offsetBy: PARAMETER_REQUIRED_PREFIX.characters.count))

guard let _ = bitcoinPaymentURI.rangeOfString(SCHEME, options: NSStringCompareOptions.CaseInsensitiveSearch, range: schemeRange) else {
guard let _ = bitcoinPaymentURI.range(of: SCHEME, options: NSString.CompareOptions.caseInsensitive, range: schemeRange) else {
return nil
}

let urlComponents = NSURLComponents(string: String(bitcoinPaymentURI))
let urlComponents = URLComponents(string: String(bitcoinPaymentURI))

guard let address = urlComponents?.path where !address.isEmpty else {
guard let address = urlComponents?.path, !address.isEmpty else {
return nil
}

Expand All @@ -185,16 +185,16 @@ public class BitcoinPaymentURI: BitcoinPaymentURIProtocol {

var required: Bool = true

if (queryItem.name.characters.count <= PARAMETER_REQUIRED_PREFIX.characters.count || queryItem.name.rangeOfString(PARAMETER_REQUIRED_PREFIX, options: NSStringCompareOptions.CaseInsensitiveSearch, range: paramReqRange) == nil) {
if (queryItem.name.characters.count <= PARAMETER_REQUIRED_PREFIX.characters.count || queryItem.name.range(of: PARAMETER_REQUIRED_PREFIX, options: NSString.CompareOptions.caseInsensitive, range: paramReqRange) == nil) {
required = false
}

newParameters[queryItem.name.stringByReplacingOccurrencesOfString(PARAMETER_REQUIRED_PREFIX, withString: "")] = Parameter(value: value, required: required)
newParameters[queryItem.name.replacingOccurrences(of: PARAMETER_REQUIRED_PREFIX, with: "")] = Parameter(value: value, required: required)
}
}

$0.parameters = newParameters
})
}

}
}
8 changes: 4 additions & 4 deletions BitcoinPaymentURI/Model/Parameter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import Foundation

/// Parameter model.
public class Parameter {
open class Parameter {

/// The paramenter value.
public private(set) var value: String
open fileprivate(set) var value: String

/// A boolean indicating if the parameter is required.
public private(set) var required: Bool
open fileprivate(set) var required: Bool

/**
Constructor.
Expand All @@ -28,4 +28,4 @@ public class Parameter {
self.required = required
}

}
}
2 changes: 1 addition & 1 deletion BitcoinPaymentURITests/BitcoinPaymentURITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class BitcoinPaymentURITests: XCTestCase {
XCTAssertEqual(bpuri.parameters?["foo"]?.required, false, "Failed: Wrong value.")
XCTAssertEqual(bpuri.parameters?["fiz"]?.value, "biz", "Failed: Wrong value.")
XCTAssertEqual(bpuri.parameters?["fiz"]?.required, true, "Failed: Wrong value.")
XCTAssertEqual(bpuri.uri, "bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?foo=bar&message=Donation%20for%20project%20xyz&req-fiz=biz&label=Luke-Jr&amount=50.0", "Failed: Wrong value.")
XCTAssertEqual(bpuri.uri, "bitcoin:175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W?label=Luke-Jr&foo=bar&message=Donation%20for%20project%20xyz&amount=50.0&req-fiz=biz", "Failed: Wrong value.")
}

}

0 comments on commit 8a6f4d8

Please sign in to comment.