英文:
Add "THIS has been renamed to THAT" for my own framework in iOS
问题
假设我有一个框架,名为A
,我通过将A.framework
文件添加到项目中来动态地包含它。
考虑一下框架A
中的代码情景:
struct Foo {
static let `default` = Foo()
}
现在我将名称从default
更改为shared
:
struct Foo {
static let shared = Foo()
}
现在,当我们更新Swift版本时,如果某些语法发生变化,有时会出现以下错误:
'NSLayoutAttribute'已更名为'NSLayoutConstraint.Attribute'
在这种错误中,我们点击错误内的修复按钮,名称会自动更改。
我想在我的A.framework
中进行一些名称更改或函数声明更改时实现相同的效果,并将其应用到使用它的应用程序中。是否有已知的方法来实现这一点?
英文:
Let's say I have a framework, A
, that I include in a lot of projects dynamically by adding A.framework
file inside the projects.
Consider a scenario inside the code of framework A
:
struct Foo {
static let `default` = Foo()
}
Now I changed the name from default
to shared
:
struct Foo {
static let shared = Foo()
}
Now, as we update the Swift versions, if some syntax changes, we sometimes get an error like
> 'NSLayoutAttribute' has been renamed to 'NSLayoutConstraint.Attribute'
wherein we tap the fix button inside that error and the name changes automatically.
I want to achieve the same when I make some name change or function declaration change inside my A.framework
and roll it out to the apps using it. Is there a known way to achieve the same ?
答案1
得分: 7
你可以使用@available属性来实现相同的行为。
struct Foo {
@available(*, unavailable, renamed: "shared")
static let defaults = Foo()
static let shared = Foo()
}
它将会给你与此处显示的完全相同的行为:
注意:
你也可以对函数做同样的操作,唯一的要求是参数数量必须相同。
struct Foo {
@available(*, unavailable, renamed: "sharedFun(fName:lName:)")
static func defaultFun(first: String, last: String) {}
static func sharedFun(fName: String, lName: String) {}
}
英文:
You can achieve the same behaviour by using @available attribute.
struct Foo {
@available(*, unavailable, renamed: "shared")
static let defaults = Foo()
static let shared = Foo()
}
It will give you an exact behaviour as shown here:
Note:
You can do the same with functions as well, only thing is you have to have the same number of parameters.
struct Foo {
@available(*, unavailable, renamed: "sharedFun(fName:lName:)")
static func defaultFun(first: String, last: String) {}
static func sharedFun(fName: String, lName: String) {}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论