ios - Firemonkey Windows/iOS 兼容代码

标签 ios delphi firemonkey multiplatform

我正在尝试在 iOS 上运行我用 Firemonkey 编写的应用程序。 我读过这个http://docwiki.embarcadero.com/RADStudio/Seattle/en/Migrating_Delphi_Code_to_Mobile_from_Desktop但我的意思是这还不是全部 :-/

我有这个程序

procedure generateData(Var OutVar:String;DataText:String);
var
  S: TBytes;
  StreamInput, StreamOutput: TMemoryStream;
  // String to TBytes
  function UTF8Bytes(const s: UTF8String): TBytes;
  begin
    SetLength(Result, Length(s));
    {$IFDEF IOS} // iOS strings are 0 based but hor about retyped ???
    // this ?
    if Length(Result)>0 then Move(s[0], Result[0], Length(s)-1);
    // or this ?
    //if Length(Result)>0 then Move(s[0], Result[0], Length(s));
    // or this ?
    //if Length(Result)>0 then Move(s[1], Result[0], Length(s));
    {$ELSE} // Win strings are 1 based
    if Length(Result)>0 then Move(s[1], Result[0], Length(s));
    {$ENDIF}
  end;
begin
  StreamInput := TMemoryStream.Create;
  StreamOutput := TMemoryStream.Create;
  S := UTF8Bytes(DataText);

  {$IFDEF IOS} // What about TBytes? They are different too ?
  // this ?
  //StreamInput.Write(S[0], Length(S)-1);
  // or this ?
  StreamInput.Write(S[0], Length(S));
  {$ELSE}
  StreamInput.Write(S[1], Length(S));
  {$ENDIF}

  StreamInput.Position := 0;

  MyCryptoStreamFunction(StreamInput, StreamOutput);
  StreamOutput.Position := 0;

  SetLength(S, StreamOutput.Size);
  {$IFDEF IOS}
  // this ?
  StreamOutput.Read(S[0], StreamOutput.Size);
  // or this ?
  //StreamOutput.Read(S[0], StreamOutput.Size-1);
  // this will raise exception and kill app
  //StreamOutput.Read(S[1], StreamOutput.Size);
  {$ELSE}
  StreamOutput.Read(S[1], StreamOutput.Size);
  {$ENDIF}
  OutVar := StringReplace(EncodeBase64(S,Length(S)), sLineBreak, '',[rfReplaceAll]);
end;

在 Windows 上正常工作,但在 iOS 上此代码 StreamOutput.Read(S[1], StreamOutput.Size); 引发异常并终止我的应用。

任何人都可以帮助 {$IFDEF IOS} 中的代码变体等于 {ELSE} 中的代码吗 通过它们的功能?

最佳答案

使用 Low(s) 获取所有平台的字符串的第一个索引:

// utf8 string to TBytes
function UTF8Bytes(const s: UTF8String): TBytes;
begin
  SetLength(Result, Length(s));
  if (Length(s) > 0) then
    Move(s[Low(s)],Result[0],Length(s));
end;

S := UTF8Bytes(DataText); 在进入函数之前将 unicode 字符串 DataText 隐式转换为 utf8 字符串。

TBytes 是动态字节数组。所有动态数组都是从零开始的。

StreamInput.Write(S[0],Length(S));

使用System.UTF8Encode是将 unicode 字符串转换为 utf8 字节数组的另一种方法:

procedure UTF8Encode(const US: UnicodeString; var B: array of Byte);

关于ios - Firemonkey Windows/iOS 兼容代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40802717/

相关文章:

ios - 使用它作为 BLE 外设在 raspberry-pi 上配置 wifi

c# - 获取 UIView 的大小

delphi - 我想通过 Delphi 来使用 Wifi。如何?

delphi - 使用 RTTI 加载 FireMonkey 风格的资源

android - 在 Delphi 10.3.3 上以 Android API 29 为目标

ios - 在 Swift 2 的绘图应用程序中创建一个 'undo' 函数

iOS 多行标签在 Xcode 7.3 中不支持多行

Delphi TWebBrowser window.devicePixelRatio 属性

delphi - 我如何知道 Delphi IDE 对象检查器位于哪个监视器上?

delphi - TPlane 与 TImage3D?