objective-c - 从 Objective C 类调用 Swift 方法

标签 objective-c inheritance swift

我想在 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

基本上我想不通,为什么我的学生不是人... =/

最佳答案

关于objective-c - 从 Objective C 类调用 Swift 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26476819/

相关文章:

objective-c - 在 Objective C 中向 NSMutableArray 添加对象时遇到问题

javascript - 如何继承和链接String而不污染String.prototype?

php - Doctrine 2 与关联的继承映射

xcode - 文件所有者 'view' socket 未显示

swift - 在其他数组中使用 ForEach 的索引

iphone - 如何使用 Storyboard?

ios - 您如何使用键值验证?

iOS - 如何预加载键盘?

c# - 无法转换从通用基础继承的对象

ios - 从 Parse 中检索数组并将它们保存在具有相应值的字典中。 [字符串() : [String]()]