Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

ios - Swift 5 Value of type has no member

I created a swift xcframework with a single public func:

public func getClientWithCompletion(clientId: String, completion: @escaping (Result<Client, Error>) -> Void) {
    let urlString = "https://api-here.com"
    guard let url = URL(string: urlString) else { return }
    
    URLSession.shared.dataTask(with: url) { (data, resp, err) in
        if let err = err {
            completion(.failure(err))
            return
        }
        
        do {
            let client = try JSONDecoder().decode(Client.self, from: data!)
            print("client: ", client)
            print("client name: ", client.name)
            completion(.success(client))
        } catch let jsonError {
            completion(.failure(jsonError))
        }
    }.resume()
}

The two print statements look correct. I'm seeing the client:

Client(name: "Blueprint")

and the client name:

Blueprint

Here is the Client struct

public struct Client {
    let name: String
}

extension Client: Codable {
    enum CodingKeys: String, CodingKey {
        case name
    }
}

However, when I call this function from a demo project I'm not able to access the member name:

astra.getClientWithCompletion(clientId: "7ce19ab3d29c4680b1f9e7e135472bec") { res in
    switch res {
    case .success(let client):
        print(client) <-- prints fine
        print("new way of showing client: ", client.name) <-- Value of type 'Client' has no member 'name'
                
    case .failure(let error):
        print("failure: ", error)
    }
}

What is going on here where I can't access the name member of Client on the demo app?

question from:https://stackoverflow.com/questions/65894244/swift-5-value-of-type-has-no-member

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you define a type’s access level as internal or public (or use the default access level of internal without specifying an access level explicitly), the default access level of the type’s members will be internal.

https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

I hate that, and wish it worked like you were thinking it would. The big problem with it is inconsistency: public will cascade down, but only in a public extension. As such, I recommend using extensions for access grouping. You can't do that with stored properties, though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...