gpt4 book ai didi

swift - 访问子类时不能在类型错误的实例上使用静态成员

转载 作者:可可西里 更新时间:2023-11-01 01:37:22 25 4
gpt4 key购买 nike

我 100% 确定这个问题已得到解答,但我发现的答案似乎无法融入我的解决方案。我有以下代码作为 Playground 。

import Foundation

public class AuthenticationInfo
{
public class RootObject
{
var resultParameters: ResultParameters = ResultParameters()

}

public class ResultParameters
{
var adminInfo: AdminInfo = AdminInfo()
var ServerBuildNumber: Int = -1
var ServerIs64Bit: Bool = false
var ServerOSPlatform: Int = -1
var ServerStartTime: Int64 = -1
var ServerVersion: String = ""
var EnableLiveDataUpdates: Bool = false
var DelayLiveDataUpdates: String = ""
var SessionTimeout: Int = 0;
var ServerName: String = ""
var ServerPort: Int = -1
var UserName: String = ""
var WebAPIVersion: String = ""
var Locale: String = ""
}

public class AdminInfo
{
var AdminUUID: String = ""
var AllowChangeAgentClientInfoSettings: Int = -1
var AllowChangeAgentCustomFieldSettings: Int = -1
var AllowChangeAgentGeneralSettings: Int = -1
var AllowChangeAgentServerSettings: Int = -1
var AllowChangeComputerTracking: Int = -1
var AllowChangeComputerTrackingScreenshot: Int = -1
var AllowChangeCustomFields: Int = -1
var AllowChangeHistoryOptions: Int = -1
var AllowChangeMobileDeviceTracking: Int = -1
var AllowEnterCustomFieldData: Int = -1
var AllowManageDeviceUsers: Int = -1
var AllowManageiOSDevices: Int = -1
var AllowMobileRemoteControl: Int = -1
var AllowModifyADEPAccount: Int = -1
var AllowModifyBookstoreBooks: Int = -1
var AllowModifyMDMDeviceEnrollmentProfiles: Int = -1
var AllowModifyMobileActions: Int = -1
var AllowModifyMobileMedia: Int = -1
var AllowModifySDConfigProfiles: Int = -1
var AllowModifySDGroups: Int = -1
var AllowModifySDImages: Int = -1
var AllowModifySDPackages: Int = -1
var AllowModifySLPackages: Int = -1
var AllowModifySamsungLicenses: Int = -1
var AllowModifyStagingServers: Int = -1
var AllowModifyVPPAccount: Int = -1
var AllowModifyiOSApplications: Int = -1
var AllowModifyiOSConfigurationProfiles: Int = -1
var AllowModifyiOSPolicies: Int = -1
var AllowRemoteControl: Int = -1
var AllowRemoveCommandsFromHistory: Int = -1
var AllowRemoveComputerRecords: Int = -1
var AllowRemoveInventoryData: Int = -1
var AllowRemoveLicenseReports: Int = -1
var AllowRemoveSDLogEntry: Int = -1
var AllowRemoveiOSDeviceRecords: Int = -1
var AllowRemoveiOSHistoryCommands: Int = -1
var AllowResetSDPackges: Int = -1
var AllowRetrySDPackges: Int = -1
var AllowVPPLicenseManagement: Int = -1
var AllowViewAdminCenter: Int = -1
var AllowViewCommandsWindow: Int = -1
var AllowViewComputerTrackingData: Int = -1
var AllowViewComputerTrackingScreenshot: Int = -1
var AllowViewCustomFields: Int = -1
var AllowViewMobileDeviceTrackingData: Int = -1
var AllowViewSDCenter: Int = -1
var AllowViewSLCenter: Int = -1
var AllowViewServerStatus: Int = -1
var CanChangeServerSettings: Int = -1
var CanDeployAgents: Int = -1
var CanLogin: Int = -1
var CanSeeAllRecords: Int = -1
var CommandPermissions: Int64 = -1
var CommandPermissions2: Int = -1
var IsADUser: Int = -1
var IsSuperAdmin: Int = -1
var id: Int = -1
var CommandPermissionsLow32: Int64 = -1
var CommandPermissionsHigh32: Int = -1
}

}

var lobj_AuthenticationInfo: AuthenticationInfo
var lobj_AuthenticationInfo_RootObject: AuthenticationInfo.RootObject

lobj_AuthenticationInfo = AuthenticationInfo()
lobj_AuthenticationInfo_RootObject = lobj_AuthenticationInfo.RootObject

在最后一行出现以下错误:

静态成员 RootObject 不能用于 AuthenticationInfo 类型的实例。

更多信息:这些类是由代码生成器基于 JSON 数据创建的,这些数据将从我调用的 Web API 返回。因此,我相信这些对象的结构需要保持完整,所以当我使用 EVRefelction 将数据加载到类中时,结构与数据完全匹配。

有人可以帮助我让它工作吗?

最佳答案

与 Java 不同,在 swift 中,所有内部类都是隐式的 static。例如:

public class Outer {
public class Inner {

}
}

当你想创建一个Inner类型的对象时,你不需要先创建一个Outer类型的对象。你可以这样做:

let x = Outer.Inner()

而且因为内部类是静态的,所以不能用外部类的实例访问它们:

let x = Outer()
let y = x.Inner //Wrong!

我认为您在这里要做的是使 RootObject 成为非静态的,以便可以从 AuthenticationInfo 的实例访问它。这不能直接完成。您应该在 AuthenticationInfo 类中创建一个 RootObject 的实例:

public class AuthenticationInfo {
public class RootObject {
//blah blah blah
}
// Other inner classes goes here
var rootObj = RootObject()
}

现在您可以创建一个 AuthenticationInfo 的实例并访问存储在 Authentication Info 中的 rootObj:

var x = AuthenticationInfo()
var y = AuthenticationInfo.rootObj

从您的编码风格来看,我猜您是一名 Java 程序员。您显式地编写所有类型的变量。但在 swift 中,很少使用。

关于swift - 访问子类时不能在类型错误的实例上使用静态成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35375126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com