“标签"类型的值没有成员“lastUsed" [英] Value of type 'Tags' has no member 'lastUsed'

查看:29
本文介绍了“标签"类型的值没有成员“lastUsed"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Swift 类,标签.

I have a class, Tags, in Swift.

public class Tags {
    var lastUsed​: UInt64
    var frequency: Int
    var id: String
    var name​: String

    init(id: String = "", frequency: Int, lastUsed: UInt64, name: String) {
        self.id = id
        self.frequency = frequency
        self.lastUsed​ = lastUsed
        self.name​ = name
    }
}

我可以访问项目中某些类的字段,但很有趣的是,我似乎无法访问某些类的某些字段.

I can access the fields from some classes in my project, but funny enough I cant seem to access some of the fields from some classes.

违规字段是 lastUsedname.

当我运行 Find Selected Symbol in Workspace 时,我发现这些字段在某些引用它们的文件中可见,而在其他文件中不可见.

When I run Find Selected Symbol in Workspace , I discovered that the fields are visible from some files where they are referenced and are not visible from other files.

我实际上可以访问类中Tags类的其他字段.

I can actually access the other fields of the class Tags in the class.

我无法访问它的类具有以下定义:

The class I cannot access it from has the following definition:

import RealmSwift


 class StoredTags: Object{
    
    @objc dynamic var frequency: Int
    @objc dynamic var id: String
    @objc dynamic var lastUsed​: UInt64
    @objc dynamic var name​: String
    
    
    init(freq: Int,  id: String, lastUsed: UInt64, name: String){
        self.lastUsed​ = lastUsed
        self.name​ = name
        self.frequency = freq
        self.id = id
    }
    
    init(t: Tags){
        self.lastUsed​ = t.lastUsed. //Error-- Value of type 'Tags' has no member 'lastUsed'
        self.name​ = t.name​
        self.frequency = t.frequency
        self.id = t.id
        
    }
    class func getFromTags(t: Tags) -> StoredTags{
        let st = StoredTags()
        st.frequency = t.frequency
        st.id = t.id
        st.lastUsed​ = t.lastUsed//Error-- Value of type 'Tags' has no member 'lastUsed'
        st.name​ = t.name//Error-- Value of type 'Tags' has no member 'name'
        
        return st
        
    }
    
    required override init() {
        self.frequency = 0
        self.id = ""
        self.lastUsed​ = 0
        self.name​ = ""
    }
    
    
    
}

我怀疑这可能是 xcode 的一些问题;但我已经尝试了所有想到的.

I suspect it might be some issue with xcode; but I have tried all that comes to mind.

请问这可能是什么问题?

What could be the issue here, please?

推荐答案

问题是 namelastUsed 的声明有一个不可见的字符(U+200B =零宽度空间")作为标识符的最后一个字符.

The problem is that the declarations of name and lastUsed have an invisible character (U+200B = "ZERO WIDTH SPACE") as the last character of the identifier.

这是一个简短的例子,展示了似乎是一个悖论.第一个打印语句不编译,但最后一个编译:

Here is a short example demonstrating what seems to be a paradox. The first print statement does not compile, but the last one does:

struct Tags {
    let name​ = "name"
}

print(Tags().name) // (1) Error: Value of type 'Foo' has no member 'name'
print(Tags().name​) // (2) No error

不幸的是,即使打开了Editor->Invisibles",Xcode 也不会显示此字符.在 vi 中打开文件显示问题:

Unfortunately, Xcode does not display this character, even if "Editor->Invisibles" is switched on. Opening the file in vi shows the issue:

struct Tags {
    let name<200b> = "name"
}

print(Tags().name) // (1) Error: Value of type 'Foo' has no member 'name'
print(Tags().name<200b>) // (2) No error

请注意,标识符名称中允许使用不可见字符,这已在 Swift 论坛.

Note that invisible characters are allowed in identifier names, this has been discussed in the Swift forum.

第一个打印语句是使用 Xcode 的代码完成创建的,Xcode 省略尾随的不可见字符.我会认为这是一个错误.

The first print statement was created with Xcode's code completion and Xcode omits the trailing invisible character. I would consider that a bug.

第二个打印语句是通过仔细复制name 标识符(包括尾随的不可见字符)创建的.

The second print statement was created by carefully copying the name​ identifier including the trailing invisible character.

总结: Swift 标识符可以包含不可见的字符,但这些字符不能很好地与 Xcode 的代码完成配合使用,只会引起混淆.重写所有出现的这些标识符可以解决问题.

Summary: Swift identifiers can contain invisible characters, but those do not work well with Xcode's code completion and cause only confusion. Rewriting all occurrences of those identifiers fixes the issue.

这篇关于“标签"类型的值没有成员“lastUsed"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