2016-02-13 39 views
2

我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將數據加載到類中時,結構與數據完全匹配。

有人可以幫助我得到這個工作嗎?

回答

2

與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的實例訪問它。這不能直接完成。從您的編碼風格

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

,:你應該在你的AuthenticationInfo類中創建的RootObject一個實例:

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

現在你可以創建的AuthenticationInfo一個實例,並訪問存儲在認證信息的rootObj我猜你是Java程序員。你明確地寫出所有類型的變量。但很快,這很少被使用。

+0

我是否應該將類改爲結構體? –

+0

@GeorgeMCeaserJr你可以。請注意,結構不能從其他類型繼承,它們是值而不是引用。 IMO的'ResultInfo'和'AdminParameters'適合成爲結構。 – Sweeper

+0

所以,即使當我將這兩個結構更改爲結構時,我仍然得到相同的錯誤。這些類是從C#轉換而來的。是否有一個不同的工具可用於直接從生成的JSON中在Swift中創建數據模型?基本上我想將我的JSON字符串傳遞給EVReflection提供的AuthenticationInfo類的init方法,並讓它創建整個AuthenticationInfo對象並允許我訪問所有的子對象。有沒有辦法做到這一點? –

相關問題