Swift-based OC files invoke Swift code (written on the previous basis)

Source: Internet
Author: User

The first two days have just written the swift call OC, today on the original basis, implemented OC invoke Swift.

First, create a oneswiftfile.swift file and create a class that inherits from the NSObject (this place you can choose to inherit the parent class yourself)

You then create several methods in the class to make it easy to use in the OC file

Class oneswiftfile:nsobject{
    override init () {
         NSLog ("This is in the Init method ... ");
   }
    
   //Create a method
    func somefunc () {
         NSLog ("This is in the SomeFunc () method ...");
   }
    
   //Create a method with parameters and return worth
    func somearraybyvalue (v1: string,v2:string), Nsarray {
        return [v1,v2];
   
   //Create a tagged method
    func somearrayvalue (v1:string, Withvalue v2:string,withvalue v3:string), Nsarray {
        return [V1,v2, V3];
   }

Here is the difference between a label and a label://Create a method with a label, if it has a label, it will be displayed as a label, if there is no label, the parameter name is labeled, you can compare the following method:
/*
-(Nsarray * _nonnull) Somearraybyvalue: (NSString * _nonnull) v1 v2: (NSString * _nonnull) v2;
-(Nsarray * _nonnull) Somearrayvalue: (NSString * _nonnull) v1 withvalue: (NSString * _nonnull) v2 Withvalue: (NSString * _Non NULL) v3;
*/

}

Here to emphasize, when you finish writing the method, compile, will be in the #import < project name-swift.h> header file (this header file will be described below) the bottom of the generated corresponding method, so you can prompt, convenient method call

When the Swift file is finished, we begin to consider how to use the methods in the class, first import the header file in the created OC file (BGIMGVIEW.H.M), the format of the header file is:#import < project name-swift.h>, such as: #import <SwiftAndOCChangeDemo-Swift.h>

Explain that this header file is not found in the file list, but you can click to see, the bottom of the file is the system automatically all the Swift language files are compiled in a OC language, including the Swift file Method generation, which further illustrates that Swift wants to replace OC, But you can't completely remove OC, because this is the base, and the bottom is the C language.



The object is then created in the OC file, calling the method

Call the methods in the Oneswiftfile.swift file
Oneswiftfile *ones = [[Oneswiftfile alloc]init];
SomeFunc method
[OneS SomeFunc];

Methods with parameters and return values
Nsarray *arr = [OneS somearraybyvalue:@ "good study" v2:@ "Day Up"];
For (NSString *str in arr)
{
NSLog (@ "Output every string ...%@", str);
}
Methods with labels
arr = [OneS somearrayvalue:@ "haha" withvalue:@ "hehe" withvalue:@ "hehe"];
For (NSString *str in arr)
{
NSLog (@ "Output every string ...%@", str);
}

Results show:


SOURCE Download: http://download.csdn.net/detail/hbblzjy/9610203


Swift-based OC files invoke Swift code (written on the previous basis)

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.