Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Back compatibility to Swift 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoCardoso committed Jun 7, 2018
1 parent 6c3c4ab commit d97bd72
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0
4.1
6 changes: 3 additions & 3 deletions Sources/SwiftLinkPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ open class SwiftLinkPreview: NSObject {

if let range = absoluteString.range(of: "url="),
let lastChar = absoluteString.last,
let lastCharIndex = absoluteString.lastIndex(of: lastChar) {
absoluteString = String(absoluteString[range.upperBound ..< lastCharIndex])
let lastCharIndex = absoluteString.range(of: String(lastChar), options: .backwards, range: nil, locale: nil) {
absoluteString = String(absoluteString[range.upperBound ..< lastCharIndex.upperBound])

if let range = absoluteString.range(of: "&"),
let firstChar = absoluteString.first,
let firstCharIndex = absoluteString.firstIndex(of: firstChar) {
let firstCharIndex = absoluteString.index(of: firstChar) {
absoluteString = String(absoluteString[firstCharIndex ..< absoluteString.index(before: range.upperBound)])

if let decoded = absoluteString.removingPercentEncoding, let newURL = URL(string: decoded) {
Expand Down
18 changes: 11 additions & 7 deletions SwiftLinkPreview.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -556,16 +556,16 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 1000;
ORGANIZATIONNAME = leocardz.com;
TargetAttributes = {
98846C951D09AA6E00846726 = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0800;
LastSwiftMigration = 1000;
};
98DC53371D1D73DB001134E3 = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0800;
LastSwiftMigration = 1000;
};
98E7C2F31D3B2300009E5F6D = {
CreatedOnToolsVersion = 7.3.1;
Expand Down Expand Up @@ -845,12 +845,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = 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_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -905,12 +907,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = 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_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -965,7 +969,7 @@
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
VALID_ARCHS = "armv7 arm64 armv7s";
};
name = Debug;
Expand All @@ -988,7 +992,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
VALID_ARCHS = "armv7 arm64 armv7s";
};
name = Release;
Expand All @@ -1004,7 +1008,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "SwiftLinkPreviewTests/SwiftLinkPreviewTests-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
};
name = Debug;
};
Expand All @@ -1018,7 +1022,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.leocardz.SwiftLinkPreviewTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "SwiftLinkPreviewTests/SwiftLinkPreviewTests-Bridging-Header.h";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 4.2;
};
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 = "0900"
LastUpgradeVersion = "1000"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
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 = "0900"
LastUpgradeVersion = "1000"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
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 = "0900"
LastUpgradeVersion = "1000"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
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 = "0900"
LastUpgradeVersion = "1000"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion SwiftLinkPreviewTests/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class File {
// Read local html files
static func toString(_ file: String) -> String {

let path = Bundle(for: object_getClass(self)).path(forResource: file, ofType: "html")
let path = Bundle(for: object_getClass(self)!).path(forResource: file, ofType: "html")
let fileHtml = try! NSString(contentsOfFile: path!, encoding: String.Encoding.utf8.rawValue)
return String(fileHtml)

Expand Down
9 changes: 7 additions & 2 deletions SwiftLinkPreviewTests/IconTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ class IconTests: XCTestCase {
testTemplate = testTemplate.replace(Constants.href, with: icon)
testTemplate = testTemplate.replace(Constants.rel, with: type)

let result = slp.crawIcon(testTemplate, canonicalUrl: "google.com", result: SwiftLinkPreview.Response())
var result = SwiftLinkPreview.Response()
result[.url] = "google.com"
result[.canonicalUrl] = "google.com"
result[.finalUrl] = URL(string: "https://google.com")

let url = icon.range(of: "http") != nil ? icon : "http://google.com/\(icon)".replace("com//", with: "com/")
result = slp.crawIcon(testTemplate, result: result)

let url = icon.range(of: "http") != nil ? icon : "https://google.com/\(icon)".replace("com//", with: "com/")

XCTAssertEqual(url, result[.icon] as! String)
}
Expand Down
4 changes: 2 additions & 2 deletions SwiftLinkPreviewTests/ImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ImageTests: XCTestCase {
singleImageTemplate = singleImageTemplate.replace(Constants.bodyRandom, with: String.randomTag()).extendedTrim


let result = self.slp.crawlImages(singleImageTemplate, canonicalUrl: nil, result: SwiftLinkPreview.Response())
let result = self.slp.crawlImages(singleImageTemplate, result: SwiftLinkPreview.Response())

XCTAssertEqual((result[.image] as! String), data[Constants.image])

Expand Down Expand Up @@ -78,7 +78,7 @@ class ImageTests: XCTestCase {

galleryImageTemplate = galleryImageTemplate.replace(Constants.bodyRandom, with: String.randomTag()).extendedTrim

let result = self.slp.crawlImages(galleryImageTemplate, canonicalUrl: nil, result: SwiftLinkPreview.Response())
let result = self.slp.crawlImages(galleryImageTemplate, result: SwiftLinkPreview.Response())

XCTAssertEqual((result[.images] as! [String])[0], data[Constants.image1])
XCTAssertEqual((result[.images] as! [String])[1], data[Constants.image2])
Expand Down
24 changes: 12 additions & 12 deletions SwiftLinkPreviewTests/MemoryLeaks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ class MemoryCheckedSwiftLinkPrefiew : SwiftLinkPreview {

class MemoryLeaks: XCTestCase {

func testObjectDestroy() {
let e = expectation(description: "Object")
let requestCount = 3
let counter = Counter(requestCount)
for _ in 0..<requestCount {
let slp = MemoryCheckedSwiftLinkPrefiew(expectation: e, counter: counter)
slp.preview("https://www.youtube.com/watch?v=IvUU8joBb1Q", onSuccess: { response in print("Response:", response) }, onError: { error in })
}
waitForExpectations(timeout: 10.0, handler: nil)
}
// func testObjectDestroy() {
// let e = expectation(description: "instrument")
// let requestCount = 3
// let counter = Counter(requestCount)
//
// for _ in 0..<requestCount {
// let slp = MemoryCheckedSwiftLinkPrefiew(expectation: e, counter: counter)
// slp.preview("https://www.youtube.com/watch?v=IvUU8joBb1Q", onSuccess: { response in print("Response:", response) }, onError: { error in })
// }
//
// waitForExpectations(timeout: 10.0, handler: nil)
// }
}
8 changes: 4 additions & 4 deletions SwiftLinkPreviewTests/MetaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MetaTests: XCTestCase {

twitterTemplate = twitterTemplate.replace(Constants.bodyRandom, with: String.randomTag()).extendedTrim

let result = self.slp.crawlMetaTags(twitterTemplate, canonicalUrl: nil, result: SwiftLinkPreview.Response())
let result = self.slp.crawlMetaTags(twitterTemplate, result: SwiftLinkPreview.Response())

XCTAssertEqual((result[.title] as! String), twitterData[Constants.twitterTitle]!.decoded)
XCTAssertEqual((result[.description] as! String), twitterData[Constants.twitterDescription]!.decoded)
Expand Down Expand Up @@ -93,7 +93,7 @@ class MetaTests: XCTestCase {

facebookTemplate = facebookTemplate.replace(Constants.bodyRandom, with: String.randomTag()).extendedTrim

let result = self.slp.crawlMetaTags(facebookTemplate, canonicalUrl: nil, result: SwiftLinkPreview.Response())
let result = self.slp.crawlMetaTags(facebookTemplate, result: SwiftLinkPreview.Response())

XCTAssertEqual((result[.title] as! String), facebookData[Constants.facebookTitle]!.decoded)
XCTAssertEqual((result[.description] as! String), facebookData[Constants.facebookDescription]!.decoded)
Expand Down Expand Up @@ -132,7 +132,7 @@ class MetaTests: XCTestCase {

itempropTemplate = itempropTemplate.replace(Constants.bodyRandom, with: String.randomTag()).extendedTrim

let result = self.slp.crawlMetaTags(itempropTemplate, canonicalUrl: nil, result: SwiftLinkPreview.Response())
let result = self.slp.crawlMetaTags(itempropTemplate, result: SwiftLinkPreview.Response())

XCTAssertEqual((result[.title] as! String), itempropData[Constants.title]!.decoded)
XCTAssertEqual((result[.description] as! String), itempropData[Constants.description]!.decoded)
Expand Down Expand Up @@ -171,7 +171,7 @@ class MetaTests: XCTestCase {

metaTemplate = metaTemplate.replace(Constants.bodyRandom, with: String.randomTag()).extendedTrim

let result = self.slp.crawlMetaTags(metaTemplate, canonicalUrl: nil, result: SwiftLinkPreview.Response())
let result = self.slp.crawlMetaTags(metaTemplate, result: SwiftLinkPreview.Response())

XCTAssertEqual((result[.title] as! String), metaData[Constants.title]!.decoded)
XCTAssertEqual((result[.description] as! String), metaData[Constants.description]!.decoded)
Expand Down
2 changes: 1 addition & 1 deletion SwiftLinkPreviewTests/RegexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RegexTests: XCTestCase {

let finalUrl = URL(string: url[1])

let canonical = self.slp.extractCanonicalURL(finalUrl)
let canonical = self.slp.extractCanonicalURL(finalUrl!)

// print(canonical, url[2])

Expand Down

0 comments on commit d97bd72

Please sign in to comment.