### Call .NET APIs from Swift Source: https://github.com/royalapplications/beyondnet/blob/main/README_MANUAL_BUILD.md Example of calling a .NET method (System.DateTime.now.toString) from Swift after setting up bindings. ```swift print((try? System.DateTime.now?.toString()?.string()) ?? "Error") ``` -------------------------------- ### C# Method Overloading Example Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Illustrates C# static methods with the same name but different parameter types, demonstrating the need for C's suffix-based disambiguation. ```csharp public static class OverloadTests { public static void Print(int value) { } public static void Print(DateTime value) { } public static void Print(string value) { } } ``` -------------------------------- ### Generator Configuration File Example Source: https://github.com/royalapplications/beyondnet/blob/main/README.md This JSON object defines the configuration for the generator. It specifies paths for the .NET assembly, build targets, output locations for generated code in C#, C, Swift, and Kotlin, and options for type filtering and build behavior. ```json { "AssemblyPath": "/Path/To/Target/.NET/Assembly.dll", "Build": { "Target": "apple-universal", "ProductName": "AssemblyKit", "ProductBundleIdentifier": "com.mycompany.assemblykit", "ProductOutputPath": "/Path/To/ProductOutput", "MacOSDeploymentTarget": "13.0", "iOSDeploymentTarget": "16.0", "DisableParallelBuild": false, "DisableStripDotNETSymbols": false, "NoWarn": [ "SYSLIB5006" ] }, "CSharpUnmanagedOutputPath": "/Path/To/Generated/CSharpUnmanaged/Output_CS.cs", "COutputPath": "/Path/To/Generated/C/Output_C.h", "SwiftOutputPath": "/Path/To/Generated/Swift/Output_Swift.swift", "KotlinOutputPath": "/Path/To/Generated/Kotlin/Output_Kotlin.kt", "KotlinPackageName": "com.mycompany.mypackagename", "KotlinNativeLibraryName": "NativeAssemblyName", "EmitUnsupported": false, "GenerateTypeCheckedDestroyMethods": false, "EnableGenericsSupport": false, "DoNotGenerateSwiftNestedTypeAliases": false, "DoNotGenerateDocumentation": false, "DoNotDeleteTemporaryDirectories": false, "IncludedTypeNames": [ "IncludedTypeName", "AnotherIncludedTypeName" ], "ExcludedTypeNames": [ "ExcludedTypeName", "AnotherExcludedTypeName" ], "ExcludedAssemblyNames": [ "Assembly1, Version=4.2.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed", "Assembly2" ], "AssemblySearchPaths": [ "/Path/To/Assemblies", "/Another/Path/To/Assemblies" ] } ``` -------------------------------- ### C# Property Declaration Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Example of a C# property declaration. This serves as the source for generated C and Swift accessors. ```csharp public class PropertyTests { public int FavoriteNumber { get; set; } } ``` -------------------------------- ### C# Method Signature Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Example of a C# method signature that will be exposed through C bindings. Note the absence of an explicit exception parameter. ```csharp static void WriteLine(string text) ``` -------------------------------- ### C# generic method example Source: https://github.com/royalapplications/beyondnet/blob/main/README.md A simple C# generic method that returns the default value for a given type `T`. ```csharp T ReturnDefaultValue() { return default(T); } ``` -------------------------------- ### C# method returning `IEnumerable` via `out` parameter Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Example of a C# method that returns an `IEnumerable` type using an `out` parameter. ```csharp void ReturnIEnumerableAsOut(out IEnumerable returnValue) { returnValue = "Abc"; } ``` -------------------------------- ### C Destructor for System.Guid Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Example of a generated C destructor function for a .NET type (System.Guid). This must be called manually to free the object and prevent memory leaks. ```c void System_Guid_Destroy(System_Guid_t self) ``` -------------------------------- ### Configure Beyond.NET for Apple Universal Build Source: https://github.com/royalapplications/beyondnet/blob/main/README.md This JSON configuration file specifies the .NET assembly path and targets the 'apple-universal' build for creating an XCFramework. ```json { "AssemblyPath": "bin/Release/net10.0/publish/BeyondDemo.dll", "Build": { "Target": "apple-universal" } } ``` -------------------------------- ### Swift and .NET String Conversion Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Shows how to convert between a .NET System.String and a Swift String using convenience extensions provided by Beyond.NET. ```swift let systemString = System.String.empty let swiftString = systemString.string() let systemStringRet = swiftString.dotNETString() ``` -------------------------------- ### Integrate and Use .NET Library in Swift Source: https://github.com/royalapplications/beyondnet/blob/main/README.md This Swift code demonstrates how to convert Swift strings to .NET strings, instantiate a .NET class, call its methods, and convert the result back to a Swift string. It includes error handling for .NET interop. ```swift import SwiftUI import BeyondDemoKit struct ContentView: View { var body: some View { VStack { Text("\(greeting(for: "You"))") } } func greeting(for name: String) -> String { do { // Convert the Swift String into a .NET System.String let nameDN = name.dotNETString() // Create an instance of the .NET class "Hello" let hello = try BeyondDemo.Hello(nameDN) // Get a .NET System.String containing the greeting let theGreetingDN = try hello.getGreeting() // Convert the .NET System.String to a Swift String let theGreeting = theGreetingDN.string() // Return the greeting return theGreeting } catch { fatalError("An error occurred: \(error.localizedDescription)") } } } #Preview { ContentView() } ``` -------------------------------- ### Using out parameter placeholder in Swift Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Demonstrates the use of out parameter placeholders in Swift to avoid specifying default values for `out` parameters. ```swift var returnValue = System.Collections.IEnumerable_DNInterface.outParameterPlaceholder try target.returnIEnumerableAsOut(&returnValue) ``` -------------------------------- ### Swift Event Consumption Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Demonstrates how to consume a .NET event in Swift. It includes creating an instance, defining and adding an event handler, triggering the event, and removing the handler. Error handling is omitted for brevity. ```swift // Create an instance of Beyond.NET.Sample.EventTests let eventTest = try! Beyond.NET.Sample.EventTests() // Create a variable that will hold the last value passed in to our event handler var lastValuePassedIntoEventHandler: Int32 = 0 // Create an event handler let eventHandler = Beyond.NET.Sample.EventTests_ValueChangedDelegate { sender, newValue in // Remember the last value passed in here lastValuePassedIntoEventHandler = newValue } // Add the event handler eventTest.valueChanged_add(eventHandler) // Set a new value (our event handler will be called for this one) try! eventTest.value_set(5) // Remove the event handler eventTest.valueChanged_remove(eventHandler) // Set a another new value (our event handler will NOT be called for this one because we already removed the event handler) try! eventTest.value_set(10) // Prints "5" print(lastValuePassedIntoEventHandler) ``` -------------------------------- ### Configure Xcode Library Search Paths Source: https://github.com/royalapplications/beyondnet/blob/main/README_MANUAL_BUILD.md Adjust 'Library Search Paths' in Xcode build settings to help the linker find the native dylib. ```bash $(PROJECT_DIR)/../MyLibNative/bin/Release/net10.0/osx-universal/publish ``` -------------------------------- ### Using `inout` parameter in Swift Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Demonstrates how to call a Swift function that was imported from a C# method with an `out` parameter. ```swift var returnValue: Int32 = 0 try target.returnIntAsOut(&returnValue) // returnValue now contains the value returned by .NET ``` -------------------------------- ### Add Native Library to Xcode Project Source: https://github.com/royalapplications/beyondnet/blob/main/README_MANUAL_BUILD.md Link the compiled native dylib to your Xcode project under 'Frameworks, Libraries and Embedded Content'. ```bash Add other... - Add files... ``` -------------------------------- ### Define a .NET Class for Greetings Source: https://github.com/royalapplications/beyondnet/blob/main/README.md This C# code defines a simple class with a constructor and a method to generate a greeting. It's intended to be compiled into a native library for Apple platforms. ```csharp namespace BeyondDemo; public class Hello { public string Name { get; } public Hello(string name) { Name = name; } public string GetGreeting() { return $"Hello, {Name}!"; } } ``` -------------------------------- ### Configure .NET Project for Native Compilation Source: https://github.com/royalapplications/beyondnet/blob/main/README_MANUAL_BUILD.md Modify your .NET project file to enable Native AOT compilation and specify target runtime identifiers. ```xml net10.0 true true osx-x64;osx-arm64 ``` -------------------------------- ### Swift import of C# `IEnumerable` `out` parameter Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Shows the Swift import signature for a C# method returning `IEnumerable` via an `out` parameter. ```swift func returnIEnumerableAsOut(_ returnValue: inout System.Collections.IEnumerable) throws ``` -------------------------------- ### Generated C Method Overload Suffixes Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Shows how C interfaces handle method overloading by appending numerical suffixes to differentiate methods with identical names and argument counts. ```c void OverloadTests_Print(int32_t value, System_Exception_t* outException); void OverloadTests_Print_1(System_DateTime_t value, System_Exception_t* outException); void OverloadTests_Print_2(System_String_t value, System_Exception_t* outException); ``` -------------------------------- ### Configure Xcode Header Search Paths Source: https://github.com/royalapplications/beyondnet/blob/main/README_MANUAL_BUILD.md Adjust 'Header Search Paths' in Xcode build settings to help the compiler find the generated C header files. ```bash $(PROJECT_DIR)/../Generated ``` -------------------------------- ### Box Swift Int32 to .NET Object Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Converts a Swift Int32 to a .NET System.Object using extension methods. Error handling is omitted. ```swift let number: Int32 = 5 let numberObj = number.dotNETObject() let numberRet = try numberObj.value // Or: try numberObj.castToInt32() ``` -------------------------------- ### Swift import of C# `out` parameter Source: https://github.com/royalapplications/beyondnet/blob/main/README.md This Swift code shows how a C# method with an `out` parameter is imported. ```swift func returnIntAsOut(_ returnValue: inout Int32) throws ``` -------------------------------- ### Swift Type Checking and Casting Extensions Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Demonstrates Swift extensions for type checking (`is`) and safe casting (`castAs`). These mirror C#'s `is` and `as` keywords. ```swift let string = System.String.empty if string.is(System.String.typeOf) { print("Hooray, it's a System.String.") } if !string.is(System.Guid.typeOf) { print("Yes, it's certainly not a Sytem.Guid.") } if let object: System.Object = string.castAs() { print("Hooray, a System.String is also a System.Object so the cast succeeded.") } if let guid: System.Guid = string.castAs() { print("Oh no, a System.String should not be convertible to a System.Guid. This is an error!") } ``` -------------------------------- ### Import C Bindings in Swift Bridging Header Source: https://github.com/royalapplications/beyondnet/blob/main/README_MANUAL_BUILD.md Include the generated C bindings header file in your Objective-C bridging header to make C APIs available in Swift. ```objective-c #import "Output_C.h" ``` -------------------------------- ### Box C int32_t to .NET Object Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Converts a C int32_t to a .NET System.Object. Error handling for the cast back is omitted. ```c int32_t number = 5; System_Object_t numberObj = DNObjectFromInt32(number); int32_t numberRet = DNObjectCastToInt32(numberObj, NULL); // TODO: Error handling ``` -------------------------------- ### Using `IEnumerable` `out` parameter with default value in Swift Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Illustrates calling the imported Swift function with a default value for the `IEnumerable` `out` parameter. ```swift // System.String implements System.Collections.IEnumerable var returnValue: System.Collections.IEnumerable = System.String.empty try target.returnIEnumerableAsOut(&returnValue) // returnValue now contains a .NET string with the following content: "Abc" ``` -------------------------------- ### C Binding for WriteLine with Exception Handling Source: https://github.com/royalapplications/beyondnet/blob/main/README.md The C binding for the WriteLine method includes an out parameter for exceptions. This parameter should be checked after the call to determine if an exception occurred. ```c void WriteLine(System_String_t text, System_Exception_t* exception) ``` -------------------------------- ### Swift Method Overloading Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Swift's approach to method overloading, which directly supports multiple methods with the same name but different parameter types, similar to C#. ```swift class func print(_ value: Int32) throws class func print(_ value: System_DateTime) throws class func print(_ value: System_String) throws ``` -------------------------------- ### Call C# Delegate from Swift Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Calls a .NET delegate from Swift, transforming a string to uppercase. Error handling is omitted for brevity. ```swift // Create an input string and convert it to a .NET System.String let inputString = "Hello World".dotNETString() // Call Beyond.NET.Sample.Transformer.transformString by: // - Providing the input string as the first argument // - Initializing an instance of Beyond_NET_Sample_Transformer_StringTransformerDelegate by passing it a closure that matches the .NET delegate as its sole parameter let outputString = try! Beyond.NET.Sample.Transformer.transformString(inputString, .init({ stringToTransform in // Take the string that should be transformed, call System.String.ToUpper on it and return it return try! stringToTransform.toUpper() })).string() // Convert the returned System.String to a Swift String // Prints "HELLO WORLD!" print(outputString) ``` -------------------------------- ### C# Delegate Declaration for String Transformation Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Defines a delegate and a method in C# that uses this delegate to transform a string. ```csharp public static class Transformer { public delegate string StringTransformerDelegate(string inputString); public static string TransformString( string inputString, StringTransformerDelegate stringTransformer ) { string outputString = stringTransformer(inputString); return outputString; } } ``` -------------------------------- ### Swift Property Declaration and Setter Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Swift representation of a property. The getter is a proper property, but the setter is exposed as a function due to throwing limitations. ```swift var favoriteNumber: Int32 { get throws } func favoriteNumber_set(_ value: Int32) throws ``` -------------------------------- ### Swift Binding for WriteLine with Throws Source: https://github.com/royalapplications/beyondnet/blob/main/README.md The Swift binding for WriteLine is annotated with 'throws', allowing for native Swift error handling. This abstracts away the underlying C exception mechanism. ```swift func writeLine(_ text: System_String) throws ``` -------------------------------- ### C# `out` parameter declaration Source: https://github.com/royalapplications/beyondnet/blob/main/README.md This C# code declares a method with an `out` parameter. ```csharp void ReturnIntAsOut(out int returnValue); ``` -------------------------------- ### Generated C Property Accessors Source: https://github.com/royalapplications/beyondnet/blob/main/README.md The C functions generated to access the C# property. Note the `_Get` and `_Set` suffixes. ```c int32_t PropertyTests_FavoriteNumber_Get(PropertyTests_t self, System_Exception_t* outException); void PropertyTests_FavoriteNumber_Set(PropertyTests_t self, int32_t value, System_Exception_t* outException); ``` -------------------------------- ### C# Event Definition Source: https://github.com/royalapplications/beyondnet/blob/main/README.md Defines a C# class with a delegate and an event that fires when a property's value changes. The new value is passed to the event handler. ```csharp public class EventTests { public delegate void ValueChangedDelegate(object sender, int newValue); public event ValueChangedDelegate? ValueChanged; private int m_value; public int Value { get => m_value; set { m_value = value; ValueChanged?.Invoke(this, value); } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.