OC調用Swift

來源:互聯網
上載者:User

標籤:des   log   art   ret   int   oid   自己   return   pos   

改動main.m檔案

#import <Foundation/Foundation.h>#import "Root.h"int main(int argc, const char * argv[]){    @autoreleasepool    {        Root *rt = [[Root alloc] init];        [rt desc];    }    return 0;}

OC檔案:Root.h

#import <Foundation/Foundation.h>//Objective-c 的標頭檔假設須要引用Swift的類。則能夠使用以下這樣的方式@class Person;@interface Root : NSObject-(Person *)returnPerson;-(void)desc;@end

Root.m

#import "Root.h"//Objective-c調用Swift//須要匯入固定的標頭檔。此標頭檔項目裡面找不到,但卻是存在。

而且會自己主動把Swift類轉換成OC的類,在裡面能找到//格式為 #ProductName#-Swift.h#import <OC_Swift-Swift.h>@implementation Root-(void)desc{ Person *ps = [[Person alloc] initWithName:@"Rose"]; ps.name = @"Jack"; [ps desc];}-(Person *)returnPerson{ Person *ps = [[Person alloc] initWithName:@"Tom"]; return ps;}@end


Swift檔案:Person.swift

import Foundation//假設此類須要被OC的類來調用。一定要繼承自NSObjectclass Person : NSObject{    var name: String    {        willSet        {            NSLog("將要把名字設定為:" + name)        }    }    override init()    {        self.name = ""    }    init(name: String)    {        self.name = name    }    func desc()    {        print("這是一個Swift的類,name: " + self.name)    }}



OC調用Swift

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.