Discuss Swift.

Swift Documentation

Post

Replies

Boosts

Views

Activity

Clearing Swift App Data
Swift Newbie here... I've been coding for a very long time, but never on Apple platform until now. I'm walking my way through tutorials and examples, and trying this and that. Today I'm trying to learn how to clear extraneous SwiftData schemas from the persistent data store on my MacOS platform. I created an App named 'Xyz' that's just the sample App that is created when one chooses File->New->Project..., built it and ran it. It supports creation of a persistent list of items, and after learning what I could from the code I deleted the project folder and moved on to the next 'lesson'. Now I find that the items that were created when I ran the Xyz App are still out there, somewhere. I need to remove them, but I cannot seem to find any info on where they are stored or how to remove them. In my experience, discarding extraneous application data is a fundamental task normally performed over and over during early stages of application development. I understand the Migration Path concept, but that seems way too complex for Apps that are nothing more than learning environments... Help?
2
0
635
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) -> 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
510
Jan ’24
Type mismatch in SessionGetInfo() swift
The Apple documentation for SessionGetInfo for swift mentions that this API takes third argument of type UnsafeMutablePointer<SessionAttributeBits>? but I m getting the below error when I pass an argument of this type. Cannot convert value of type 'UnsafeMutablePointer<SessionAttributeBits>' to expected argument type 'UnsafeMutablePointer<UInt32>' Why is it expecting a different type. The documentation states otherwise. How to resolve this? Is this a Bug? public static func GetSessionInfo () -> Void { var sessionID = SecuritySessionId() var sessionAttrs = SessionAttributeBits() let status = SessionGetInfo(callerSecuritySession, &sessionID, &sessionAttrs) //error:Cannot convert value of type 'UnsafeMutablePointer<SessionAttributeBits>' to expected argument type 'UnsafeMutablePointer<UInt32>' if status != errSessionSuccess { print("Could not get session info. Error \(status)") } }
6
0
785
Jan ’24
NWConnectionGroup stateUpdateHandler not called for failed states
I'm building a network client with Swift (using QUIC). I set everything up properly (I know this because I can successfully connect, send and receive streams). But I'm trying to catch connection errors. For example if I try to connect to a totally bogus IP address, I would like to display Connecting, then ConnectionFailed I do the following: create my NWMultiplexGroup descriptor set my appropriate NWParameters create my NWConnectionGroup set up my handlers (setReceiveHandler, newConnectionHandler) and my state update handler i call connection.start When I pass a valid address to a server that is listening for the connection, all is good - in my stateUpdateHandler I get the .ready state, but I don't get any intermediate states, and if I pass it a bogus IP address, I get absolutely no callbacks to my handler (I would have expected to get .waiting and/or .failed) I couldn't find any quic options that I'm not doing, and the apple documentation is not helpful Any suggestions as to what I might be missing?
0
0
647
Jan ’24
How to codesign CLI tool so that I can read CNContact.note field?
I want to build a CLI tool (using SwiftPM - without XCode) to read the contacts on my mac. The end goal is to use the notes field or maybe custom fields to build a simple CRM (customer relationship tool) to keep track of some things. It especially means reading the NOTE field, and also writing it back. But... as mentioned on com.apple.developer.contacts.notes | Apple Developer Documentation reading the note field requires the com.apple.developer.contacts.notes. How do I do that? If it runs locally only on my machine I am happy. I wrote an entitlements.plist: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>com.apple.developer.contacts.notes</key> <true/> </dict> </plist> And do # build swift build --configuration release --disable-sandbox --arch arm64 Building for production... [2/2] Linking contacts Build complete! (0.29s) #sign codesign --sign - --entitlements entitlements.plist --deep .build/release/contacts --force .build/release/contacts: replacing existing signature But upon running, I get: ./.build/release/contacts fish: Job 1, './.build/release/contacts' terminated by signal SIGKILL (Forced quit) Without signing I get: *** Terminating app due to uncaught exception 'CNPropertyNotFetchedException', reason: 'A property was not requested when contact was fetched.' *** First throw call stack: ( 0 CoreFoundation 0x000000018b1cc570 __exceptionPreprocess + 176 1 libobjc.A.dylib 0x000000018acbdeb4 objc_exception_throw + 60 2 CoreFoundation 0x000000018b1cc460 +[NSException exceptionWithName:reason:userInfo:] + 0 3 Contacts 0x000000019f8f9b74 -[CNContact note] + 152 4 contacts 0x0000000104879e04 $s8contacts3CliV3runyyKF + 436 5 contacts 0x000000010487a0c8 $s8contacts3CliV14ArgumentParser15ParsableCommandAadEP3runyyKFTW + 12 6 contacts 0x000000010487a160 contacts_main + 96 7 dyld 0x000000018acf90e0 start + 2360 ) libc++abi: terminating due to uncaught exception of type NSException fish: Job 1, './.build/release/contacts' terminated by signal SIGABRT (Abort) I am new to Swift and SwiftPM and the world of code signing. I currently am NOT a member of the Apple Developer program but if needed I am (reluctantly) willing to pay 99$ to be able to sign/notarize/.. but since the goal is to only run it for myself I hppe there is a way to self-sign. Do I need so sign my cli? Is it even possible to codesign command line tools (i've seen comments that it is not)? How would I do that? What am I missing? Cheers, Oliver
2
0
592
Jan ’24
How to run `xctest` bundle - or how to add `entitlement` to test?
I am writing a SPM based project for MacOS. In this project? I need to access MacOS Keychain. I am write a swift test built by SPM testTarget(). I can see it generates a bundle ./.build/x86_64-apple-macosx/debug/MyProjectTests.xctest with an executable: % file ./.build/x86_64-apple-macosx/debug/MyProjectPackageTests.xctest/Contents/MacOS/MyProjectPackageTests ./.build/x86_64-apple-macosx/debug/MyProjectPackageTests.xctest/Contents/MacOS/MyProjectPackageTests: Mach-O 64-bit bundle x86_64 This bundle file cannot be executed. How can I execute its tests? I tried with xcodebuild test-without-building -xctestrun ./.build/x86_64-apple-macosx/debug/MyProjectPackageTests.xctest -destination 'platform=macOS' without any chance. Obviously the next question is can I 'simply' add entitlement to this bundle with codesign to fix my enttilement error. My error when running the test is A required entitlement isn't present.
1
0
913
Jan ’24
Adding a "favourite" button that adds to a "favourite" section
HI, I don't know much about coding or Xcode, however I have managed to figure out (from various YouTube videos) how to make an IOS app that will randomly show me different quotes I like, as well as a search function. What I would ideally like to do is to create a button that allows me to save certain quotes in a separate tab. However, I have come to a dead end following YouTube so I wouldn't if anybody would be able to help me here. Thanks so much. Peabo
0
0
237
Jan ’24
PusherSwift Can't establish a websocket connection
I'm trying to connect to a socket from my own application, when I call pusher.connect() the following is displayed in the console: 2024-01-23 13:05:30.963189+0500 pusher_test[64299:2525981] [] nw_protocol_copy_ws_definition_block_invoke [C1:1] nw_ws_validate_server_response 2024-01-23 13:05:30.964076+0500 pusher_test[64299:2525981] [connection] nw_read_request_report [C1] Receive failed with error "Software caused connection abort" Below is my code for testing class AuthRequestBuilder: AuthRequestBuilderProtocol { func requestFor(socketID: String, channelName: String) -&gt; URLRequest? { var request = URLRequest(url: URL(string: authPath)!) request.httpMethod = "POST" request.addValue("Bearer "+token, forHTTPHeaderField: "Authorization") request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept") return request } } let optionsWithEndpoint = PusherClientOptions( authMethod: AuthMethod.authRequestBuilder(authRequestBuilder: AuthRequestBuilder()), host:.host(host), port:6001, useTLS: false ) pusher = Pusher(key: "pusher_app_key", options: optionsWithEndpoint) pusher.connect() let myPresenceChannel = pusher.subscribeToPresenceChannel(channelName: "presence-online") I write in flutter and am new to swift, but the error occurs in native, that's why I'm here. xcode version: 14.3.1. Swift version: swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100) I tested it on the iOS simulator, but the error persists on a real device
1
0
367
Jan ’24
CoreFoundation Crash (CoreFoundation: __CFRunLoopServiceMachPort + 160)
I've been seeing this crash for a few months now, and have not been able to reproduce this or make sense of the stack trace. I would really appreciate any help deciphering what the stack trace is telling me. I'm seeing mention of GRDB and from what I can tell, it looks like this is happening right as the app launches? The app does do some writing to GRDB on app launch, if that is of any relevance. Please let me know what would be a good approach to making sense of this stack trace and investigating this crash. Thank you so much in advance. Stack trace: Triggered by Thread: 0 Thread 0 name: Thread 0 Crashed: 0 libsystem_kernel.dylib 0x00000001d7159178 mach_msg2_trap + 8 (:-1) 1 libsystem_kernel.dylib 0x00000001d7158f10 mach_msg2_internal + 80 (mach_msg.c:201) 2 libsystem_kernel.dylib 0x00000001d7158e28 mach_msg_overwrite + 436 (mach_msg.c:0) 3 libsystem_kernel.dylib 0x00000001d7158c68 mach_msg + 24 (mach_msg.c:323) 4 CoreFoundation 0x000000018fa2fb1c __CFRunLoopServiceMachPort + 160 (CFRunLoop.c:2624) 5 CoreFoundation 0x000000018fa2da14 __CFRunLoopRun + 1208 (CFRunLoop.c:3007) 6 CoreFoundation 0x000000018fa2d478 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420) 7 GraphicsServices 0x00000001d2fae4f8 GSEventRunModal + 164 (GSEvent.c:2196) 8 UIKitCore 0x0000000191e5162c -[UIApplication _run] + 888 (UIApplication.m:3685) 9 UIKitCore 0x0000000191e50c68 UIApplicationMain + 340 (UIApplication.m:5270) 10 Ruby 0x0000000104f476d0 main + 64 (AppDelegate.swift:28) 11 dyld 0x00000001b2766dcc start + 2240 (dyldMain.cpp:1269) Thread 1 name: Thread 1: 0 libsystem_kernel.dylib 0x00000001d7166e5c guarded_pwrite_np + 8 (:-1) 1 libsqlite3.dylib 0x00000001b8e5a3e4 seekAndWrite + 456 (sqlite3.c:44224) … 40 libdispatch.dylib 0x00000001979f1300 _dispatch_client_callout + 20 (object.m:561) 41 libdispatch.dylib 0x0000000197a02dbc _dispatch_root_queue_drain + 864 (queue.c:7051) 42 libdispatch.dylib 0x0000000197a033ec _dispatch_worker_thread2 + 156 (queue.c:7119) 43 libsystem_pthread.dylib 0x00000001f9a1b928 _pthread_wqthread + 228 (pthread.c:2642) 44 libsystem_pthread.dylib 0x00000001f9a1ba04 start_wqthread + 8 (:-1) Thread 2: 0 libsystem_pthread.dylib 0x00000001f9a1b9fc start_wqthread + 0 (:-1) Thread 3: 0 libsystem_pthread.dylib 0x00000001f9a1b9fc start_wqthread + 0 (:-1) Thread 4 name: Thread 4: 0 libsystem_kernel.dylib 0x00000001d7159178 mach_msg2_trap + 8 (:-1) 1 libsystem_kernel.dylib 0x00000001d7158f10 mach_msg2_internal + 80 (mach_msg.c:201) 2 libsystem_kernel.dylib 0x00000001d7158e28 mach_msg_overwrite + 436 (mach_msg.c:0) 3 libsystem_kernel.dylib 0x00000001d7158c68 mach_msg + 24 (mach_msg.c:323) 4 CoreFoundation 0x000000018fa2fb1c __CFRunLoopServiceMachPort + 160 (CFRunLoop.c:2624) 5 CoreFoundation 0x000000018fa2da14 __CFRunLoopRun + 1208 (CFRunLoop.c:3007) 6 CoreFoundation 0x000000018fa2d478 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420) 7 Foundation 0x000000018e9c048c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212 (NSRunLoop.m:373) 8 Foundation 0x000000018e9ed74c -[NSRunLoop(NSRunLoop) runUntilDate:] + 64 (NSRunLoop.m:420) 9 UIKitCore 0x0000000191db34a8 -[UIEventFetcher threadMain] + 420 (UIEventFetcher.m:1208) 10 Foundation 0x000000018ea43de0 __NSThread__start__ + 732 (NSThread.m:991) 11 libsystem_pthread.dylib 0x00000001f9a1c4d4 _pthread_start + 136 (pthread.c:904) 12 libsystem_pthread.dylib 0x00000001f9a1ba10 thread_start + 8 (:-1) Thread 5 name: Thread 5: 0 libsystem_kernel.dylib 0x00000001d7159178 mach_msg2_trap + 8 (:-1) 1 libsystem_kernel.dylib 0x00000001d7158f10 mach_msg2_internal + 80 (mach_msg.c:201) 2 libsystem_kernel.dylib 0x00000001d7158e28 mach_msg_overwrite + 436 (mach_msg.c:0) 3 libsystem_kernel.dylib 0x00000001d7158c68 mach_msg + 24 (mach_msg.c:323) 4 CoreFoundation 0x000000018fa2fb1c __CFRunLoopServiceMachPort + 160 (CFRunLoop.c:2624) 5 CoreFoundation 0x000000018fa2da14 __CFRunLoopRun + 1208 (CFRunLoop.c:3007) 6 CoreFoundation 0x000000018fa2d478 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420) 7 CFNetwork 0x0000000190d2b060 +[__CFN_CoreSchedulingSetRunnable _run:] + 384 (CoreSchedulingSet.mm:1479) 8 Foundation 0x000000018ea43de0 __NSThread__start__ + 732 (NSThread.m:991) 9 libsystem_pthread.dylib 0x00000001f9a1c4d4 _pthread_start + 136 (pthread.c:904) 10 libsystem_pthread.dylib 0x00000001f9a1ba10 thread_start + 8 (:-1) Thread 6: 0 libsystem_pthread.dylib 0x00000001f9a1b9fc start_wqthread + 0 (:-1) Thread 7 name: Thread 7: 0 libsystem_kernel.dylib 0x00000001d7159178 mach_msg2_trap + 8 (:-1) 1 libsystem_kernel.dylib 0x00000001d7158f10 mach_msg2_internal + 80 (mach_msg.c:201) 2 libsystem_kernel.dylib 0x00000001d7158e28 mach_msg_overwrite + 436 (mach_msg.c:0) 3 libsystem_kernel.dylib 0x00000001d7158c68 mach_msg + 24 (mach_msg.c:323) 4 FirebaseCrashlytics 0x00000001065bf1f4 FIRCLSMachExceptionReadMessage + 56 (FIRCLSMachException.c:192) 5 FirebaseCrashlytics 0x00000001065bf1f4 FIRCLSMachExceptionServer + 104 (FIRCLSMachException.c:168) 6 libsystem_pthread.dylib 0x00000001f9a1c4d4 _pthread_start + 136 (pthread.c:904) 7 libsystem_pthread.dylib 0x00000001f9a1ba10 thread_start + 8 (:-1) Thread 8: 0 libsystem_pthread.dylib 0x00000001f9a1b9fc start_wqthread + 0 (:-1) Thread 0 crashed with ARM Thread State (64-bit): x0: 0x0000000010004005 x1: 0x0000000507000806 x2: 0x0000000200000000 x3: 0x0000250300000000 x4: 0x0000000000000000 x5: 0x0000250300000000 x6: 0x0000000000000002 x7: 0x00000000ffffffff x8: 0xfffffffffffffbbf x9: 0x0000000000002503 x10: 0x0000000000000000 x11: 0x0000000000000001 x12: 0x0000000000002503 x13: 0x0000000000050fc8 x14: 0x0000000000000000 x15: 0x0000000000000000 x16: 0xffffffffffffffd1 x17: 0x0000000197a390e4 x18: 0x0000000000000000 x19: 0x00000000ffffffff x20: 0x0000000000000002 x21: 0x0000250300000000 x22: 0x0000000000000000 x23: 0x0000250300000000 x24: 0x000000016b00e7b8 x25: 0x0000000200000000 x26: 0x0000000507000806 x27: 0x0000000507000806 x28: 0x0000000107000806 fp: 0x000000016b00e720 lr: 0x00000001d7158f10 sp: 0x000000016b00e6d0 pc: 0x00000001d7159178 cpsr: 0x1000 esr: 0x56000080 Address size fault
3
2
2.5k
Jan ’24
[APNS][RemoteNotifications] didReceiveRemoteNotification UIBackgroundFetchResult method doesn't being called, when App is in background state, iOS swift push notification.
Hello Friends, Could you please help me, whats wrong. We have requirement that, when I receive remote notifications on device, I need to save it in to db (without tapping or opening notification banner), regardless my app is in Foreground or in Background. I receive remote notifications on device & simulator both. And UNUserNotificationCenter delegates are being called if app is in foreground, but didReceiveRemoteNotification is not calling when app is background, not killed. func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // Save to DB completionHandler(.newData) } I am having Xcode 15.2 & I tried on iOS versions 16.x & 17.x both on Simulator and on real devices. // Added this code in App delegate UNUserNotificationCenter.current().delegate = self UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in if granted { // User granted permission DispatchQueue.main.async { UIApplication.shared.registerForRemoteNotifications() } } else { // User denied permission } }
2
0
1.2k
Jan ’24
URLs in iOS 17's New Speech Recognition API: prepareCustomLanguageModel vs Configuration URL
I'm working with the new speech recognition APIs in iOS 17 and have encountered some confusion regarding the use of URLs in SFSpeechLanguageModel.prepareCustomLanguageModel and the SFSpeechLanguageModel.Configuration. In the SFSpeechLanguageModel.Configuration initializer, I provide a URL that points to a custom language model .bin file. However, there's also a URL parameter in the prepareCustomLanguageModel method. I'm unclear about the purpose of this second URL and how it differs from the one in the configuration. To add to the confusion, the documentation for these new APIs is not fully fleshed out at this point. I've tried injecting both .bin files (for the custom language model and the one for prepareCustomLanguageModel) into the same URL, but the results haven't clarified their distinct roles. In experiments I conducted, I checked the confidence level of recognized phrases from the same audio file with and without the custom language model .bin file. Surprisingly, the confidence levels remained the same in both scenarios, leading me to question if the custom model is being utilized correctly. Has anyone else worked with these new APIs and can provide clarity on: The distinct roles of the URLs in SFSpeechLanguageModel.Configuration and prepareCustomLanguageModel. Why there might be no noticeable difference in confidence levels when using a custom language model. Any insights or experiences with these new aspects of the iOS 17 speech recognition API would be greatly appreciated.
0
1
742
Jan ’24
Xcode 15.2 - ObjC Swift Bridging broken?
I have an older project with a mixed ObjC + Swift codebase. I'm in the process of converting everything to Swift in the hopes that Xcode will work better again when the Codebase is not mixed anymore. This is an example of the issues I'm currently facing: The class IVMapViewController is defined in ObjC and the property mapViewModel is declared in the ObjC Header fine. The ObjC Header file is included in the bridging header. I'm trying to access the property mapViewModel in an swift extension on the same class. I can actually compile & run the code just fine, but Xcode Autocompletion does not work, and Xcode generates "Phantom" errors in the Issue Navigator (those errors also stay there when a build completes successful) On the other hand, if I put an actual error in the code, compilation fails, but the actual error will NOT show up in the Issue Navigator. I'll have to dig through the build log to find the actual error. Am I doing anything wrong? Do I need to adjust some project setting?
0
0
477
Jan ’24
Camera usage inside Virtualization?
I'm trying to use the host camera from inside a virtual machine created using the Virtualization framework in Swift. I can't seem to figure out how to achieve this though, and unlike audio devices, keyboards, displays etc there doesn't seem to be a corresponding class and docs page for cameras or generic usb devices. Is there any way to connect a built in Apple camera to a mac virtual machine created with the Virtualization framework?
1
1
639
Jan ’24
Error -16831/START-TIME is too close to live
Hello everyone, I was playing a livestream when I received the error -16831/START-TIME is too close to live returned from the AVPlayerItemNewErrorLogEntry function. I don't know why the error is returned, please help me explain. Similarly, I am also getting error: -12888/Playlist File unchanged for longer than 1.5 * target duration , I also read error -12888 in the documentation page 170: https://docs.huihoo.com/apple/wwdc/ 2018/502_measuring_and_optimizing_hls_performance.pdf but still don't understand the reason. Hope you can help!
1
1
659
Jan ’24