Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to Swift 3. #613

Merged
merged 1 commit into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Ably.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Pod::Spec.new do |s|
s.version = "1.0.4"
s.summary = "iOS client for Ably"
s.description = <<-DESC
iOS client library for ably.io, the realtime messaging service, written in Objective-C and ready for Swift 2.0.
iOS client library for ably.io, the realtime messaging service, written in Objective-C and ready for Swift 3.0.
DESC
s.homepage = "https://www.ably.io"
s.license = { :type => 'Apache 2.0', :file => 'LICENSE' }
Expand Down
176 changes: 82 additions & 94 deletions Ably.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ end

def test_pods
project_pods
pod 'Quick', '0.9.3'
pod 'Nimble', '4.1.0'
pod 'Quick', '1.1.0'
pod 'Nimble', '7.0.0'
# Helpers
pod 'Aspects'
pod 'SwiftyJSON', '2.4.0'
pod 'SwiftyJSON', '3.1.4'
end

target 'Ably' do
Expand Down
22 changes: 11 additions & 11 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,29 @@ PODS:
- KSCrashAblyFork/Reporting/Tools (1.15.8-ably-1):
- KSCrashAblyFork/Recording
- msgpack (0.1.8)
- Nimble (4.1.0)
- Quick (0.9.3)
- Nimble (7.0.0)
- Quick (1.1.0)
- SocketRocket (0.5.1)
- SwiftyJSON (2.4.0)
- SwiftyJSON (3.1.4)

DEPENDENCIES:
- Aspects
- KSCrashAblyFork (= 1.15.8-ably-1)
- msgpack (= 0.1.8)
- Nimble (= 4.1.0)
- Quick (= 0.9.3)
- Nimble (= 7.0.0)
- Quick (= 1.1.0)
- SocketRocket (= 0.5.1)
- SwiftyJSON (= 2.4.0)
- SwiftyJSON (= 3.1.4)

SPEC CHECKSUMS:
Aspects: 7595ba96a6727a58ebcbfc954497fc5d2fdde546
KSCrashAblyFork: 6d0dd5b033710109a8fdde28103eeb0e7f9ba1f7
msgpack: 97491d2ea799408f4694f2c7d7fd79baf77853dd
Nimble: 97a0a4cae5124c117115634b2d055d8c97d0af19
Quick: 13a2a2b19a5d8e3ed4fd0c36ee46597fd77ebf71
Nimble: 874982b605d4d752fcac6be695d13c502de84b1b
Quick: dafc587e21eed9f4cab3249b9f9015b0b7a7f71d
SocketRocket: d57c7159b83c3c6655745cd15302aa24b6bae531
SwiftyJSON: 96918c1bf505efa50c4f72957018dd3452090c9c
SwiftyJSON: c2842d878f95482ffceec5709abc3d05680c0220

PODFILE CHECKSUM: 160cf53ef13d4ed09e5e81c2f2840457b07e012a
PODFILE CHECKSUM: 51df8c07c1f9b8c5dc1c7d7b7c47457ddfc72d30

