大刘的 iOS 自学笔记

OC方法重载

2022-06-12  本文已影响0人  大刘

Created by 大刘 liuxing8807@126.com

Java/c++/c#/swift全部支持方法重载, 但OC不支持重载,可以利用编译器使用C方法重载

//
//  ViewController.m
//  C_overload
//
//  Created by liuweizhen on 2018/12/14.
//  Copyright © 2018 daliu. All rights reserved.
//

#import "ViewController.h"

__attribute__((overloadable)) double sum(double a, double b) {
    return a + b;
}

__attribute__((overloadable)) int sum(int a, int b) {
    return a + b;
}

__attribute__((overloadable)) int sum(UIView *a, UIView *b) {
    return 100;
}

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%d", sum(self.view, self.view));
}

@end
上一篇下一篇

猜你喜欢

热点阅读