標籤: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