我想在 swift 类和 objective-c 类之间创建继承。
swift类创建一个人,objective c类创建一个学生(也是人)
我的敏捷人是:
import Foundation
class person {
var age: Int
var height: Float
init(){
self.age = 22
self.heigh =1.80
}
init(age:Int, height:Float){
self.age=age
self.height=height
}
}
Objective-c 的学生是:
学生.h:
#import <Foundation/Foundation.h>
#import "Praktikum2a-Swift.h"
@interface student : person
@property (nonatomic) int studentID;
-(void)setStudentID:(int)stdID;
@end
学生.m:
#import <Foundation/Foundation.h>
#import "Praktikum2a-Swift.h"
#import "student.h"
@implementation student
-(id)init{
return [self initWithStudentID:1337];
}
-(id)initWithStudentID:(int)stdID{
self=[super init];
if(self){
[self setStudentID:stdID];
}
}
@end
和主要的:
#import <Foundation/Foundation.h>
#import "student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
student *hugo = [[student alloc] init];
[hugo setAge: 22 setHeight: 1.80];
NSLog(@"Hello, World!");
}
return 0;
}
项目和包名称是 Praktikum2a
基本上我想不通,为什么我的学生不是人... =/
最佳答案
Note that you cannot subclass a Swift class in Objective-C
关于objective-c - 从 Objective C 类调用 Swift 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26476819/