public 由于编译器中的 'internal' 保护级别,无法访问框架 init 中的结构
public struct in framework init is inaccessible due to 'internal' protection level in compiler
我在名为 "MyFramework"
的框架中有一个结构
public struct ShipmentPackage:Encodable {
let package_code:String
let weight:Float
}
然后,当我尝试在另一个 project/framework
中创建 ShipmentPackage 时
import MyFramework
let onePackage = ShipmentPackage(package_code:"BX",weight:100)
我收到一条错误消息 'ShipmentPackage' 由于 'internal' 保护级别,初始化程序无法访问
我来这个linkhttps://forums.swift.org/t/public-struct-init-is-unexpectedly-internal/5028
我尝试将我的代码更改为以下内容:
第一次尝试:
public struct ShipmentPackage:Encodable {
let package_code:String
let weight:Float
public init(package_code:String,weight:Float){
self.package_code = package_code
self.weight = weight
}
}
第二次尝试:
public struct ShipmentPackage:Encodable {
public let package_code:String
public let weight:Float
public init(package_code:String,weight:Float){
self.package_code = package_code
self.weight = weight
}
}
我也尝试将 package_code 和权重更改为 public,但是上面的 none 有效,我在编译时收到错误消息
<unknown>:0: error: 'init' is inaccessible due to 'internal' protection level
<unknown>:0: note: 'init' declared here
<unknown>:0: error: 'init' is inaccessible due to 'internal' protection level
如有任何提示,我们将不胜感激!
Lesson learned: all public struct need a public init
这不太准确。 documentation 状态:
结构类型的默认成员初始化器
The default memberwise initializer for a structure type is considered private if any of the structure’s stored properties are private. Likewise, if any of the structure’s stored properties are file private, the initializer is file private. Otherwise, the initializer has an access level of internal.
所以内置成员初始化器只在包内可用。如果您不提供 public 初始值设定项,您将无法从外部 space.
创建结构
public struct YourFrameworkStruct {
let frameworkStructProperty: String!
/// Your public structs in your framework need a public init.
///
/// Don't forget to add your let properties initial values too.
public init(frameworkStructProperty: String) {
self.frameworkStructProperty = frameworkStructProperty
}
}
感谢所有评论,
我终于弄清楚为什么它给我错误。
我的两次尝试都应该没问题。
事实证明这个结构没有引起问题
我有其他结构使用此结构并标记为 public,
例如
public struct Shipment:Encodable {
let carrier_code:String
let packages:[ShipmentPackage]
}
缺少初始化程序,但出于某种原因 XCode 不会指示我的工作区的错误,而是在编译时给出错误。
在为所有 public 结构提供初始值设定项后,应用程序通过了编译器。
public struct Shipment:Encodable {
let carrier_code:String
let packages:[ShipmentPackage]
public init(carrier_code:String,packages:[ShipmentPackage]){
self.carrier_code = carrier_code
self.packages = packages
}
}
我原来的 post 不是很好,因为我 post 编写的代码没有任何问题,但决定不删除这个 post,它可能对新手有帮助以后像我一样
经验教训: 所有 public 结构都需要一个 public init
当您尝试访问不同框架中的结构时,
**
all the variables within the struct should be public and should have a
default value set in the init
**
例如:
public 结构 LocationPoint {
public 变量位置:NSNumber
public 变量颜色:UIColor
public init(location: NSNumber? = 0, color: UIColor? = nil ) {
self.location = location ?? 0
self.color = color ?? UIColor.init(red: 255.0/255.0, green: 1.0, blue: 205/255.0, alpha: 1)
}
}
根据调用struct的方式,也可以这样写init
public init() {
self.location = 0
self.color = UIColor.init(red: 255.0/255.0, green: 1.0, blue: 205/255.0, alpha: 1)
}
我在名为 "MyFramework"
的框架中有一个结构public struct ShipmentPackage:Encodable {
let package_code:String
let weight:Float
}
然后,当我尝试在另一个 project/framework
中创建 ShipmentPackage 时import MyFramework
let onePackage = ShipmentPackage(package_code:"BX",weight:100)
我收到一条错误消息 'ShipmentPackage' 由于 'internal' 保护级别,初始化程序无法访问 我来这个linkhttps://forums.swift.org/t/public-struct-init-is-unexpectedly-internal/5028
我尝试将我的代码更改为以下内容:
第一次尝试:
public struct ShipmentPackage:Encodable {
let package_code:String
let weight:Float
public init(package_code:String,weight:Float){
self.package_code = package_code
self.weight = weight
}
}
第二次尝试:
public struct ShipmentPackage:Encodable {
public let package_code:String
public let weight:Float
public init(package_code:String,weight:Float){
self.package_code = package_code
self.weight = weight
}
}
我也尝试将 package_code 和权重更改为 public,但是上面的 none 有效,我在编译时收到错误消息
<unknown>:0: error: 'init' is inaccessible due to 'internal' protection level
<unknown>:0: note: 'init' declared here
<unknown>:0: error: 'init' is inaccessible due to 'internal' protection level
如有任何提示,我们将不胜感激!
Lesson learned: all public struct need a public init
这不太准确。 documentation 状态:
结构类型的默认成员初始化器
The default memberwise initializer for a structure type is considered private if any of the structure’s stored properties are private. Likewise, if any of the structure’s stored properties are file private, the initializer is file private. Otherwise, the initializer has an access level of internal.
所以内置成员初始化器只在包内可用。如果您不提供 public 初始值设定项,您将无法从外部 space.
创建结构public struct YourFrameworkStruct {
let frameworkStructProperty: String!
/// Your public structs in your framework need a public init.
///
/// Don't forget to add your let properties initial values too.
public init(frameworkStructProperty: String) {
self.frameworkStructProperty = frameworkStructProperty
}
}
感谢所有评论, 我终于弄清楚为什么它给我错误。 我的两次尝试都应该没问题。
事实证明这个结构没有引起问题
我有其他结构使用此结构并标记为 public, 例如
public struct Shipment:Encodable {
let carrier_code:String
let packages:[ShipmentPackage]
}
缺少初始化程序,但出于某种原因 XCode 不会指示我的工作区的错误,而是在编译时给出错误。
在为所有 public 结构提供初始值设定项后,应用程序通过了编译器。
public struct Shipment:Encodable {
let carrier_code:String
let packages:[ShipmentPackage]
public init(carrier_code:String,packages:[ShipmentPackage]){
self.carrier_code = carrier_code
self.packages = packages
}
}
我原来的 post 不是很好,因为我 post 编写的代码没有任何问题,但决定不删除这个 post,它可能对新手有帮助以后像我一样
经验教训: 所有 public 结构都需要一个 public init
当您尝试访问不同框架中的结构时, **
all the variables within the struct should be public and should have a default value set in the init
**
例如:
public 结构 LocationPoint { public 变量位置:NSNumber public 变量颜色:UIColor
public init(location: NSNumber? = 0, color: UIColor? = nil ) {
self.location = location ?? 0
self.color = color ?? UIColor.init(red: 255.0/255.0, green: 1.0, blue: 205/255.0, alpha: 1)
}
}
根据调用struct的方式,也可以这样写init
public init() {
self.location = 0
self.color = UIColor.init(red: 255.0/255.0, green: 1.0, blue: 205/255.0, alpha: 1)
}