0

I'm a beginner in both ObjectiveC and Swift (but have to develop an iOS share extension for a Cordova app).

I'm trying to implement this code snippet in my share extension

NSURL *destinationURL = [NSURL URLWithString:@"myapp://"];

// Get "UIApplication" class name through ASCII Character codes.
NSString *className = [[NSString alloc] initWithData:[NSData dataWithBytes:(unsigned char []){0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E} length:13] encoding:NSASCIIStringEncoding];
if (NSClassFromString(className)) {
    id object = [NSClassFromString(className) performSelector:@selector(sharedApplication)];
    [object performSelector:@selector(openURL:) withObject:destinationURL];
}

For now I have the following, but I don't really know how to translate the "performSelector" part as it seems it's not in Swift.

    let bytesArray : [UInt8] = [0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E]

    let classNameNs = NSString.init(data: NSData(bytes: bytesArray, length: bytesArray.count), encoding: NSASCIIStringEncoding) ?? ""

    let className = classNameNs as String

    NSClassFromString(className).map { clazz in
        let result = clazz.performSelector(Selector("sharedApplication"))
    }

Can someone help me complete this part please? thanks

Community
  • 1
  • 1
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419

2 Answers2

1

The simplest solution is update to Xcode 7 and Swift 2.0, where performSelector: does exist.

However, I have found that in nearly all situations where my Objective-C code used performSelector:, I don't need it in Swift, because in Swift a function is a first-class citizen and can be stored as a value and later retrieved and called. In general, the dynamism implied by performSelector: should not used — as all too frequently it is, in Objective-C — as a crutch to avoid having to know the actual class of a method's receiver.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • My XCode seems to be 7.0 but I don't even know the Swift version I am using. What I can say is that my IDE autocompletes for "performSelector" but I don't really know how to use it. Remember I'm a total noob, developing for iOS since 1 day only :) I'm really looking for the fastest solution as I'm prototyping. – Sebastien Lorber Oct 15 '15 at 16:34
  • I don't really care what you are or how long you've been developing for iOS. If what you mean is "please write my code for me, right now, I'm in a hurry", you'll have to hope that someone other than me does that for you. In my view, that would not be a proper use of Stack Overflow. – matt Oct 15 '15 at 16:36
  • Arguably most beginners questions are not proper use of StackOverflow, a most of them could be answered by investing a lot of time reading language spec and gaining experience. I thought it was clear that my question was of this kind. But I understand your point and you'll notice I have provided what I have successfully transposed but I'm just missing the last bit :'( – Sebastien Lorber Oct 15 '15 at 16:43
  • "Arguably most beginners questions are not proper use of StackOverflow" That is not what I said. I answered the question _as_ a beginner question. Your beginner question was: "I don't really know how to translate the `performSelector` part as it seems it's not in Swift." I explained that it _is_ in Swift and also that you can probably do without it. That's a _lot_ of useful information for you to be going on with, and a lot more than you knew before. – matt Oct 15 '15 at 17:01
1

You have to just provide a simple string into that method like this:

NSClassFromString(className).map { clazz in
    let result = clazz.performSelector("sharedApplication")
}
Soberman
  • 2,556
  • 1
  • 19
  • 22
  • I don't know why but it says that I can't call performSelector on AnyObject (but actually clazz is a AnyClass so I don't understand – Sebastien Lorber Oct 16 '15 at 09:05