George M Ceaser Jr
George M Ceaser Jr

Reputation: 1759

Static Member cannot be used on instance of type error when accessing sub classes

I am 100% sure this has been answered but the answers I am finding I cannot seem to make fit into my solution. I have the following code a 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

On the last line I am getting the following error:

Static member RootObject cannot be used on instance of type AuthenticationInfo.

MORE INFO: These classes were created by a code generator based on JSON data that will be returned from a web api I call. Thus, I believe the structure of these objects needs to stay intact so when I use EVRefelction to load the data into the class, the structure matches exactly the data.

Can someone please assist me in getting this to work?

Upvotes: 3

Views: 6206

Answers (1)

Sweeper
Sweeper

Reputation: 270790

Unlike Java, in swift, all inner classes are implicitly static. For example:

public class Outer {
    public class Inner {

    }
}

When you want to create an object of type Inner, you don't need to create an object of type Outer first. You can just do this:

let x = Outer.Inner()

And because inner classes are static, they cannot be accessed with an instance of the outer class:

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

I think what you want to do here is to have the RootObject non-static so that it can be accessed from an instance of AuthenticationInfo. This cannot be done directly. You should create an instance of RootObject in your AuthenticationInfo class:

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

Now you can create an instance of AuthenticationInfo and access the rootObj stored in the Authentication Info:

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

From your style of coding, I guess you were a Java programmer. You write all the types of variables explicitly. But in swift, this is seldomly used.

Upvotes: 2

Related Questions