Recent

Author Topic: ObjC for loop to pascal  (Read 3647 times)

rossh_lz

  • New Member
  • *
  • Posts: 36
ObjC for loop to pascal
« on: February 17, 2022, 09:04:25 am »
How to write a Pascal version of this style of c loop?


for (NSString *key in dictionary) {

  // do something with key
}



// dictionary is an  objcclass  NSDictionary

Thanks

Zvoni

  • Hero Member
  • *****
  • Posts: 3466
Re: ObjC for loop to pascal
« Reply #1 on: February 17, 2022, 10:50:05 am »
at a guess: TStringList or TFPGMap as a Dictionary
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2032
  • Former Delphi 1-7, 10.2 user
Re: ObjC for loop to pascal
« Reply #2 on: February 17, 2022, 12:13:49 pm »
Here's an example from something I wrote ages ago:

Code: Pascal  [Select][+][-]
  1. ...
  2.      myDictionary : NSDictionary;
  3.      i  : Byte = 0;  
  4. ...
  5.      myDictionary := NSProcessInfo.ProcessInfo.environment;
  6.      for i := 0 to (myDictionary.count - 1) do
  7.        begin
  8.          Form1.Memo1.Lines.Add('Env Var: '
  9.            + myDictionary.allKeys.objectAtIndex(i).UTF8String
  10.            + ' = '
  11.            + myDictionary.allValues.objectAtIndex(i).UTF8String
  12.          );
  13.        end;

Outputs:

Quote
Env Var: COMMAND_MODE = unix2003
Env Var: PATH = /usr/bin:/bin:/usr/sbin:/sbin
Env Var: TMPDIR = /var/folders/t3/f0rslzrn70qb61l12j31c5sr0000gn/T/
Env Var: __CFBundleIdentifier = com.company.project1
Env Var: LOGNAME = trev
Env Var: HOME = /Users/trev
Env Var: XPC_FLAGS = 0x0
Env Var: DISPLAY = /private/tmp/com.apple.launchd.e7IspcKmOY/org.xquartz:0
Env Var: SSH_AUTH_SOCK = /private/tmp/com.apple.launchd.0xEkRTg8Fg/Listeners
Env Var: USER = trev
Env Var: XPC_SERVICE_NAME = application.com.company.project1.783302.10524195
Env Var: SHELL = /bin/tcsh
Env Var: __CF_USER_TEXT_ENCODING = 0x1F5:0x0:0xF

Full example in my Wiki article:  Accessing macOS System Information.
« Last Edit: February 17, 2022, 12:21:02 pm by trev »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1071
Re: ObjC for loop to pascal
« Reply #3 on: February 17, 2022, 09:28:09 pm »
FPC supports the so-called Objective-C fast enumeration using the Pascal "for x in y do" construct. See https://wiki.freepascal.org/FPC_PasCocoa#Fast_enumeration (you do need to activate the objectivec2 modeswitch)

 

TinyPortal © 2005-2018