Delphi FireMonkey TListBox AddObject exception on Android

こ雲淡風輕ζ 提交于 2019-12-01 12:52:29

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 IFDEFs or worrying about ARC.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!