Write New Or Migrate Existing Code To Android And IOS Using Delphi XE6 Firemonkey

Delphi XE6 Firemonkey Migrate Code From Windows To MobileEmbarcadero has a resource up on their DocWiki where they cover how to move some code which is Windows based to mobile devices like Android and IOS. This information is for Delphi XE6 but most of it also applies to Delphi XE5. The topics that are covered are string types, zero based strings, new versions of Copy, Delete, Pos, and Trim, array types, catching hardware exceptions with try-except blocks, atomic intrinsics, and automatic reference counting.

The string types section is pretty complex and without going into much detail basically you need to be using these string types: System.String, System.Char,  System.Byte,  System.UInt8, System.SysUtils.TStringBuilder, System.String, System.MarshaledString.

The zero based vs. one based strings is also complex but basically in order to be cross platform compatible you need to not access characters in a string directly as if it was an array. Instead you need to use TStringHelper.Chars aka MyString.Chars[index].

Replacements for the Copy, Pos, Delete, and Trim functions which are cross platform are TStringHelper.IndexOf, TStringHelper.Remove, TStringHelper.Substring, and TStringHelper.Trim. So you would use MyString.IndexOf(‘123’) to do a Pos() on the contents of MyString for ‘123’.

The catching hardware exceptions by using a function within the try-except statement was something I didn’t even know before reading the article. Basically to catch hardware exceptions you have to use a function (that CAN NOT be inlined) inside of the try-except block instead of just having straight code.

The Object Pascal compilers don’t support built in assembly code on mobile but you can use atomic intrinsic functions which are System.AtomicExchange, System.AtomicIncrement, System.AtomicCmpExchange, and System.AtomicDecrement.

And lastly the mobile compiles support automatic reference counting. If you need to explicitly free an object you can use the DisposeOf function instead of the Free function. If you are running into Low Memory errors on IOS you might investigate how you can use DisposeOf on your objects instead of Free.

Head over and check out the full resource from Embarcadero about writing new code and migrating existing code to mobile devices.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Leave a Reply

Show Buttons
Hide Buttons
Read previous post:
Simulate Liquid Buoyancy With Box2d In Delphi XE6 Firemonkey On Android And IOS

One of the sample projects included with Box2d Firemonkey for Delphi XE5 & XE6 on Android and IOS is a...