Recent

Author Topic: Error: This construct requires the \{\$modeswitch objectivec1\} mode switch  (Read 1272 times)

Wilko500

  • Full Member
  • ***
  • Posts: 180
[Solved]
The Function GetSignificantDir (In CocoaAll) was working without error when it was located in my main form.  I created a new unit and moved it with other stuff into this new unit as part of a code restructure and then on compiling got the error.

Easily fixed by adding the mode switch to my unit but can someone explain the error to me.  I don't understand what it means by this construct

Code: Pascal  [Select][+][-]
  1. if(count < paths.count) then

errored with a message u_pvupload.pas(159,19) Error: This construct requires the \{\$modeswitch objectivec1\} mode switch to be active

Code: Pascal  [Select][+][-]
  1. function GetSignificantDir(DirLocation: qword; DomainMask: qword; count: byte): string;
  2. var
  3.   paths : NSArray;
  4. begin
  5.  
  6.   paths := NSSearchPathForDirectoriesInDomains(DirLocation, DomainMask, True);
  7.   if(count < paths.count) then
  8.     Result := NSString(paths.objectAtIndex(Count)).UTF8String
  9.   else
  10.     Result := '';
  11. end;

FPC 3.2.3/Lazarus 3.3 on  macOS Monterey 12.7.4
« Last Edit: May 05, 2024, 01:23:33 pm by Wilko500 »
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

jamie

  • Hero Member
  • *****
  • Posts: 7701
You need at the top, after the unit name the mode for the compiler to be in.

For that type of code, it has to be a mode that supports objects..


All units require the compiler operating mode at the top of the file when getting into classes,objects etc.

so pick a mode.

in your case, it looks like  you are using objective type, not sure if you really need that.


The only true wisdom is knowing you know nothing

Wilko500

  • Full Member
  • ***
  • Posts: 180
I created my unit with Lazarus File - New Unit and thus at the top of the unit I have the default
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode ObjFPC}{$H+}  

But the code snippet errors until I replace the default with
Code: Pascal  [Select][+][-]
  1. unit u_PvUpload;
  2.  
  3. //{$mode ObjFPC}{$H+}
  4. {$modeswitch objectivec1}

The code for GetSignificantDir was taken from an Apple web page (don't have a link to hand). While solving the problem in hand I did not try to understand the code, just use it. I had not realised that NSArray was an ObjectiveC construct, hence the error.

I am porting a Win VB6 program to MacOS and need to get Mac folders for /Library/Application Support, /Library Preferences for application managed data so I do not want the /User/Library locations.  I was unable to find any other method to get them.

In hindsight my question(s) should have been
1. Will using {$modeswitch objectivec1} cause me any problems later on as development continues?
2. Should I create a special unit for this function and procedures that depend on it so I can revert to the default {$mode ObjFPC}{$H+} for  all the other code?
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

PascalDragon

  • Hero Member
  • *****
  • Posts: 6393
  • Compiler Developer
In hindsight my question(s) should have been
1. Will using {$modeswitch objectivec1} cause me any problems later on as development continues?

You shouldn't use the modeswitch-directive alone, because the default mode of FPC is the non-object oriented, TP-like FPC mode. Though Lazarus passes -Mobjfpc to the compiler, but if you should ever compile the unit without Lazarus you might experience other issues...

So better use this:

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. {$modeswitch objectivec1}

2. Should I create a special unit for this function and procedures that depend on it so I can revert to the default {$mode ObjFPC}{$H+} for  all the other code?

You can of course create a separate unit, but in essence the modeswitch should enable only additional functionality required by code interfacing with Objective C.

Wilko500

  • Full Member
  • ***
  • Posts: 180
Thank you for clarification.  I will leave the code in the same unit but with both $mode and $modeswitch directives.
I will mark this as solved.
MacBook Pro mid 2015 with OS Monterey 12.7.6
FPC 3.2.3 Lazarus 3.7
FPC 3.2.2 Lazarus 3.4

 

TinyPortal © 2005-2018