Add new record - DA/Cocoa

I’m familiar with DataAbstract for .NET and use it in pretty much all my .NET apps. I’ve been starting on my first read macOS app in Oxygene and have been learning the DA API for Cocoa.

Today, I was trying to implement adding a new record to a table and wrote up this code:

  var NewJob := JobsDataTable.addNewRow;
  NewJob.setValue(AClientNum) forKey('ClientNumber');
  NewJob.setValue(AClientName) forKey('ClientName');
  NewJob.setValue(AJobDesc) forKey('JobDescription');
  NewJob.setValue(AJobNotes) forKey('JobNotes');
  NewJob.setValue(6000) forKey('JobNumber');
  NewJob.setValue('alan') forKey('User');
 { fill in remaining fields }
  DataLib.fDataModule.applyChangesForTable(JobsDataTable);

Is this the proper coding approach? my code is crashing on that last line with this exception

2017-12-12 09:13:21.759810-0600 maajobs[5192:843305] -[__ElementsBoxedChar length]: unrecognized selector sent to instance 0x7f9c19dd4760
!> Exception of type NSException on thread DE29
!> Message: -[__ElementsBoxedChar length]: unrecognized selector sent to instance 0x7f9c19dd4760

has anyone looked into this?

Alan,

where do the 4 variables you pass in come from? is one of them a char (instead of a string) by any chance?

in true Cocoa style, the row dictionary is untapped and can take any object value, but the actual values you set just match the value expected for the field (eg a string, an integer/nsnumber, a date, or whatever the field is defined as.

tis sounds like you are passing a char instead fo a string, and because the dictionary is not strongily typed, th compiler cannot automatically convert the it to a string for you. eg:

var c := 'a'; // thats a char;
var s: String := x; // compiler converts to a char fior you
var I: id := x; // compiler won't convert, you keep the char.

the dictionary is like the last version.

does this make sense?

Thanks for the reply, marc. This seems to be a “.NET vs Cocoa” difference.

Yes, in my database here we quite often use single character fields. In the .NET world, these are equivalent to a string. Obviously that is not the case in the realm of Cocoa. good thing to know.

I’ll take a look again a little later today to try your approach of assigning the char to an Id, and then assigning that to the field???

Thanks

alan

thanks. this works, makes sense, and solves my current issue.

Things SHOULD go smoother now.

alan

1 Like

Not exactly. Chars and String are different in both, but the compiler will convert a char to a String, when it knows one is expected (on both platforms). because the datable is untyped, to doesn’t know that. the same is true in .NET:

var c := 'a'; // thats a char;
var s: String := x; // compiler converts to a char fior you
var I: object := x; // compiler won't convert, you keep the char.

no, thats exactly the wrong thing to do; I used the to illustrate whats happening now :wink: cast it to a string explicitly, or assign it to a variable that’s strongly typed as string.

var x := 'a';
NewJob.setValue(x) forKey('ClientNumber'); // will fail

var x := 'a';
NewJob.setValue(x as String) forKey('ClientNumber'); // will be ok

var x: String := 'a';
NewJob.setValue(x) forKey('ClientNumber'); // will be ok

Yeah, I did figure this out. Carlo told me a nice fact about the Elements compiler and Cocoa that everyone should know…

var x := ‘a’; << results in x being a Char

var x := “a”; << results in x being a string.

Alan

1 Like