0

所以,我尝试创建一个属性包装器,从不需要的字符中去除电话号码并向其添加国家/地区代码:

@propertyWrapper
struct MSISDN {

private var _wrappedValue: String

public var wrappedValue: String {
    get {
        return fullMsisdn
    }
    set {
        _wrappedValue = newValue
    }
}

private var cleaned: String {
    return cleanStr(str: _wrappedValue)
}

private var fullMsisdn: String {
    return withCountryCode(cleaned)
}

private func cleanStr(str: String) -> String {
    return str.replacingOccurrences(of: "[ \\-()]", with: "", options: [.regularExpression])
}

private func withCountryCode(_ msisdn: String) -> String {
    guard msisdn.count == 10 && msisdn.starts(with: "69") else { return msisdn }
    
    return "+30\(msisdn)"
}

init(wrappedValue: String) {
    self._wrappedValue = wrappedValue
}

现在,当我尝试创建这样的 var 时@MSISDN var msisdn: String = "69 (4615)-11-21" ,出现以下错误

error: msisdn.playground:71:17: error: closure captures '_msisdn' before it is declared
    @MSISDN var ms: String = "69 (4615)-11-21"
                ^

msisdn.playground:71:17: note: captured value declared here
    @MSISDN var msisdn: String = "69 (4615)-11-21"
            ^

如果我尝试像下面这样分两个步骤来做,一切正常。

@MSISDN var msisdn: String
msisdn = "69 (4615)-11-21"

任何人都可以帮我一个大忙并为我分解吗?

4

0 回答 0