COCOAPODS: 1.2.1
COCOAPODS: 1.2.0
1 change: 0 additions & 1 deletion Source/ARTWebSocketTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reas
break;
case ARTWsBuggyClose:
case ARTWsGoingAway:
case ARTWsAbnormalClose:
// Connectivity issue
[s.delegate realtimeTransportDisconnected:s withError:nil];
break;
Expand Down
553 changes: 277 additions & 276 deletions Spec/Auth.swift

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions Spec/Crypto.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Crypto : QuickSpec {
override func spec() {
describe("Crypto") {
let key = "+/h4eHh4eHh4eHh4eHh4eA=="
let binaryKey = NSData(base64EncodedString: key, options: NSDataBase64DecodingOptions.init(rawValue: 0))!
var longKey = NSMutableData(data: binaryKey)
longKey.appendData(binaryKey)
let binaryKey = NSData(base64Encoded: key, options: NSData.Base64DecodingOptions.init(rawValue: 0))!
var longKey = NSMutableData(data: binaryKey as Data)
longKey.append(binaryKey as Data)

// RSE1
context("getDefaultParams") {
Expand All @@ -29,7 +29,7 @@ class Crypto : QuickSpec {
"key": key
])
expect(params.algorithm).to(equal("AES"))
expect(params.key).to(equal(binaryKey))
expect(params.key).to(equal(binaryKey as Data))
expect(params.keyLength).to(equal(128))
expect(params.mode).to(equal("CBC"))

Expand All @@ -38,7 +38,7 @@ class Crypto : QuickSpec {
"algorithm": "DES"
])
expect(params.algorithm).to(equal("DES"))
expect(params.key).to(equal(longKey))
expect(params.key).to(equal(longKey as Data))
expect(params.keyLength).to(equal(256))
expect(params.mode).to(equal("CBC"))
}
Expand All @@ -47,18 +47,18 @@ class Crypto : QuickSpec {
context("key parameter") {
it("can be a binary") {
let params = ARTCrypto.getDefaultParams(["key": binaryKey])
expect(params.key).to(equal(binaryKey))
expect(params.key).to(equal(binaryKey as Data))
}

it("can be a base64-encoded string with standard encoding") {
let params = ARTCrypto.getDefaultParams(["key": key])
expect(params.key).to(equal(binaryKey))
expect(params.key).to(equal(binaryKey as Data))
}

it("can be a base64-encoded string with URL encoding") {
let key = "-_h4eHh4eHh4eHh4eHh4eA=="
let params = ARTCrypto.getDefaultParams(["key": key])
expect(params.key).to(equal(binaryKey))
expect(params.key).to(equal(binaryKey as Data))
}
}

Expand All @@ -74,7 +74,7 @@ class Crypto : QuickSpec {
// RSE1e
it("should check that keyLength is valid for algorithm") {
expect{ARTCrypto.getDefaultParams([
"key": binaryKey.subdataWithRange(NSMakeRange(0, 10))
"key": binaryKey.subdata(with: NSMakeRange(0, 10))
])}.to(raiseException())
}

Expand All @@ -84,25 +84,25 @@ class Crypto : QuickSpec {
context("generateRandomKey") {
// RSE2a, RSE2b
it("takes a single length argument and returns a binary") {
var key: NSData = ARTCrypto.generateRandomKey(128)
var key: NSData = ARTCrypto.generateRandomKey(128) as NSData
expect(key.length).to(equal(128 / 8))

key = ARTCrypto.generateRandomKey(256)
key = ARTCrypto.generateRandomKey(256) as NSData
expect(key.length).to(equal(256 / 8))
}

// RSE2a, RSE2b
it("takes no arguments and returns the default algorithm's default length") {
let key: NSData = ARTCrypto.generateRandomKey()
let key: NSData = ARTCrypto.generateRandomKey() as NSData
expect(key.length).to(equal(256 / 8))
}
}

context("encrypt") {
it("should generate a new IV every time it's called, and should be the first block encrypted") {
let params = ARTCipherParams(algorithm: "aes", key: key)
let cipher = ARTCrypto.cipherWithParams(params)
let data = "data".dataUsingEncoding(NSUTF8StringEncoding)!
let params = ARTCipherParams(algorithm: "aes", key: key as ARTCipherKeyCompatible)
let cipher = ARTCrypto.cipher(with: params)
let data = "data".data(using: String.Encoding.utf8)!

var distinctOutputs = Set<NSData>()
var output: NSData?
Expand All @@ -111,10 +111,10 @@ class Crypto : QuickSpec {
cipher.encrypt(data, output:&output)
distinctOutputs.insert(output!)

let firstBlock = output!.subdataWithRange(NSMakeRange(0, Int((cipher as! ARTCbcCipher).blockLength)))
let paramsWithIV = ARTCipherParams(algorithm: "aes", key: key, iv: firstBlock)
let firstBlock = output!.subdata(with: NSMakeRange(0, Int((cipher as! ARTCbcCipher).blockLength)))
let paramsWithIV = ARTCipherParams(algorithm: "aes", key: key as ARTCipherKeyCompatible, iv: firstBlock)
var sameOutput: NSData?
ARTCrypto.cipherWithParams(paramsWithIV).encrypt(data, output:&sameOutput)
ARTCrypto.cipher(with: paramsWithIV).encrypt(data, output:&sameOutput)

expect(output!).to(equal(sameOutput!))
}
Expand All @@ -127,10 +127,10 @@ class Crypto : QuickSpec {
context("with fixtures from \(cryptoTest).json") {
let (key, iv, items) = AblyTests.loadCryptoTestData(cryptoTest)
let decoder = ARTDataEncoder.init(cipherParams: nil, error: nil)
let cipherParams = ARTCipherParams(algorithm: "aes", key: key, iv: iv)
let cipherParams = ARTCipherParams(algorithm: "aes", key: key as ARTCipherKeyCompatible, iv: iv)
let encrypter = ARTDataEncoder.init(cipherParams: cipherParams, error: nil)

func extractMessage(fixture: AblyTests.CryptoTestItem.TestMessage) -> ARTMessage {
func extractMessage(_ fixture: AblyTests.CryptoTestItem.TestMessage) -> ARTMessage {
let msg = ARTMessage(name: fixture.name, data: fixture.data)
msg.encoding = fixture.encoding
return msg
Expand All @@ -142,10 +142,10 @@ class Crypto : QuickSpec {
let encryptedFixture = extractMessage(item.encrypted)

var error: NSError?
let decoded = fixture.decodeWithEncoder(decoder, error: &error) as! ARTMessage
let decoded = fixture.decode(with: decoder, error: &error) as! ARTMessage
expect(error).to(beNil())

let encrypted = decoded.encodeWithEncoder(encrypter, error: &error)
let encrypted = decoded.encode(with: encrypter, error: &error)
expect(error).to(beNil())

expect(encrypted as? ARTMessage).to(equal(encryptedFixture))
Expand All @@ -158,10 +158,10 @@ class Crypto : QuickSpec {
let encryptedFixture = extractMessage(item.encrypted)

var error: NSError?
let decoded = fixture.decodeWithEncoder(decoder, error: &error) as! ARTMessage
let decoded = fixture.decode(with: decoder, error: &error) as! ARTMessage
expect(error).to(beNil())

let decrypted = encryptedFixture.decodeWithEncoder(encrypter, error: &error)
let decrypted = encryptedFixture.decode(with: encrypter, error: &error)
expect(error).to(beNil())

expect(decrypted as? ARTMessage).to(equal(decoded))
Expand All @@ -171,4 +171,4 @@ class Crypto : QuickSpec {
}
}
}
}
}
2 changes: 1 addition & 1 deletion Spec/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let JSONStringToData: String -> NSData? = {
}

let JSONDataToAny: NSData -> AnyObject? = {
try? NSJSONSerialization.JSONObjectWithData($0, options: .MutableLeaves)
try? NSJSONSerialization.JSONObjectWithData($0, options: .mutableLeaves)
}

/**
Expand Down
8 changes: 4 additions & 4 deletions Spec/NSObject+TestSuite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import Aspects
extension NSObject {

/// Inject a block of code to the identified class method.
class func testSuite_injectIntoClassMethod(selector: Selector, code: ()->()) -> AspectToken? {
class func testSuite_injectIntoClassMethod(_ selector: Selector, code: @escaping ()->()) -> AspectToken? {
let block: @convention(block) (AspectInfo) -> Void = { _ in
code()
}
return try? self.aspect_hookSelector(selector, withOptions: .PositionAfter, usingBlock: unsafeBitCast(block, AnyObject.self))
return try? self.aspect_hook(selector, with: AspectOptions(), usingBlock: unsafeBitCast(block, to: AnyObject.self))
}

/// Replace identified class method with a block of code.
class func testSuite_replaceClassMethod(selector: Selector, code: ()->()) -> AspectToken? {
class func testSuite_replaceClassMethod(_ selector: Selector, code: @escaping ()->()) -> AspectToken? {
let block: @convention(block) (AspectInfo) -> Void = { _ in
code()
}
return try? self.aspect_hookSelector(selector, withOptions: .PositionInstead, usingBlock: unsafeBitCast(block, AnyObject.self))
return try? self.aspect_hook(selector, with: .positionInstead, usingBlock: unsafeBitCast(block, to: AnyObject.self))
}

}
46 changes: 23 additions & 23 deletions Spec/ReadmeExamples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class ReadmeExamples : QuickSpec {
client.connection.on { stateChange in
let stateChange = stateChange!
switch stateChange.current {
case .Connected:
case .connected:
print("connected!")
case .Failed:
print("failed! \(stateChange.reason)")
case .failed:
print("failed! \(String(describing: stateChange.reason))")
default:
break
}
Expand All @@ -60,13 +60,13 @@ class ReadmeExamples : QuickSpec {
let channel = client.channels.get("test")

channel.subscribe { message in
print(message.name)
print(message.data)
print(message.name as Any)
print(message.data as Any)
}

channel.subscribe("myEvent") { message in
print(message.name)
print(message.data)
print(message.name as Any)
print(message.data as Any)
}

channel.publish("greeting", data: "Hello World!")
Expand All @@ -82,8 +82,8 @@ class ReadmeExamples : QuickSpec {
channel.history { messagesPage, error in
let messagesPage = messagesPage!
print(messagesPage.items)
print(messagesPage.items.first)
print((messagesPage.items.first as? ARTMessage)?.data) // payload for the message
print(messagesPage.items.first as Any)
print(messagesPage.items.first?.data as Any) // payload for the message
print(messagesPage.items.count) // number of messages in the current page of history
messagesPage.next { nextPage, error in
// retrieved the next page in nextPage
Expand All @@ -99,7 +99,7 @@ class ReadmeExamples : QuickSpec {
defer { client.close() }

client.connection.on { stateChange in
if stateChange!.current == .Connected {
if stateChange!.current == .connected {
let channel = client.channels.get("test")

channel.presence.enter("john.doe") { errorInfo in
Expand All @@ -120,10 +120,10 @@ class ReadmeExamples : QuickSpec {

channel.presence.history { presencePage, error in
let presencePage = presencePage!
if let first = presencePage.items.first as? ARTPresenceMessage {
print(first.action) // Any of .Enter, .Update or .Leave
print(first.clientId) // client ID of member
print(first.data) // optional data payload of member
if let first = presencePage.items.first {
print(first.action) // Any of .enter, .update or .leave
print(first.clientId as Any) // client ID of member
print(first.data as Any) // optional data payload of member
presencePage.next { nextPage, error in
// retrieved the next page in nextPage
}
Expand Down Expand Up @@ -152,8 +152,8 @@ class ReadmeExamples : QuickSpec {

channel.history { messagesPage, error in
let messagesPage = messagesPage!
print(messagesPage.items.first)
print((messagesPage.items.first as? ARTMessage)?.data) // payload for the message
print(messagesPage.items.first as Any)
print(messagesPage.items.first?.data as Any) // payload for the message
messagesPage.next { nextPage, error in
// retrieved the next page in nextPage
}
Expand All @@ -168,8 +168,8 @@ class ReadmeExamples : QuickSpec {

channel.presence.get { membersPage, error in
let membersPage = membersPage!
print(membersPage.items.first)
print((membersPage.items.first as? ARTPresenceMessage)?.data) // payload for the message
print(membersPage.items.first as Any)
print((membersPage.items.first)?.data as Any) // payload for the message
membersPage.next { nextPage, error in
// retrieved the next page in nextPage
}
Expand All @@ -184,8 +184,8 @@ class ReadmeExamples : QuickSpec {

channel.presence.history { presencePage, error in
let presencePage = presencePage!
if let first = presencePage.items.first as? ARTPresenceMessage {
print(first.clientId) // client ID of member
if let first = presencePage.items.first {
print(first.clientId as Any) // client ID of member
presencePage.next { nextPage, error in
// retrieved the next page in nextPage
}
Expand All @@ -196,7 +196,7 @@ class ReadmeExamples : QuickSpec {
it("testGenerateToken") {
let client = ARTRest(options: AblyTests.commonAppSetup())

client.auth.requestToken(nil, withOptions: nil) { tokenDetails, error in
client.auth.requestToken(nil, with: nil) { tokenDetails, error in
let tokenDetails = tokenDetails!
print(tokenDetails.token) // "xVLyHw.CLchevH3hF....MDh9ZC_Q"
let client = ARTRest(token: tokenDetails.token)
Expand All @@ -209,7 +209,7 @@ class ReadmeExamples : QuickSpec {
client.channels.get("test").publish("foo", data: "bar") { _ in
client.stats { statsPage, error in
let statsPage = statsPage!
print(statsPage.items.first)
print(statsPage.items.first as Any)
statsPage.next { nextPage, error in
// retrieved the next page in nextPage
}
Expand All @@ -221,7 +221,7 @@ class ReadmeExamples : QuickSpec {
let client = ARTRest(options: AblyTests.commonAppSetup())

client.time { time, error in
print(time) // 2016-02-09 03:59:24 +0000
print(time as Any) // 2016-02-09 03:59:24 +0000
}
}

Expand Down
Loading