Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Cross-Platform-Entwicklung (https://www.delphipraxis.net/91-cross-platform-entwicklung/)
-   -   IOS Platform Funktion cast {class}Pointer to Interface ??? (https://www.delphipraxis.net/217685-ios-platform-funktion-cast-%7Bclass%7Dpointer-interface.html)

QuickAndDirty 19. Aug 2025 10:20

IOS Platform Funktion cast {class}Pointer to Interface ???
 
ich habe diese schöne KI generierte unit ...die Fehler habe ich fast alle korrigiert... außer diesen einen.

Delphi-Quellcode:
unit uCurrentTimeZoneID;

interface
uses SysUtils,
     Types,
     Classes;

Function GetCurrentTimeZoneID:String;

implementation

Uses dateutils
    , Macapi.Helpers
    , Macapi.ObjectiveC
    , iOSapi.Foundation
    , FMX.Helpers.iOS ;

Function GetCurrentTimeZoneID:String;
Begin
  // On iOS, we use the Foundation framework's NSTimeZone class.
  // The 'name' property provides the IANA identifier.
  var LTimeZone: NSTimeZone;
  try
    // Get the system's local timezone object
    LTimeZone := TNSLocalTimeZone.OCClass.localTimeZone; <--------FEHLER : Localtimezone ist ein Pointer aber NSTimeZone ist ein Interface...
    if LTimeZone <> nil then
      Result := NSStrToStr(LTimeZone.name) // convert NSString to String
    else
      Result := TTimeZone.Local.ID; // Fallback if the object is nil
  except
    on E: Exception do
      Result := TTimeZone.Local.ID;// In case of any error, fallback to the default Delphi implementation
  end;
End;

end.
Allerding liefert TNSLocalTimeZone.OCClass NUR Pointer wo es eigentlich irgendwelche Objekte liefern sollte.
kann ich das einfach mit nem Typecast hinter mich bringen?
Delphi-Quellcode:
LTimeZone := NSTimeZone(TNSLocalTimeZone.OCClass.localTimeZone);

Rollo62 19. Aug 2025 13:33

AW: IOS Platform Funktion cast {class}Pointer to Interface ???
 
Probier das mal mit wrap()

Delphi-Quellcode:
function GetCurrentTimeZoneID: string;
var
  LTZPtr: Pointer;
  LTimeZone: NSTimeZone;
begin
  try
    // 1) Pointer aus der Klassen-API holen
    LTZPtr := TNSTimeZone.OCClass.localTimeZone;

    // 2) In Interface „wrappen“
    if LTZPtr <> nil then
    begin
      LTimeZone := TNSTimeZone.Wrap(LTZPtr);
      // 3) NSString -> Delphi-String
      Result := NSStrToStr(LTimeZone.name); // IANA-ID, z.B. "Europe/Berlin"
      Exit;
    end;

    // Fallback
    Result := TTimeZone.Local.ID;
  except
    on E: Exception do
      Result := TTimeZone.Local.ID;
  end;
end;

end.

QuickAndDirty 19. Aug 2025 15:00

AW: IOS Platform Funktion cast {class}Pointer to Interface ???
 
OMG danke das sieht gut aus.
So netto war keine Zeile von dem KI code korrekt, aber trotzdem war die "Idee" die richtige und es ließ sich alles korrigieren.


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:21 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz