How to create an NSArray lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- LazProgger · 2016-05-16 16:32 I need to create an array of type NSArray containing a list of files on Mac OS X. However, I cannot find any constructor or other method to create such an array. I have searched the web and found that Delphi has a TNSArray.Create, in Lazarus I have found nothing similar. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  var  AFiles: array of string;  ANSFiles: NSArray;  ANSString: NSString;  i: integer;begin   ANSFiles := NSArray. ??? // How can I create a new array?   for i := 0 to length(AFiles) - 1 do begin    ANSString := NSSTR(pchar(AFiles[i])));    ANSFiles. ??? // How can I add ANSString?  end; end;         Up to now, this is my code. How can I create the array and add items? ---------------------------------------------------------------------- skalogryz · 2016-05-16 16:51 look at this web Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$modeswitch objectivec1}  var  AFiles: array of string;  ANSFiles: NSMutableArray;  ANSString: NSString;  i: integer;begin   ANSFiles := NSMutableArray(NSMutableArray.array).init; // How can I create a new array?   for i := 0 to length(AFiles) - 1 do begin    ANSString := NSSTR(pchar(AFiles[i])));    ANSFiles.addObject(ANSString);  end; end; ---------------------------------------------------------------------- Jonas Maebe · 2016-05-17 06:58 That should be "NSMutableArray.alloc" instead of "NSMutableArray.array". You also don't need the NSMutableArray() typecast, because NSMutableArray.alloc returns an id. @LazProgger: as indirectly suggested by skalogryz, you cannot use Objective-Pascal without knowing at least some basics of Objective-C. We did not integrate Objective-C into Pascal, but created a new Pascal dialect that is almost a 1-to-1 translation of Objective-C (and that can be mixed with regular Object Pascal, like with C++ and Objective-C in Objective-C++). See http://wiki.freepascal.org/FPC_PasCocoa for more information, but again, that assumes a basic knowledge of Objective-C. You can find a very limited overview of how to do some basic things at http://wiki.freepascal.org/FPC_PasCocoa/Differences ---------------------------------------------------------------------- BeniBela · 2016-05-17 08:53 An rray for the NSA? Do not give them stuff, they will spy on you! Especially with an r-ray, it is like an x-ray, is it not? ---------------------------------------------------------------------- skalogryz · 2016-05-17 12:32 Quote from: BeniBela on May 17, 2016, 10:53:52 amAn rray for the NSA? History reference: "NS" in lots of Cocoa naming comes from NeXTSTEP system. ---------------------------------------------------------------------- skalogryz · 2016-05-17 13:03 Quote from: Jonas Maebe on May 17, 2016, 08:58:28 amThat should be "NSMutableArray.alloc" instead of "NSMutableArray.array"Now I'm thinking that a method cannot be named "array" since array is a reserved word. So the name of the method should be array_. packages/cocoaint/src/foundation/NSArray.inc Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { NSArrayCreationCategory }  NSArrayCreationCategory = objccategory external (NSArray)    class function array_: id; message 'array';    class function arrayWithObject(anObject: id): id; message 'arrayWithObject:';   ...  Quote from: Jonas Maebe on May 17, 2016, 08:58:28 amYou also don't need the NSMutableArray() typecast, because NSMutableArray.alloc returns an id. is "id" type treated as NSObject with "init" method declared? ---------------------------------------------------------------------- Jonas Maebe · 2016-05-17 13:20 Quote from: skalogryz on May 17, 2016, 03:03:46 pm Quote from: Jonas Maebe on May 17, 2016, 08:58:28 amYou also don't need the NSMutableArray() typecast, because NSMutableArray.alloc returns an id. is "id" type treated as NSObject with "init" method declared? The "id" identifier is treated more or less the same as in Objective-C: it's an Objective-Pascal/C class instance of an arbitrary type, to which you hence can send any message that's in scope. ---------------------------------------------------------------------- skalogryz · 2016-05-17 13:39 Quote from: Jonas Maebe on May 17, 2016, 03:20:46 pmThe "id" identifier is treated more or less the same as in Objective-C: it's an Objective-Pascal/C class instance of an arbitrary type, to which you hence can send any message that's in scope. Hmm... does it mean that by Code: [Select]NSMutableArray.alloc| the NSMutableArray class is still in the scope, thus calling .init method works? (sorry, for asking the question, I'm out of OSX machine now). ---------------------------------------------------------------------- LazProgger · 2016-05-17 13:46 @Jonas: Thank you for the link. Yes, I do not know anything about Objective C. I just want to try to port an application to Mac OS X and I am trying to find some equivalents for Windows Api functions I have used in the Windows version. So,  I have searched the web for some code and mostly found Objective C examples that are often using the NSArray type to pass information to the system. That was how I came to this question. And thanks for the code examples! I would never had come to this on my own! ---------------------------------------------------------------------- Jonas Maebe · 2016-05-17 15:17 Quote from: skalogryz on May 17, 2016, 03:39:49 pmQuote from: Jonas Maebe on May 17, 2016, 03:20:46 pmThe "id" identifier is treated more or less the same as in Objective-C: it's an Objective-Pascal/C class instance of an arbitrary type, to which you hence can send any message that's in scope. Hmm... does it mean that by Code: [Select]NSMutableArray.alloc| the NSMutableArray class is still in the scope, thus calling .init method works? The scope is the regular Pascal scope. E.g., if the unit CocoaAll is in your uses clause, then all methods from all classes, interfaces and categories declared in that unit are in scope. ---------------------------------------------------------------------- skalogryz · 2016-05-17 15:42 Quote from: Jonas Maebe on May 17, 2016, 05:17:29 pmThe scope is the regular Pascal scope. E.g., if the unit CocoaAll is in your uses clause, then all methods from all classes, interfaces and categories declared in that unit are in scope. all methods of all classes are available for "id" type? interesting... is/were there an issue with name collisions? (resolved at compile-time by throwing an error?) Unrealistic example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    // ObjC method has underscore in its name  NSClass1 = objcclass    function array_ : integer; message 'array_';  end;   // ObjC method doesn't have an underscore in its name  // but pascal declaration does  NSClass2 = objcclass    function array_ : integer; message 'array';   end; var  m : id;...  m.array_;  --- Please don't bother answering. I've reread the wiki, it's explained. ---------------------------------------------------------------------- LazProgger · 2016-05-17 23:29 I have tried it and the NSMutableArray.alloc.init and NSMutableArray.release thing is working. However, neither alloc, init and relase are suggested by the code completion nor I can find something in the unit where NSArray is declared. Nevertheless, the compiler is not complaining. Is there a reason for that? ---------------------------------------------------------------------- skalogryz · 2016-05-18 01:52 Quote from: LazProgger on May 18, 2016, 01:29:05 amIs there a reason for that? Depends on what version of Lazarus is being used. (You've to understand that code completion is not based on FPC sources, but a stand-alone CodeTools library). The trunk version of Lazarus finds NSArray declaration (the actual file) without problem. But there're two other issues. 1) CodeTools seems to be confused by redeclared NSObject at (packages/cocoaint/src/AnonClassDefinitionQuartzcore.pas). That prevents Lazarus from showing "alloc" and "init" for NSArray. (Since it cannot identify that they're inherited from NSObject declared at objcbase.pas). @Jonas, would it be reasonable to remove NSObject declaration from there?! For Lazarus version I can easily clean this up by: 1. Removing NSObject declaration from AnonClassDefinitionQuartzcore.pas completely.  2. in Lazarus Tools->Rescan FPC Source Directory. After that code completion on NSArray shows all NSObject methods. 2) It might be that CodeTools are missing the feature of "id" type to have all declared objc methods available. At least it looks so. If you'd use type casting however, it would work: Code: [Select]NSMutableArray(NSMustableArray.alloc).|  <-- completion works