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

Do not convert APIs not available in Swift #160

Open
Ibrahimhass opened this issue Mar 2, 2021 · 11 comments
Open

Do not convert APIs not available in Swift #160

Ibrahimhass opened this issue Mar 2, 2021 · 11 comments

Comments

@Ibrahimhass
Copy link
Member

Here is the Objective C code.

NSInvocationOperation * operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(invokeOpenDB) object:nil];

Converted Swift Code:

let operation = NSInvocationOperation(target: self, selector: #selector(invokeOpenDB), object: nil)

But NSInvocation is not available in Swift.

@alex-swiftify
Copy link
Member

@Ibrahimhass I know that NSInvocation isn't available in Swift, but can you suggest any straightforward change to the converter to cover this?
We have a backlog task to translate NSInvocation to DispatchQueue.main.async here:
https://swiftify.atlassian.net/browse/SWC-805
... but this looks way too difficult, and may not be worth the efforts at all.

If you find any drop-in Swift replacement, i.e. like the SwiftTryCatch library, we could use that.

@Ibrahimhass
Copy link
Member Author

@alex-swiftify This is one GitHub Project which can be used.
https://github.com/piotrtobolski/SwiftyInvocation

Please let me know if this can be used, if so then I will update it to Swift 5 and we can use it in the same way as SwiftTryCatch

@alex-swiftify
Copy link
Member

@Ibrahimhass Feel free to try updating the project to the latest Swift version.
If most changes would be changing NSInvocation to SIInvocation, or other involved classes with NS prefix to SI, that's fine.

After a very quick look, the library could help to port the code from the summary of https://swiftify.atlassian.net/browse/SWC-805,
but it doesn't contain a drop-in replacement for NSInvocationOperation used in this ticket.
See if you can implement a corresponding SIInvocationOperation class easily.

@alex-swiftify
Copy link
Member

@Ibrahimhass If this is doable in under 2 hours you may update and publish that project.
Otherwise, we'll postpone this.

@Ibrahimhass
Copy link
Member Author

@alex-swiftify I have updated the project to Swift 5 and can successfully get the output for the test case mentioned here

let array: NSArray = ["1", "2", "3"]

let selector = #selector(NSArray.object(at:))
let methodSignature = NSArray.si_instanceMethodSignature(for: selector)
let invocation = SIInvocation(methodSignature: methodSignature)
invocation.target = array
invocation.selector = selector
var argument = 1
invocation.setArgument(&argument, at: 2)
invocation.retainArguments()

invocation.invoke()

var returnValue: Unmanaged<NSString>?
invocation.getReturnValue(&returnValue)

XCTAssertEqual(returnValue?.takeUnretainedValue(), "2")

But for cases where the function needs a parameter or returns a value type, the program crashes as we have to use the
swift_getImplementation function.

Somewhat related StackOverflow answer.

This might take longer than 2 hours so lets postpone this for now.

@alex-swiftify
Copy link
Member

@Ibrahimhass I got you. Let's either push the update to Swift 5.x as the PR to the original repo, or fork SwiftyInvocation in the Swiftify repo.

@Ibrahimhass
Copy link
Member Author

@alex-swiftify I have a forked version on my GitHub profile where the issues were already fixed earlier.
https://github.com/Ibrahimhass/SwiftyInvocation

I have also forked a version of the original repo under Swiftify
https://github.com/Swiftify-Corp/SwiftyInvocation.

@alex-swiftify
Copy link
Member

@Ibrahimhass Isn't it more logical to have the version updated to Swift 5 under Swiftify?

@Ibrahimhass
Copy link
Member Author

@alex-swiftify Yes, without a doubt, I had earlier forked it to my profile here, the Swift 5 code had some issues I was thinking of fixing this issue first and then update the repository here

@alex-swiftify
Copy link
Member

@Ibrahimhass Well, if we agreed that we postpone this, maybe you just fork from your repository to Swiftify for now.
Don't spend a lot of time on this.

@Ibrahimhass
Copy link
Member Author

@alex-swiftify I have forked my repo to Swiftify now.
https://github.com/Swiftify-Corp/SwiftyInvocation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants