Swift interacts with OBJECTIVE-C

Source: Internet
Author: User

Both Swift and OC can be used in the same project, but not in the same file at the same time.

How OC invokes Swift-related information

***.mYou can import it in a file 工程名-Swift.h .
If the project is named ABC , it can be in a file that requires the use of swift-related information .m #import "ABC-Swift.h" .

How Swift calls OC-related information

When you create a oc/swift file in a Swift/oc project, there are hints, such as
Click Yes to create a 工程名-Bridging-Header.h file that will be .h used in Swift to import () the relevant OC files that are used by Swift #import "****.h" .

Reference: https://itunes.apple.com/us/book/using-swift-cocoa-objective/id888894773?mt=11

Watch out for problems.
    • 1, Swift inherits Objective-c and overloads the parent class method with a compilation exception
    • 2. Methods in Swift NSClassFromString have no effect on Swift's class
    • 3, Swift in performSelector: the beginning of the method IMP , NSInvocation has been removed, with what substitution?
    • 4. AnyObject How to convert to type in Swift 闭包 ?

There may be unresolved issues, if you have a way to resolve, please leave a message:) Thank you so much!

1, Swift inherits Objective-c and overloads the parent class method with a compilation exception

Parent Class Code:

@interface Superclass : NSObject- (Nsarray *)Arraywithstring:(NSString *)StringArray:(Nsarray *)Array dictionary:(Nsdictionary *)Dictionary number:(NSNumber *)Number;@end@implementation Superclass- (nsarray *)  arraywithstring :(nsstring *) string< Span class= "PLN" > Array:(nsarray *) array dictionary:(nsdictionary*) dictionary number:(nsnumber*) number{ return @[]; } @end         

To *-Bridging-Header.h import a header file in a file .h :#import "SuperClass.h"

Sub-class Code:

Class Subclass: Superclass { OverrideFunc arraywithstring(String: nsstring, Array: nsarray, Dictionary: nsdictionary, Number: nsnumber) -> nsarray { return [ "2" ] }}    /span>                

There are compiler exceptions when there are parameters or a return Objective-c method that is overloaded by Swift:

Overriding method with selector ‘***‘ has incompatible type ‘****‘

Workaround:


You can see in quick help the method's parameters and the type of return value on the face class code to read as follows:

Class Subclass: Superclass { OverrideFunc arraywithstring(String: string, Array: anyobject[], Dictionary:< Span class= "PLN" > nsdictionary, Number:< Span class= "PLN" > nsnumber)  -> anyobject[] {  return [ "2" ] }}    /span>                
2. In Swift NSClassFromStringMethod has no effect on Swift's class

code example above

Using in the Swift class

VarSuperc: Anyclass! = Nsclassfromstring ( "superclass" println  (nsstringfromclass (superc  //output superclassvar< Span class= "PLN" > Subc: anyclass! = nsclassfromstring ( "subclass" ) println ( Nsstringfromclass (subc//output nil               

Temporary workaround:

var subC: AnyClass! = SubClass.selfprintln(NSStringFromClass(subC)) // 输出SubClass

Workaround:
Added on subclass @objc(SubClass) , the SubClass class becomes:

@objc(SubClass)class SubClass: SuperClass {...}
3. In Swift, performSelector:The beginning of the method, IMPNSInvocationhave been removed, what substitution?

The Performselector:method and related selector-invoking methods is not imported in Swift because they is inherentl Y unsafe. From

The specified method of executing the specified object in Objective-c:

    • Use performSelector: the method with the beginning;
    • Use IMP ;
    • Use NSMethodSignature and NSInvocation , see method of passing multiple parameters when using Nsinvocation at runtime when calling method in Object-c

But in Swift performSelector: , the beginning of the method, IMP NSInvocation has been removed ...

Swift's NSInvocationOperation class, NSObject func forwardInvocation(anInvocation: NSInvocation!) all have the right NSInvocation reference, for the Mao point to go into God horse not ... Maybe it's because of Xcode6 Beta2 or the beta version ... So is beta3 ...

So what's the substitute for it?

I'm still working with objective-c to solve this problem.
Xcode6 beta3 Swift IMP , but not in the same way as in Objective-c IMP . I don't know how to use it yet.

4. In Swift AnyObjectHow to convert to 闭包Type?

Swift includes a protocol type named Anyobject that represents any kind of object, just as ID does in objective-c. The Anyobject protocol allows you-to-write Type-safe Swift code while maintaining the flexibility of a untyped object. Because of the additional safety provided by the Anyobject protocol, Swift imports ID as anyobject.
Swift closures and objective-c blocks is compatible, so can pass Swift closures to objective-c methods that expect BL Ocks. Swift closures and functions has the same type, so can even pass the name of a Swift function.
From
As mentioned above, the SwiftAnyObjectEquivalent to the objective-c in theid, in Swift the闭包Equivalent to the objective-c in theBlock。 But there are problems in practical applications, such as Objective-cidType can be cast toBlockAs follows:

typedef void(^BasicBlock)(void);id b1 = ^{};BasicBlock b1 = b1;

But in Swift, if the cast has an error, the following:

TypealiasBasicblock = () - voidfunc a ()  -> basicblock {  func b () { Println ( " 123213 ");  } return b }var C :anyobject = A ();  //error            

The problem is that it Swift returns a () through issue Block 3 闭包 , but it returns the type, the type that is received in, the id Swift AnyObject conversion to the specified 闭包 type, and then execution.

Swift interacts with OBJECTIVE-C

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.