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
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 as TObject
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:
{$IFDEF AUTOREFCOUNT}
type
TIntegerWrapper = class
public
Value: Integer;
constructor Create(AValue: Integer);
end;
constructor TIntegerWrapper.Create(AValue: Integer);
begin
inherited Create;
Value := AValue;
end;
{$ENDIF}
...
ListBox1.Items.AddObject(date, {$IFDEF AUTOREFCOUNT}TIntegerWrapper.Create(id){$ELSE}TObject(id){$ENDIF});
...
{$IFDEF AUTOREFCOUNT}
id := TIntegerWrapper(ListBox1.Items.Objects[index]).Value;
{$ELSE}
id := Integer(ListBox1.Items.Objects[index]);
{$ENDIF}
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:
uses
.., System.Generics.Collections;
private
IDs: TList<Integer>;
...
var
...
Index: Integer;
begin
...
Index := IDs.Add(id);
try
ListBox1.Items.Add(date);
except
IDs.Delete(Index);
raise;
end;
...
end;
...
Index := ListBox1.Items.IndexOf('some string');
id := IDs[Index];
This is portable to all platforms without having to use IFDEF
s or worrying about ARC.