首页 > 解决方案 > How to set first argument label in objective c to use in swift?

问题描述

I thought to use first argument label of objective function in swift, it can be done by adding preposition to function name.

- (void) makeRoundedWithCornerRadius: (CGFloat) cornerRadius;

I can use it in swift project like this (It is what I expected)

myView.makeRounded(withCornerRadius: 16)

And I made another function like this

- (void) makeBorderWithColor: (UIColor*) color width: (CGFloat) width;

However when I'm about to use it in swift, first argument label does not show the way I expected. What I expected was

myView.makeBorder(withColor: .red, width: 10)

But it showed like..

myView.makeBorder(with: .red, width: 10)

It eliminates color label. I am very confused and even can't guess the rule.

What is the rule that convert Objective-C function name to Swift's first argument label? And is it other way to make first argument label that I want?

标签: objective-cswiftparametersargumentslabel

解决方案


You can use method renaming – NS_SWIFT_NAME keyword:

- (void) makeRoundedWithCornerRadius: (CGFloat) cornerRadius width: (CGFloat) width NS_SWIFT_NAME(makeBorder(withColor:width:));

https://developer.apple.com/documentation/swift/objective-c_and_c_code_customization/renaming_objective-c_apis_for_swift


推荐阅读