gbenroscience
gbenroscience

Reputation: 1098

Value of type 'Tags' has no member 'lastUsed'

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.

The offending fields are lastUsed and name.

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.

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​ = ""
    }
    
    
    
}

enter image description here

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?

Upvotes: 2

Views: 593

Answers (2)

Martin R
Martin R

Reputation: 539765

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

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

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

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

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

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.

Upvotes: 1

Dimitris Delis
Dimitris Delis

Reputation: 55

I tried out your code and got the same error results. Even though I can't figure out why the errors are showing, if you change the names to something more specific like tagsLastUsed and tagsName it works fine. My guess is that xcode kinda bugs and thinks these are reserved words. Would comment but I don't have enough points for it so I'm posting as an answer

Upvotes: 1

Related Questions