Skip to content

NSBundle.GetUrlForResource nullability does not match runtime behavior #24803

@nanasi880

Description

@nanasi880

Apple platform

Mac Catalyst

Framework version

net10.0-*

Affected platform version

.NET 10.0

Description

Summary

Foundation.NSBundle.GetUrlForResource(...) is currently annotated as non-null (NSUrl), but it can return null at runtime when the resource is missing.

This does not match Apple's API contract and makes NRT behavior unsafe for callers.

Affected APIs

  • NSBundle.GetUrlForResource(string name, string fileExtension)
  • NSBundle.GetUrlForResource(string name, string fileExtension, string subdirectory)
  • NSBundle.GetUrlForResource(string name, string fileExtension, string subdirectory, NSUrl bundleURL)
  • NSBundle.GetUrlForResource(string name, string fileExtension, string subdirectory, string localizationName)

Native API evidence (Objective-C headers)

In Xcode SDK headers, the native Objective-C signatures are explicitly nullable:

Foundation.framework/Headers/NSBundle.h

+ (nullable NSURL *)URLForResource:(nullable NSString *)name withExtension:(nullable NSString *)ext subdirectory:(nullable NSString *)subpath inBundleWithURL:(NSURL *)bundleURL;
- (nullable NSURL *)URLForResource:(nullable NSString *)name withExtension:(nullable NSString *)ext;
- (nullable NSURL *)URLForResource:(nullable NSString *)name withExtension:(nullable NSString *)ext subdirectory:(nullable NSString *)subpath;
- (nullable NSURL *)URLForResource:(nullable NSString *)name withExtension:(nullable NSString *)ext subdirectory:(nullable NSString *)subpath localization:(nullable NSString *)localizationName;

Steps to Reproduce

using Foundation;

var url = NSBundle.MainBundle.GetUrlForResource("definitely_missing_file", "txt");

// Compiles as non-null today, but url can be null at runtime.
Console.WriteLine(url == null ? "null" : url.AbsoluteString);

Did you find any workaround?

No response

Build logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-bindingsbugIf an issue is a bug or a pull request a bug fix

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions