“THIS已重命名为THAT”已添加到我的iOS框架中。

huangapple go评论82阅读模式
英文:

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()
}

它将会给你与此处显示的完全相同的行为:

“THIS已重命名为THAT”已添加到我的iOS框架中。


注意:
你也可以对函数做同样的操作,唯一的要求是参数数量必须相同。

struct Foo {
    
    @available(*, unavailable, renamed: "sharedFun(fName:lName:)")
    static func defaultFun(first: String, last: String) {}
    
    static func sharedFun(fName: String, lName: String) {}
}

“THIS已重命名为THAT”已添加到我的iOS框架中。

英文:

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:

“THIS已重命名为THAT”已添加到我的iOS框架中。


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) {}
}

“THIS已重命名为THAT”已添加到我的iOS框架中。

huangapple
  • 本文由 发表于 2020年1月6日 18:40:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定