Code first struct in Swift


(FireSide Experience) #1

Hi,

I am trying to create a struct in Swift code for my code first DA Server. I keep getting a RODL file error when running my DA Server. I have a “struct” defined like this.

public class User: RemObjects.SDK.Types.ComplexType {

var UserID = 0
var UserName = “”
var FirstName = “”
var LastName = “”
var Email = “”
var DateLastLoggedIn: DateTime = DateTime.UtcNow
var Characters: Character[] = [Character()]

}

Is this the right way to create a “struct” in code first?

Thanks!

using Elements version 10.0.0.2305 and VS or Water

Error: image


(marc hoffman) #2

Can you actually “Check the Details property (of the Exception) for a detailed error list”?


(antonk) #4

You have to define properties as public :

public class User: RemObjects.SDK.Types.ComplexType {
  public var UserID = 0
  public var UserName = “” 
  ...

(FireSide Experience) #6

Perfect! Thank you antonk. That worked. :slight_smile:
I do get an error with this line though. How should I be instantiating the DateTime?


(antonk) #7

Which exactly error did you get? Could you show the code around it?
For me this code line works perfectly.


(FireSide Experience) #8

sure,

This is the code

public class User: RemObjects.SDK.Types.ComplexType {

public var UserID = 0
public var UserName = ""
public var FirstName = ""
public var LastName = ""
public var Email = ""
public var DateLastLoggedIn: DateTime = DateTime.UtcNow
public var Characters: Character[] = [Character()]

}

Error

image

Referenced in this method

@ServiceMethod public func GetTwonkUser(_ username: String!) -> User {

    if let dbuser = DataAccess.sharedInstance.rda.GetTable<Users>().Where( { return $0.Username == username }).FirstOrDefault() {
        
        let u = User()
        u.UserID = dbuser.UserID           
        u.UserName = dbuser.Username
        u.FirstName = dbuser.Firstname
        u.LastName = dbuser.Lastname
        u.Email = dbuser.Email
        u.DateLastLoggedIn = dbuser.DateLastLoggedIn  // <----  HERE!

        var ichar = 0           
        for char in DataAccess.sharedInstance.rda.GetTable<Characters>().Where( { return $0.UserID == dbuser.UserID }) {

            // add character               
            let c = Character()
            c.CharacterID = char.CharacterID
            c.Name = char.Name

            // add character stats               
            let chstats = DataAccess.sharedInstance.rda.GetTable<CharacterStats>().Where( { return $0.CharacterID == char.CharacterID } ).FirstOrDefault() 
            c.Level = chstats.Level
            c.Experience = chstats.Experience

            
            var icur = 0
            // add character currencies
            for curdb in DataAccess.sharedInstance.rda.GetTable<Currencies>().Where( { return $0.CharacterID == char.CharacterID } ) {

                let cur = Currency()
                let curType = GetCurrencyTypeInfo(curdb.CurrencyTypeID)
                cur.Name = curType.Name
                cur.Description = curType.Description
                cur.Amount = curdb.Amount

                c.Currencies[icur] = cur

                icur = icur+1

            }

            u.Characters[ichar] = c

            ichar = ichar+1
        
        }

        return u


    } else {
        __throw CharacterException("User " + username + " not found.")
    }

}

(antonk) #9

Unfortunately I cannot reproduce this locally.

Could you create a testcase where this issue reproduces? Please send it to support@ (both sources and binaries)

Thanks in advance


(marc hoffman) #10

If you have Elements RTL in the uses, it has its own DateTime type that RO doesn’t know about. Declare the field explicitly as

public var DateLastLoggedIn: System.DateTime = DateTime.UtcNow

(FireSide Experience) #11

perfect! thanks!