我100%確定這已被回答,但我找到的答案似乎無法適合我的解決方案。我有以下代碼遊樂場。當訪問子類時,靜態成員不能用於類型錯誤的實例
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將數據加載到類中時,結構與數據完全匹配。
有人可以幫助我得到這個工作嗎?
我是否應該將類改爲結構體? –
@GeorgeMCeaserJr你可以。請注意,結構不能從其他類型繼承,它們是值而不是引用。 IMO的'ResultInfo'和'AdminParameters'適合成爲結構。 – Sweeper
所以,即使當我將這兩個結構更改爲結構時,我仍然得到相同的錯誤。這些類是從C#轉換而來的。是否有一個不同的工具可用於直接從生成的JSON中在Swift中創建數據模型?基本上我想將我的JSON字符串傳遞給EVReflection提供的AuthenticationInfo類的init方法,並讓它創建整個AuthenticationInfo對象並允許我訪問所有的子對象。有沒有辦法做到這一點? –