I'm having a problem adding a TObject
value to a FireMonkey TListBox
in Delphi 10.0 Seattle.
An exeception is raised when casting an Integer
variable to a TObject
pointer.
I tried the cast to TFmxObject
with no success. On Windows, the cast works like a charm, but on Android it raises the exception.
Here is my code:
var
jValue:TJSONValue;
i,total,id: integer;
date: string;
begin
while (i < total) do
begin
date := converteDate(jValue.GetValue('date' + IntToStr(i), ''));
id := StrToInt(jValue.GetValue('id' + IntToStr(i), ''));
ListBox1.Items.AddObject(date, TObject(id));
i := i + 1;
end;
end;
The problem is that on iOS and Android (and soon Linux),
TObject
uses Automatic Reference Counting for lifetime management, and as such you cannot type-cast integer values asTObject
pointers, like you can on Windows and OSX, which do not use ARC. On ARC systems,TObject
pointers must point to real objects, as the compiler is going to perform reference-counting semantics on them. That is why you are getting an exception.To do what you are attempting, you will have to wrap the integer value inside of a real object on ARC systems, eg:
Otherwise, store your integers in a separate list and then use the indexes of the
TListBox
items as indexes into that list when needed, eg:This is portable to all platforms without having to use
IFDEF
s or worrying about ARC.