Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 尝试为应调整字符串的属性创建包装_Ios_Swift_String_Swift4_Swift5 - Fatal编程技术网

Ios 尝试为应调整字符串的属性创建包装

Ios 尝试为应调整字符串的属性创建包装,ios,swift,string,swift4,swift5,Ios,Swift,String,Swift4,Swift5,我有这个代码,我需要让其他应用程序重复使用 此代码显示存在本地化应用程序名称的消息 我有如下本地化字符串: "DoYou" = "Do you really want to close $$$?"; "Quit" = "Quit $$$"; "Keep Running" = "Keep $$$ Running"; 其中,$$必须在运行时替换为本地化应用的名称 因为我想了解有关属性包装的知识,所以我尝试创建了以下内容: extension String { func findReplace

我有这个代码,我需要让其他应用程序重复使用

此代码显示存在本地化应用程序名称的消息

我有如下本地化字符串:

"DoYou" = "Do you really want to close $$$?";
"Quit" = "Quit $$$";
"Keep Running" = "Keep $$$ Running";
其中,
$$
必须在运行时替换为本地化应用的名称

因为我想了解有关属性包装的知识,所以我尝试创建了以下内容:

extension String {

  func findReplace(_ target: String, withString: String) -> String
  {
    return self.replacingOccurrences(of: target,
                                     with: withString,
                                     options: NSString.CompareOptions.literal,
                                     range: nil)
  }
}


  @propertyWrapper
  struct AdjustTextWithAppName<String> {
    private var value: String?


    init(wrappedValue: String?) {
      self.value = wrappedValue
    }

    var wrappedValue: String? {
      get { value }
      set {
        if let localizedAppName = Bundle.main.localizedInfoDictionary?["CFBundleName"] as? String {
          let replaced = value.findReplace("$$$", withString: localizedAppName)

        }
        value = nil
      }
    }

  }
同样的错误



字符串可能多次包含应用程序的名称。这就是为什么我将扩展名设置为
String

要解决这个问题,您的属性包装器不能具有名为String的泛型类型,因为它会隐藏扩展名所属的内置类型字符串。因此,将
struct
声明更改为非泛型

@propertyWrapper
struct AdjustTextWithAppName {
或者将类型命名为其他名称

@propertyWrapper
struct AdjustTextWithAppName<T> {

要解决此问题,属性包装器不能具有名为String的genereic类型,因为它会隐藏扩展所属的内置类型字符串。因此,将
struct
声明更改为非泛型

@propertyWrapper
struct AdjustTextWithAppName {
或者将类型命名为其他名称

@propertyWrapper
struct AdjustTextWithAppName<T> {

您的函数与对它的调用不匹配,您需要将
findReplace(target:String,withString:String)
替换为
findReplace(uuu-target:String,withString:String)
@Maximilian-很抱歉,这是一个打字错误。只是这样做了,错误仍然存在。内置的本地化系统已经支持这样的占位符。您的函数与对它的调用不匹配,您需要将
findReplace(target:String,withString:String)
替换为
findReplace(uuuu-target:String,withString:String)
@Maximilian-很抱歉,这是一个打字错误。刚刚执行了此操作,错误仍然存在。内置本地化系统已经支持这样的占位符。类型为“String”的值在上一次使用时没有成员“findReplace”line@SpaceDog对我来说很好,你改变声明了吗?你是说,改变结构行和集合块?对你说我的包装不能是通用的是什么意思?啊,我明白了,防护罩里面需要退货。现在它编译得很好。谢谢你能解释一下为什么我的不起作用吗?Thanks@Maximilian,谢谢,我刚刚用这个更新了我的答案。我之前的解释不正确。“String”类型的值在上一次使用时没有成员“findReplace”line@SpaceDog对我来说很好,你改变声明了吗?你是说,改变结构行和集合块?对你说我的包装不能是通用的是什么意思?啊,我明白了,防护罩里面需要退货。现在它编译得很好。谢谢你能解释一下为什么我的不起作用吗?Thanks@Maximilian,谢谢,我刚刚用这个更新了我的答案。我先前的解释不正确。