且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

尝试为应该调整字符串的属性制作包装器

更新时间:2023-11-15 14:38:10

为了解决这个问题,你的属性包装器不能有一个名为 String 的泛型类型,因为它掩盖了你的扩展所属的内置类型 String.所以将 struct 声明改为非泛型

To solve this your property wrapper can't have a genereic type named String because that shadows the built-in type String that your extension belongs to. So change the struct declaration to not be generic

@propertyWrapper
struct AdjustTextWithAppName {

或将类型命名为其他名称

or name the type to something else

@propertyWrapper
struct AdjustTextWithAppName<T> {

并修复set方法

set {
    guard let str = newValue, let localizedAppName = Bundle.main.localizedInfoDictionary?["CFBundleName"] as? String else {
        value = nil
     } 

     value = str.findReplace(target: "$$$", withString: localizedAppName)
  }