iOS - 由于 'internal' 保护级别(来自我的 pod),'val' 无法访问
iOS - 'val' is inaccessible due to 'internal' protection level(from my pod)
所以我构建了一个 pod,我正尝试在我的项目中使用它。
我有这个数据结构:
public struct Device {
public init() {}
var id: String = ""
var serial: String = ""
var account: String = ""
var name: String = ""
var isAttached: Bool?
var isShared: Bool?
}
我在解析器中有这个函数:
func getDevices(json: JSON) -> [Device] {
var devices = [Device]()
for device in json.arrayValue {
var item = Device()
item.account = device["account"].string!
item.id = device["id"].string!
item.isAttached = device["isAttached"].boolValue
item.isShared = device["isShared"].boolValue
item.name = device["name"].string!
item.serial = device["serial"].string!
devices.append(item)
}
return devices
}
和apiclass中的这个方法:
func getDevices( success: @escaping ([Device]) -> Void, failure: @escaping (String) -> Void) {
let headers: HTTPHeaders = [
"Authorization": "Bearer \(UserDefaults.standard.string(forKey: "accessToken")!)",
"X-Account-ID": UserDefaults.standard.string(forKey: "accountToken")!
]
Alamofire.request(Constants.Url.PENDING_DEVICE, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).validate().responseJSON { response in
if response.result.isSuccess {
guard let data = response.data else { return }
let json = JSON(data)
self.devices = self.helper.getDevices(json: json)
success(self.devices)
}
if response.result.isFailure {
print("status code is:\(String(describing: response.response?.statusCode))")
let error : Error = response.result.error!
print(error.localizedDescription)
failure(error.localizedDescription)
}
}
}
然而当我在项目中使用它时是这样的:
self.requestManager.getDevice(success: { (devices) in
for device in devices {
var item = Device()
item = device
item.name //'name' is inaccessible due to 'internal' protection level error
}
}) { (error) in
//
}
在其他结构中,我创建了这个完美的作品,有什么想法吗?我以为 public init() {}
是解决此问题的方法。
如果您单独构建一个 pod 并在框架内使用它,那么在 pod 中声明的所有变量都应该是 public
或 open
.
示例:
public struct Device {
...
...
public var name: String = ""
...
}
public
和 open
仅适用于框架编写者,这意味着在框架之外(没有项目之外:))。
public - This can be used by objects outside my framework.
open - public and objects outside my framework can subclass this.
所以我构建了一个 pod,我正尝试在我的项目中使用它。 我有这个数据结构:
public struct Device {
public init() {}
var id: String = ""
var serial: String = ""
var account: String = ""
var name: String = ""
var isAttached: Bool?
var isShared: Bool?
}
我在解析器中有这个函数:
func getDevices(json: JSON) -> [Device] {
var devices = [Device]()
for device in json.arrayValue {
var item = Device()
item.account = device["account"].string!
item.id = device["id"].string!
item.isAttached = device["isAttached"].boolValue
item.isShared = device["isShared"].boolValue
item.name = device["name"].string!
item.serial = device["serial"].string!
devices.append(item)
}
return devices
}
和apiclass中的这个方法:
func getDevices( success: @escaping ([Device]) -> Void, failure: @escaping (String) -> Void) {
let headers: HTTPHeaders = [
"Authorization": "Bearer \(UserDefaults.standard.string(forKey: "accessToken")!)",
"X-Account-ID": UserDefaults.standard.string(forKey: "accountToken")!
]
Alamofire.request(Constants.Url.PENDING_DEVICE, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).validate().responseJSON { response in
if response.result.isSuccess {
guard let data = response.data else { return }
let json = JSON(data)
self.devices = self.helper.getDevices(json: json)
success(self.devices)
}
if response.result.isFailure {
print("status code is:\(String(describing: response.response?.statusCode))")
let error : Error = response.result.error!
print(error.localizedDescription)
failure(error.localizedDescription)
}
}
}
然而当我在项目中使用它时是这样的:
self.requestManager.getDevice(success: { (devices) in
for device in devices {
var item = Device()
item = device
item.name //'name' is inaccessible due to 'internal' protection level error
}
}) { (error) in
//
}
在其他结构中,我创建了这个完美的作品,有什么想法吗?我以为 public init() {} 是解决此问题的方法。
如果您单独构建一个 pod 并在框架内使用它,那么在 pod 中声明的所有变量都应该是 public
或 open
.
示例:
public struct Device {
...
...
public var name: String = ""
...
}
public
和 open
仅适用于框架编写者,这意味着在框架之外(没有项目之外:))。
public - This can be used by objects outside my framework.
open - public and objects outside my framework can subclass this.