Swift and OBJECTIVE-C hybrid programming

Source: Internet
Author: User



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



2. In order to use Swift's demo in OC, you need to create several classes below.



A) Create a Objective-c class that inherits from the NSObject. The name is Octypeclass, so I will take the initiative to produce two corresponding. m and. h files, which are 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.



Since it is assumed that you want to use the Objective-c class in Swift, this is a must-have file, detailed use later.



The result creates a Swiftfile1.swift file and a header file.



c) to describe the relationship between public, private, default, etc. The need to create a swift file, called SwiftFile2, generates a Swiftfile2.swift file.



Okay, the file Creation section is complete.






3. The class needs to be defined (since Swift can not inherit whatever class.) But assuming that we need to be used by objective-c, we use alloc,new , etc. to create methods that are 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. Because the OC file is defined, the corresponding class is generated on its own initiative. So there's 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. Classes that are public and marked as @objc (classes marked as @objc, even those that do not display a public tag are used by the Objective-c Class) can be introduced in the form project name + '-' + Swfit.h '. The following detailed code (note that when writing #import "" is used, the current version of Xcode does not proactively identify the required Swift header name.) Need to write manually, note: Do not write the wrong project name, assuming that the confirmation is written to, but has been error can go to refresh the project, or restart, again build:project-"clean, project-" Build, big deal again to start Xcode).









6. After completion, you can view the form of the source code (command + left mouse button) in the form of Xcode self-generated objective-c class.








// 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. The following classes start using Objective-c in Swift files.






A) Open Xcode's self-generated 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 have 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. In a swift file class, you can only access classes of public types defined in the external swift file.






Almost overlooked the most important point: if there are any problems or errors, please correct or communicate.






Demo 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.