Zach
Zach

Reputation: 880

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?

Upvotes: 0

Views: 4188

Answers (1)

user652038
user652038

Reputation:

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.

Upvotes: 2

Related Questions