In GNUstep base this method is implemented in this way:

+ (NSCharacterSet*) characterSetWithCharactersInString: (NSString*)aString
{
  NSMutableCharacterSet *ms;
  NSCharacterSet        *cs;

  ms = [NSMutableCharacterSet new];
  [ms addCharactersInString: aString];
  cs = [ms copy];
  RELEASE(ms);
  return AUTORELEASE(cs);
}

And copy will result in a non mutable character set. But from this method you 
also see what you should have used in the first place, just the first two lines 
of this method :-)

Hope this helps,
Fred



> Am 11.07.2025 um 16:35 schrieb David Chisnall <gnus...@theravensnest.org>:
> 
> On 11 Jul 2025, at 14:17, Riccardo Mottola <riccardo.mott...@libero.it> wrote:
>> 
>> Hi,
>> 
>> In Grr there is this code:
>> 
>>    wsAndTagClosing = [NSMutableCharacterSet
>> characterSetWithCharactersInString: @"/>"];
>> 
>> Clang warns me with:
>> 
>> NSString+TolerantHTML.m:109:21: warning: incompatible pointer types
>> assigning to 'NSMutableCharacterSet *' from 'NSCharacterSet *'
>> [-Wincompatible-pointer-types]
>>  109 |     wsAndTagClosing = [NSMutableCharacterSet
>> characterSetWithCharactersInString: @"/>"];
>> 
>> GCC gives a more generic issue about "distinct objective C type".
>> 
>> Now, characterSetWithCharactersInString: is a class method of
>> NSCharacterSet:
>> 
>> https://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSCharacterSet.html#method$NSCharacterSet+characterSetWithCharactersInString$
>> 
>> and NSMutableCharacterSet is a subclass of NSCharacterSet
>> 
>> Shouldn't thus the method called on the subclass still return a valid
>> subclass and not the superclass?
>> 
>> Could it be a header issue? we define it as:
>> + (NSCharacterSet*) characterSetWithCharactersInString: (NSString*)aString;
>> 
>> maybe it should return instancetype?
> 
> Factory methods on the superclass, if called on the subclass, are not 
> required to return an instance of the subclass, they can return a totally 
> unrelated subclass.  For example, a bunch of the NSString and NSDictionary 
> factory methods will return a specialised version for small of arguments.  If 
> you’re doing this kind of trick, you need an explicit check in the method 
> implementation to see if `self` is the expected class.  
> 
> If that check exists, or if this method isn’t returning a subclass instance, 
> it’s safe to turn it into instancetype and this warning will go away.  If 
> not, then this is a real warning and the mutable subclass is not being 
> returned.
> 
> You can also try `NSLog(@“What even is this? %@“, [wsAndTagClosing class]);` 
> to see.
> 
> David



Reply via email to