Creates a library object synchronously by compiling the functions stored in the specified source string.
Required.
SDKs
- iOS 8.0+
- macOS 10.11+
- Mac Catalyst 13.0+
- tvOS 9.0+
Framework
- Metal
Declaration
- (id<MTLLibrary>)newLibraryWithSource:(NSString *)source options:(MTLCompile Options *)options error:(NSError * _Nullable *)error;
Parameters
source
A string containing source code for one or more Metal functions. See Metal Shading Language Guide.
options
A set of options to influence the compilation of this source. See
MTLCompile
.Options error
A pointer to an error object that describes source code compilation problems, if any. This object contains compiler errors if the returned library object is
nil
, and compiler warnings if the library was generated with warnings. If the library was generated successfully, without errors or warnings, this error object isnil
.
Return Value
A new library object that contains the compiled source code or nil
if an error occurred.
Discussion
The source may only import the Metal standard library. There is no search path to find other functions.
Handling Errors in Swift:
In Swift, this method returns a nonoptional result and is marked with the throws
keyword to indicate that it throws an error in cases of failure.
You call this method in a try
expression and handle any errors in the catch
clauses of a do
statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.