Swift and OBJECTIVE-C hybrid programming

Source: Internet
Author: User



1. First open Xcode6, build a project, here I use the OBJECTIVE-C default programming language, the project name is "swiftandobjective".



2. In order to use Swift's demo in OC, the following classes need to be established.



A) the creation of a Objective-c class inherits from NSObject, named Octypeclass, so it automatically generates two corresponding. m and. h files, which are very familiar to people familiar with Objective-c.



b) Try a swift class, called SwiftFile1, when you click Create, a prompt will pop up asking if you want to create a hander. h file, which must be selected here. Because if you want to use the Objective-c class in Swift, this is a must-have file, use it later. The result creates a Swiftfile1.swift file and a header file.



c) In order to describe the access control of public, private, default and other relationships, you need to create a swift file, named SwiftFile2, will generate a Swiftfile2.swift file.



Okay, the file Creation section is complete.






3. The class needs to be defined (because Swift can not inherit any class, but if it needs to be used by objective-c, we use the alloc,new , etc to create the method that is inherited from NSObject, So these classes in swift, I have inherited the NSObject.



Create the following class in the Swiftfile1.swift file





import Foundation

@objc public class PublicObjcTypeSwiftClass1: NSObject {
    var property1: Int = 0
}


@objc class ObjcTypeSwiftClass1: NSObject {
    var property1: Int = 0
}

@objc private class ObjcPrivateTypeSwiftClass1: NSObject {
    var property1: Int = 0
}

public class PublicSwiftClass1 : NSObject {
    var property1: Int = 0
}

class SwiftClass1: NSObject {
    var property1: Int = 0
}

private class PrivateSwiftClass1: NSObject {
    var property1: Int = 0
}






Create the following class in Swiftfile2.swift





<pre name="code" class="objc">import Foundation

@objc public class PublicObjcTypeSwiftClass2: NSObject {
    var property1: Int = 0
    //var ocClass: OCTypeClass = OCTypeClass()
    
    //func test() {
    //    ocClass.property1 = 0
    //}
}


@objc class ObjcTypeSwiftClass2: NSObject {
    var property1: Int = 0
}

@objc private class ObjcPrivateTypeSwiftClass2: NSObject {
    var property1: Int = 0
}

public class PublicSwiftClass2: NSObject {
    var property1: Int = 0
}

class SwiftClass2: NSObject {
    var property1: Int = 0
}

private class PrivateSwiftClass2: NSObject {
    var property1: Int = 0
}







4. The corresponding class is automatically generated because the OC file is defined, so there is no need to define it again.





#import "OCTypeClass.h"
#import "SwiftAndObjective-Swift.h"

@implementation OCTypeClass

- (void)test
{
    PublicObjcTypeSwiftClass1 *type = [[PublicObjcTypeSwiftClass1 alloc] init];
    type.property1 = 0;
    
    
    ObjcTypeSwiftClass1 *type2 = [[ObjcTypeSwiftClass1 alloc] init];
    type2.property1 = 0;
}

@end






5. Open the OCTYPECLASS.M file we need to refer to the classes in the swift file. You can use the "Project project name + '-' + Swfit.h" form to introduce a class that is public and is marked as @objc (the class labeled @objc, even if the write public tag is not displayed, it will be used by the Objective-c class). The following specific code (note when writing #import "" use, the current version of Xcode will not automatically recognize the need for swift header name, need to write manually, note: Do not write the wrong project name, if the confirmation is written, but has been error can go to refresh the project, or restart, Re-build:project-"clean, project-" Build, big deal restart Xcode).






6. When you are finished, you can view the Objective-c class that Xcode automatically generates by looking at the form of the source code (command + left mouse button).





// Generated by Swift version 1.1 (swift-600.0.54.20)
#pragma clang diagnostic push

#if defined(__has_include) && __has_include(<swift/objc-prologue.h>)
# include <swift/objc-prologue.h>
#endif

#pragma clang diagnostic ignored "-Wauto-import"
#include <objc/NSObject.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#if defined(__has_include) && __has_include(<uchar.h>)
# include <uchar.h>
#elif !defined(__cplusplus) || __cplusplus < 201103L
typedef uint_least16_t char16_t;
typedef uint_least32_t char32_t;
#endif

typedef struct _NSZone NSZone;

#if !defined(SWIFT_PASTE)
# define SWIFT_PASTE_HELPER(x, y) x##y
# define SWIFT_PASTE(x, y) SWIFT_PASTE_HELPER(x, y)
#endif
#if !defined(SWIFT_METATYPE)
# define SWIFT_METATYPE(X) Class
#endif

#if defined(__has_attribute) && __has_attribute(objc_runtime_name)
# define SWIFT_RUNTIME_NAME(X) __attribute__((objc_runtime_name(X)))
#else
# define SWIFT_RUNTIME_NAME(X)
#endif
#if !defined(SWIFT_CLASS_EXTRA)
# define SWIFT_CLASS_EXTRA
#endif
#if !defined(SWIFT_PROTOCOL_EXTRA)
# define SWIFT_PROTOCOL_EXTRA
#endif
#if !defined(SWIFT_CLASS)
# if defined(__has_attribute) && __has_attribute(objc_subclassing_restricted) 
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_CLASS_EXTRA
# else
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
# endif
#endif

#if !defined(SWIFT_PROTOCOL)
# define SWIFT_PROTOCOL(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA
#endif

#if !defined(SWIFT_EXTENSION)
# define SWIFT_EXTENSION(M) SWIFT_PASTE(M##_Swift_, __LINE__)
#endif

#if !defined(OBJC_DESIGNATED_INITIALIZER)
# if defined(__has_attribute) && __has_attribute(objc_designated_initializer)
#  define OBJC_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
# else
#  define OBJC_DESIGNATED_INITIALIZER
# endif
#endif
#if defined(__has_feature) && __has_feature(modules)
@import ObjectiveC;
#endif

#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch"
#pragma clang diagnostic ignored "-Wduplicate-method-arg"

SWIFT_CLASS("_TtC17SwiftAndObjective19ObjcTypeSwiftClass1")
@interface ObjcTypeSwiftClass1 : NSObject
@property (nonatomic) NSInteger property1;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end


SWIFT_CLASS("_TtC17SwiftAndObjective19ObjcTypeSwiftClass2")
@interface ObjcTypeSwiftClass2 : NSObject
@property (nonatomic) NSInteger property1;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end


SWIFT_CLASS("_TtC17SwiftAndObjective25PublicObjcTypeSwiftClass1")
@interface PublicObjcTypeSwiftClass1 : NSObject
@property (nonatomic) NSInteger property1;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end


SWIFT_CLASS("_TtC17SwiftAndObjective25PublicObjcTypeSwiftClass2")
@interface PublicObjcTypeSwiftClass2 : NSObject
@property (nonatomic) NSInteger property1;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end


SWIFT_CLASS("_TtC17SwiftAndObjective17PublicSwiftClass1")
@interface PublicSwiftClass1 : NSObject
@property (nonatomic) NSInteger property1;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end


SWIFT_CLASS("_TtC17SwiftAndObjective17PublicSwiftClass2")
@interface PublicSwiftClass2 : NSObject
@property (nonatomic) NSInteger property1;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end


SWIFT_CLASS("_TtC17SwiftAndObjective11SwiftClass1")
@interface SwiftClass1 : NSObject
@property (nonatomic) NSInteger property1;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end


SWIFT_CLASS("_TtC17SwiftAndObjective11SwiftClass2")
@interface SwiftClass2 : NSObject
@property (nonatomic) NSInteger property1;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

#pragma clang diagnostic pop






7. Start using the Objective-c class in the Swift file below.



A) open Xcode to automatically generate the Swiftandobjective-bridging-header.h file for us and import the classes exposed to Swift.



The class that you just defined is used here for convenience only to demonstrate:






#import "OCTypeClass.h"



b) Then we don't need to do anything, just implement it in the Swfit file.








import Foundation

@objc public class PublicObjcTypeSwiftClass2: NSObject {
    var property1: Int = 0
    var ocClass: OCTypeClass = OCTypeClass()
    
    func test() {
        ocClass.property1 = 0
    }
}


@objc class ObjcTypeSwiftClass2: NSObject {
    var property1: Int = 0
}

@objc private class ObjcPrivateTypeSwiftClass2: NSObject {
    var property1: Int = 0
}

public class PublicSwiftClass2: NSObject {
    var property1: Int = 0
}

class SwiftClass2: NSObject {
    var property1: Int = 0
}

private class PrivateSwiftClass2: NSObject {
    var property1: Int = 0
}






Attention:



1. If you want to expose a Swfit class to objective-c, you need to identify the @objc



2. For the private type of swift class, it can only be used in the defined Swift file.



3. Classes in a swift file can only access classes of the public type defined in the external swift file.






Almost overlooked the most important point: if there is any problem or error, please correct or communicate.






Sample code: Code



http://download.csdn.net/detail/liyan223/8070805





















Swift and OBJECTIVE-C hybrid programming


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.