Post

Replies

Boosts

Views

Activity

Passing Cpp object to swift
I have a macOS app project implementing the direct Cpp-swift interop mechanism. The project some cpp classes and few Swift class. I wanted to pass a cpp object to a swift function as parameter, so I can use it in my swift code. But the documentation is not very clear on how to do this. Can some please share a code snippet explaining how can I achieve this? I have tried the below code, but it is giving linker error. Swift code: import StClass_modulemap public class SwiftClass { .. public static func getCppObj (pObj: inout StClass) -> Void { ... } } Cpp code: #include "Student.hpp" #include "StClass.hpp" #include <InteropLib-Swift.h> using namespace InteropLib; void Student::Introduce() { //creating cpp to be passed to swift // StClass StObj(50); // Teacher::getCppObj(StObj); std::cout<< "header included in hpp " <<std::endl; } calling Student::Introduce is giving a linker error for StClass
0
0
358
Feb ’24
Passing Swift String type to a Cpp function in the new interop
I have implemented the new cpp-swift interop mechanism using modulemap file. I have a use case to pass a swift string to the cpp function. I observed that the below code works and I am able to pass a swift String type directly to a cpp function which received it as const char *. This works only if its received as const char * in cpp(and not char *). However, this is not an interop documented behaviour for interoperating String types and wanted to know whether this is safe to use. If not, can someone suggest an alternative approach to pass a swift string to Cpp. // Swift code public static func StringToCharPointer () -&gt; Void { // calling cpp function and passing a swift String type as argument, which is received as const char * Student.Convert (sUItextdata) //sUItextdata is of type 'String' } //static Cpp function void Student::Convert (const char * pStr) { std::string s(pStr); std::cout &lt;&lt; "char * converted from swift String : " &lt;&lt; s &lt;&lt; std::endl; } Note : I am aware that there is a way to pass it to cpp and receive it as std:string, but I do not wish to use that.
1
0
711
Feb ’24
Swift extension method not accessible in Cpp using interop
I have a project implementing new interop mechanism and I m able to successfully invoke swift class method directly from swift. However, when I try to invoke the swift extension method it produces an error : No member named 'GetExtData' in 'InteropExtension::SwiftCode' Below is my cpp code: #include "CppSource.hpp" #include "InteropExtension-Swift.h" void CppSource::CppToSwiftCall() { // successfull call InteropExtension::SwiftCode::GetClassData(); //Below call is causing error. Why is swift extension method not invoked? InteropExtension::SwiftCode::GetExtData(); } Below is my swift code: import Foundation public class SwiftCode { public static func GetClassData () -&gt; String { return "classMethod" } } extension SwiftCode { public static func GetExtData () -&gt; String { return "ExtMethod" } } Can someone helpout on why is the extension methods not being invoked, and how can I invoke it?
1
2
595
Feb ’24
Can macOS dynamically change QoS value for pthreads
I have observed that in my application, Even If I set the QoS value of all the thread created to USER_INTERACTIVE, the OS is producing a warning "Thread running at User-Interactive QoS class waiting on a lower QoS thread running at Default QoS class. Investigate ways to avoid priority inversions". I am aware that it is not right to set all threads as USER_INTERACTIVE QoS, but If we assume this case for once, then it means that the OS can dynamically change the QoS of the threads even if we are explicitly setting it. Is this the correct understanding? Also, the main thread has QoS value USER_INTERACTIVE, then is it the case that its child threads will inherit the QoS value from the parent thread, If we are not setting any QoS for a pthread?
1
2
959
Jan ’24
QOS Class not getting set for pthread in macOS
I am building a macOS project where I m creating a thread using pthread in cpp. I am setting the QOS class using the pthread_attr_set_qos_class_np and later checking the value of the QOS class using the pthread_attr_get_qos_class_np. These calls are successful but the QOS class is not being set. Below is the cpp function that I m invoking from my swift code. bool CppThread::CreateThread () { pthread_t threadId; pthread_attr_t attr; qos_class_t qos_class; int relative_priority; int rc = pthread_attr_init(&attr); if (rc) { printf ("pthread_attr_init failed"); } if (pthread_attr_set_qos_class_np (&attr, QOS_CLASS_BACKGROUND, 5)) { printf ("pthread_attr_set_qos_class_np failed\n"); } int retval = pthread_create(&threadId, &attr, threadFunction, NULL); if (retval) { std::cerr << "Error creating thread" << std::endl; return false; } if (pthread_attr_get_qos_class_np (&attr, &qos_class, &relative_priority)) { printf ("pthread_attr_get_qos_class_np failed\n"); } printf ("QoS class: %d, Relative priority: %d\n", qos_class, relative_priority); pthread_join(threadId, NULL); // Wait for thread to finish std::cout << "Thread finished" << std::endl; return true; } OUTPUT: QoS class: 0, Relative priority: 0 Thread finished I have tried setting multiple QOS values but the output always shows the QOS class value as 0. Why is it not getting set?
0
0
398
Jan ’24
Type mismatch in SessionGetInfo() swift
The Apple documentation for SessionGetInfo for swift mentions that this API takes third argument of type UnsafeMutablePointer&lt;SessionAttributeBits&gt;? but I m getting the below error when I pass an argument of this type. Cannot convert value of type 'UnsafeMutablePointer&lt;SessionAttributeBits&gt;' to expected argument type 'UnsafeMutablePointer&lt;UInt32&gt;' Why is it expecting a different type. The documentation states otherwise. How to resolve this? Is this a Bug? public static func GetSessionInfo () -&gt; Void { var sessionID = SecuritySessionId() var sessionAttrs = SessionAttributeBits() let status = SessionGetInfo(callerSecuritySession, &amp;sessionID, &amp;sessionAttrs) //error:Cannot convert value of type 'UnsafeMutablePointer&lt;SessionAttributeBits&gt;' to expected argument type 'UnsafeMutablePointer&lt;UInt32&gt;' if status != errSessionSuccess { print("Could not get session info. Error \(status)") } }
6
0
892
Jan ’24
Converting 'String' Type in swift to 'const char *' in cpp
I have the below code where my swift code calls a cpp function. In this the swift calls a cpp function and passes a swift 'String' as argument to it, which is then received in the cpp code as 'const char *'. This conversion works without any errors. Is this a documented behaviour in cpp-swift interop and is this safe to use? Swift side: internal static func CharCheck (pSentence: String) -&gt; Void { TWSoundLogic.CharacterSoundCount (pSentence) } In the C++ side, void TWSoundLogic::CharacterSoundCount (const char * pSentence) { .. } However, on returning a String type from a swift function and trying to receive it as a const char* in c++, it gives an error in cpp No viable conversion from 'swift::String' to 'const char *' . Why does it not allow to return, but it allows to pass as argument? Can someone explain this behaviour?
0
1
571
Jan ’24
Cpp Compiler flags not accessible in the swift for Cpp-Swift Interop
I have a project that has Cpp code and swift code and I m making some calls from swift to cpp. For this I m using the Cpp-swift interop mechanism introduced in swift 5.9 for making direct calls between swift and cpp. I m using module.modulemap file to expose the cpp code to swift. I am facnig an error when I try to access a cpp header that are using the cpp compiler flags in some static assert statements. These methods are working fine in cpp however, when I try to access these methods from swift using the modulemap file, the compiler flags are not being identified by swift, and produces the below error error: use of undeclared identifier 'TW_KERNEL_WINDOWS'. This is my module.modulemap file: module CoreModule { header "ProcessStates.hpp" //cpp header that contains compiler flags export * } below is the cpp header code that I m trying to access in swift: #pragma once // if the given condition is false – treat as an error #define STATIC_CHECKFALSE(condition, message) static_assert (condition, message) STATIC_CHECKFALSE ((TW_KERNEL_WINDOWS == 0) || (TW_KERNEL_WINDOWS == 1), "TW_KERNEL_WINDOWS can only be 0 or 1"); Can someone help, why is this happening and how to resolve this?
5
0
853
Jan ’24
Delayed response from Apple DTS
I had raised a support request in Apple DTS for an issue related to notification. I had a few iteration with the support team related to this but the problem remains unresolved. Everytime I send a response for what has been asked, it takes more than a week for the team to reply. This issue has been pending for over a month now and my work is blocked due to this. Can someone help me in getting a quick response or tell me how to reach the relevant people who can help me with this? Here is my Case ID : 5269018
1
2
478
Jan ’24
Detecting sleep/wake event in IOS
In IOS, when the device is kept idle for some time, the screen turns off and the device enters sleep mode. It also enters sleep mode when we press the power button to turn the screen off. In my application, I wanted to detect If the device has entered/exited the sleep mode. I have followed the below links but some of these ways like observing 'Darwin notifications' is no longer allowed by apple. Other ways consider the device being locked as sleep mode, which is not precisely correct. Is there a way to correctly determine this? Please share the apple documentation links if this is possible. https://stackoverflow.com/questions/14191980/detect-screen-on-off-from-ios-service/14208787#14208787 https://nemecek.be/blog/104/checking-if-device-is-locked-or-sleeping-in-ios
2
1
1.1k
Jan ’24
where is link received when application is launched using universal link
I have a mac application that is configured to have a universal link. Clicking the universal link launches my application. The documentation says that the link is received in the application(_:continue:restorationHandler:) delegate method which is invoked very late in the application lifecycle after applicationDidBecomeActive. I have the initialisation code of my app in applicationWillFinishLaunching method. Is there any way the Universal link can be received early, so that I can show the appropriate application window based on the universal link parameters? Is there any other delegate that receives the universal link?
0
0
458
Jan ’24
'LaunchIsDefaultLaunchKey' key in the Notification parameter in applicationDidFinishLaunching(_ notification: Notification)
The notification parameter in DidFinishLaunching contains a key LaunchIsDefaultLaunchKey which according to Apple documentation is: The value for this key is an NSNumber containing a Boolean value. The value is false if the app was launched to open or print a file, to perform a Service action, if the app had saved state that will be restored, or if the app launch was in some other sense not a default launch. Otherwise its value will be true. I m not able to understand the exact usecase for this parameter, like where can we make use of this parameter. Can I use this key to identify If my application was launched using a universal link?
0
0
497
Dec ’23
Universal link not working for multiple apps
I have created a universal link which I m using for two different applications in macOS. Below is the apple-app-site-association file for the same: { "applinks":{ "apps":[], "details":[ { "appID":"E5R4JF6D5K.com.demo.App1", "paths":[ "/app1/*" ] }, { "appID":"E5R4JF6D5K.world.demo.App2", "paths":[ "/app2/*" ] } ] } } After creating this file in my server and providing the same Url in my associated domain capability for both my application, when I try to launch my applications using the link then only the first application is getting launched everytime. The second application is never launched. for Url with http://custom.com/app1/... it redirects to first app. for Url with http://custom.com/app2/... it redirects to browser. I tried uninstalling the first app, but then it always directs in browser. I tried a separate url for both apps, and it works fine. I m not able to figure out the problem. The apple documentation says that it is possible to have two application linked to a common domain. Any help?
2
0
1.2k
Dec ’23