Skip to content

removed project warnings #683

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

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions Sources/Web3Core/Transaction/Envelope/EIP1559Envelope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ extension EIP1559Envelope {

switch type {
case .transaction:
fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list, v, r, s] as [AnyObject]
fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list, v, r, s].toAnyObject()
case .signature:
fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list] as [AnyObject]
fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list].toAnyObject()
}
guard var result = RLP.encode(fields) else { return nil }
result.insert(UInt8(self.type.rawValue), at: 0)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Web3Core/Transaction/Envelope/EIP2930Envelope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ extension EIP2930Envelope {

switch type {
case .transaction:
fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list, v, r, s] as [AnyObject]
fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list, v, r, s].toAnyObject()
case .signature:
fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list] as [AnyObject]
fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list].toAnyObject()
}
guard var result = RLP.encode(fields) else { return nil }
result.insert(UInt8(self.type.rawValue), at: 0)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ extension LegacyEnvelope {
let fields: [AnyObject]
switch type {
case .transaction:
fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, v, r, s] as [AnyObject]
fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, v, r, s].toAnyObject()
case .signature:
if let chainID = chainID, chainID != 0 {
fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, chainID, BigUInt(0), BigUInt(0)] as [AnyObject]
if let chainID = self.chainID, chainID != 0 {
fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, chainID, BigUInt(0), BigUInt(0)].toAnyObject()
} else {
fields = [nonce, gasPrice, gasLimit, to.addressData, value, data] as [AnyObject]
fields = [nonce, gasPrice, gasLimit, to.addressData, value, data].toAnyObject()
}
}
return RLP.encode(fields)
Expand Down
9 changes: 9 additions & 0 deletions Sources/Web3Core/Utility/Array+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,12 @@ extension Array where Element: BinaryInteger {
return sorted_data[index]
}
}

// MARK: - Conversion

/// Transforms `[Any?]` into `[AnyObject]`
extension Array where Element == Any? {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This extension doesn't apply to [Any] but it should. As a solution where clause can be removed;
  2. I'd suggest renaming the function to toAnyObjectArray;
  3. Why self.map { ... } and not just as [AnyObject]?

This is the suggested update:

/// Transforms into `[AnyObject]`
extension Array {
    func toAnyObjectArray() -> [AnyObject] {
        self as [AnyObject]
    }
}

func toAnyObject() -> [AnyObject] {
self.map { $0 as AnyObject }
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could fail, since Any is more broad that AnyObject is, so the casting could fail.

Copy link
Collaborator

@JeneaVranceanu JeneaVranceanu Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see how this can cause problems. @yaroslavyaroslav Could you please give more explanation/examples?

It's said that all classes implicitly conform to AnyObject but we're able to cast structs as well so maybe that part about structs (or to be specific value types is confusing).


Did some research and found that casting value types to AnyObject produces a wrapper of type __SwiftValue. And here is the __SwiftValue. Look at the docs on line 13.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, why did they do that? Like now there's completely no difference between Any and AnyObject isn't it? If so, i agree that this would not fall, but i still suggest to make that extension more consistent, using either Any or AnyObject in whole method.

Copy link
Collaborator

@JeneaVranceanu JeneaVranceanu Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still some difference when it comes to casting back to the original type. I cannot explain that in detail but for example if we cast UInt64(1) to AnyObject and try to use it as if let bool = uint64AsAnyObject as? Bool it will successfully cast to Bool. But if you cast it to Any instead it will cast back only as UInt64.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but for example if we cast UInt64(1) to AnyObject and try to use it as if let bool = uint64AsAnyObject as? Bool it will successfully cast to Bool. But if you cast it to Any instead it will cast back only as UInt64.

Actually I'd consider this as a bug of those thing that is casting to AnyObject for Objective-C environment. But I don't sure though.

}
44 changes: 44 additions & 0 deletions Tests/web3swiftTests/localTests/ArrayExtensionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// ArrayExtensionTests.swift
// Created by albertopeam on 25/11/22.
//

@testable import Web3Core
import XCTest
import BigInt

final class ArrayExtensionTests: XCTestCase {
func testToAnyObjectEmpty() {
let result = [].toAnyObject()
XCTAssertEqual(result.count, 0)
}

func testToAnyObjectNils() throws {
let result = [nil, nil].toAnyObject()
XCTAssertEqual(result.count, 2)
XCTAssertTrue(result.first is NSNull)
XCTAssertTrue(result.dropFirst().first is NSNull)
}

func testToAnyObjectNilAndNonNils() throws {
let result = [
1,
nil,
"2",
NSNull(),
Data(hex: "FA"),
BigInt(3),
BigUInt(4),
EthereumAddress(Data(count: 20))
].toAnyObject()
XCTAssertEqual(result.count, 8)
XCTAssertEqual(result.first as? Int, 1)
XCTAssertTrue(result.dropFirst(1).first is NSNull)
XCTAssertEqual(result.dropFirst(2).first as? String, "2")
XCTAssertTrue(result.dropFirst(3).first is NSNull)
XCTAssertEqual(result.dropFirst(4).first as? Data, Data(hex: "FA"))
XCTAssertEqual(result.dropFirst(5).first as? BigInt, BigInt(3))
XCTAssertEqual(result.dropFirst(6).first as? BigUInt, BigUInt(4))
XCTAssertEqual(result.dropFirst(7).first as? EthereumAddress, EthereumAddress(Data(count: 20)))
}
}