从Objective C类调用Swift方法

时间:2014-10-20 23:57:02

标签: objective-c inheritance swift

我想在swift类和objective-c类之间创建一个继承。

swift类创建一个人,目标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学生是:

student.h:

#import <Foundation/Foundation.h>
#import "Praktikum2a-Swift.h"

@interface student : person

@property (nonatomic) int studentID;

-(void)setStudentID:(int)stdID;

@end

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

基本上我无法弄明白,为什么我的学生不是一个人... = /

1 个答案:

答案 0 :(得分:0)