Discuss Swift.

Swift Documentation

Post

Replies

Boosts

Views

Activity

When modifying the Image color through "onTapGesture", XCode will crash
Hi I am a beginner and I am preparing to make a Tabbar component for use in my practice APP. The component uses Image as the button. When the button is clicked (onTapGesture), I hope that the color in the IMAGE will change, but now When running to onTapGesture, XCode crashes, who knows how to write this code to make it run normally. Thanks class MainViewManager : ObservableObject{ @Published var tabbarStatus : TabbarStatus = .homeView enum TabbarStatus : CaseIterable { case homeView case articsPage case profile var icon : String{ switch self{ case .homeView: return "house" case .articsPage: return "person.2" case .profile: return "person.crop.circle" } } } } struct MainTabbar: View { @EnvironmentObject var vm : MainViewManager @State var btnColor : Color = .gray var body: some View { HStack{ ForEach(MainViewManager.TabbarStatus.allCases, id: \.self) { tabbarItem in //let selected = tabbarItem == vm.tabbarStatus Image(systemName:tabbarItem.icon) .foregroundStyle(btnColor) .onTapGesture { vm.tabbarStatus = tabbarItem btnColor = Color.accentColor } if tabbarItem != .profile { Spacer() } } } .padding(.horizontal,30) .padding(.all) .frame(maxHeight: .infinity ,alignment: .bottom) .background(Color.white) } } #Preview { MainTabbar() }
2
0
220
Nov ’23
Implementing SECP256R1 Signature Matching with Rust's FastCrypto in Swift
Hello fellow developers, I'm currently working on an SDK involving the SECP256R1 standard and facing an interesting issue. My goal is to ensure the Swift implementation of SECP256R1 signatures matches that of Rust's FastCrypto implementation. The Issue: When running tests to compare signatures generated by Swift and Rust implementations, the signatures do not match. Despite this mismatch, verification tests still succeed. I've tried using both the P256 class from CryptoKit and SecKey from the Security SDK. The Swift code is being written in Xcode 15 Beta 8, Swift 5.9. Code Snippet: struct SECP256R1PrivateKey { /// Commented is P256, uncommented is SecKey // public init(key: Data) throws { // if let privateKey = try? P256.Signing.PrivateKey(rawRepresentation: key) { // self.key = privateKey // } else { // throw AccountError.invalidData // } // } public init(key: Data) throws { if let privateKeyP256 = try? P256.Signing.PrivateKey(rawRepresentation: key) { let attributes: [String: Any] = [ kSecAttrKeyClass as String: kSecAttrKeyClassPrivate, kSecAttrKeyType as String: kSecAttrKeyTypeECDSA, kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave, kSecAttrKeySizeInBits as String: 256 ] var error: Unmanaged<CFError>? guard let privateKey = SecKeyCreateWithData(privateKeyP256.rawRepresentation as CFData, attributes as CFDictionary, &error) else { throw error?.takeRetainedValue() as Error? ?? NSError(domain: NSOSStatusErrorDomain, code: Int(errSecParam), userInfo: nil) } self.key = privateKey } else { throw AccountError.invalidData } } // public func sign(data: Data) throws -> Signature { // let signature = try self.key.signature(for: data) // return Signature( // signature: signature.rawRepresentation, // publickey: try self.publicKey().key.compressedRepresentation, // signatureScheme: .SECP256R1 // ) // } public func sign(data: Data) throws -> Signature { let dataHash = Data(data.sha256) var error: Unmanaged<CFError>? guard let signature = SecKeyCreateSignature(self.key, .ecdsaSignatureMessageX962SHA256, dataHash as NSData, &error) as Data? else { throw error!.takeRetainedValue() as Error } guard let publicKey = SecKeyCopyExternalRepresentation(try self.publicKey().key, &error) as Data? else { throw AccountError.invalidData } return Signature( signature: signature, publickey: publicKey, signatureScheme: .SECP256R1 ) } } func testThatTheRustImplementationForSignaturesIsTheSame() throws { let account = try Account(privateKey: Data(self.validSecp256r1SecretKey), accountType: .secp256r1) guard let signData = "Hello, world!".data(using: .utf8) else { XCTFail("Unable to encode message"); return; } let signature = try account.sign(signData) XCTAssertEqual( try signature.hex(), "26d84720652d8bc4ddd1986434a10b3b7b69f0e35a17c6a5987e6d1cba69652f4384a342487642df5e44592d304bea0ceb0fae2e347fa3cec5ce1a8144cfbbb2" ) } The Core Question: How do I implement the R1 signature in Swift so that it matches the signature generated by Rust's FastCrypto? Any insights, suggestions, or sample code snippets that could guide me in the right direction would be immensely appreciated! Thank you in advance!
4
1
1.3k
Nov ’23
Swift Macros: Adding an enum case with an associated value produces incorrect results
Reproduced in Xcode versions 15.0.1 and 15.1 beta 3. If a Macro adds an enum case with an associated value, other cases are transformed at runtime. In the example below, the case foo is translated into the Macro-generated case bar(false). There is lots of other strange behavior, such as: Case declarations in the same file as the enum are not transformed. For example, in the code above, a function written in SimpleEnum.swift can produce SimpleEnum.foo, whereas functions in other files cannot (the declarations are transformed). Manually-defined cases with associated values are not transformed. For example, a manually-defined case foobar(Bool) can be declared and used normally everywhere. Additional enum cases without associated values get transformed to different cases. For example, if SimpleEnum has manual cases foo, foobar, and foobarfoo, and a Macro-generated bar(Bool), these transformations take place: foo -> bar(false) foobar -> bar(true) foobarfoo -> foo etc. Not all associated value types in the Macro-generated case are treated the same. String associated values seems to work properly. Int yields incrementing transformations (e.g. foo -> bar(0), foobar -> bar(1), foobarfoo -> bar(2)) Radar: FB13417290
3
3
1.2k
Nov ’23
NavigationLink and CustomStyle BackButton
Hi Guys, sorry if I am writing some nonsense, but I am just starting to create apps in Swift. I'm creating an app just for AppleWatch and I don't want to use the default backbutton that appears with navigationLink. How can I change the appearance? I'll put a diagram of my code here: NavigationStack { VStack { NavigationLink(...) { HStack { ... } } .buttonStyle(PlainButtonStyle()) .frame(height: 80) .foregroundColor(.black) .fontWeight(.bold) .tracking(-0.5) } } Everything I find on the Internet is for navigationView which I read is deprecated. I found again on google a site that recommends using .toolbar, but the button does not change. What am I doing wrong? What can I do? Thanks
0
0
159
Nov ’23
How to set the Anchors Points of a PDF in PDFKit?
I am trying to set the top anchor point of a pdf that is inside of a view with the .ignoresSafeArea() modifier. I would also like it to work on the edges when the phone is in landscape although for simplicity I will only explain what I want for the top. I want it to function like the iOS Files app pdf viewer where when tapped it hides the navigation bars but the top of the pdf stays at the same place, but when you zoom in on the pdf it can fill the whole screen. When you zoom back out the top should return to the same place as before. Here is a simple view to show how it is being used: @MainActor struct ContentView: View { @State var showBars: Bool = true @State var pdfUrl: URL? var body: some View { NavigationStack { GeometryReader { geo in ScrollView { TabView { if let url = pdfUrl { PDFViewer(pdfUrl: url) .onTapGesture { withAnimation { showBars.toggle() } } } } .tabViewStyle(.page(indexDisplayMode: .never)) .frame(width: geo.size.width, height: geo.size.height) } .scrollDisabled(true) } .ignoresSafeArea(edges: !showBars ? .all : []) } .task { pdfUrl = renderPDF() } } func renderPDF() -> URL { let renderer = ImageRenderer(content: VStack {}) let url = URL.documentsDirectory.appending(path: "samplepdf.pdf") renderer.render { size, context in guard let pdf = CGContext(url as CFURL, mediaBox: nil, nil) else { return } pdf.beginPDFPage(nil) context(pdf) pdf.endPDFPage() pdf.beginPDFPage(nil) context(pdf) pdf.endPDFPage() pdf.closePDF() } return url } } And here is what my pdfView looks like so far: struct PDFViewer: View { var pdfUrl: URL var body: some View { PDFSheetView(document: .init(url: pdfUrl)) } } class PDFViewController: UIViewController { let document: PDFDocument? var pdfView: PDFView! init(document: PDFDocument?) { self.document = document super.init(nibName: nil, bundle: nil) } override func loadView() { let view = PDFView() self.view = view self.pdfView = view view.document = document view.displayDirection = .vertical view.autoScales = true view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true view.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true view.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true } required init?(coder: NSCoder) { document = nil super.init(coder: coder) return nil } override func viewDidLayoutSubviews() { let bounds = view.bounds if let document { if let page = document.page(at: 0) { let pageBounds = page.bounds(for: .mediaBox) if bounds.width > 0 && pageBounds.width > 0 { let scaleFactor = bounds.width / pageBounds.width let subtractionFactor = scaleFactor * 0.0125 pdfView.minScaleFactor = scaleFactor - subtractionFactor } } } } } struct PDFSheetView: UIViewControllerRepresentable { typealias UIViewControllerType = PDFViewController let document: PDFDocument? func makeUIViewController(context: Context) -> PDFViewController { let controller = PDFViewController(document: document) return controller } func updateUIViewController(_ uiViewController: PDFViewController, context: Context) { } } Is this possible to do? Like I said before, I want it to function just like the iOS Files app pdf viewer.
0
0
442
Nov ’23
Swift Regex crashes when trying to get a simple string
I'm trying to create a simple Regex Builder to parse a timestamp in hh:mm:ss or mm:ss format, a " - " separator, and then a string. I'm trying to use the new Swift RegexBuilder however it doesn't seem to work. It compiles but crashes when running when trying to get the text portion. Example code in a single playground preview file for reference: import SwiftUI import RegexBuilder struct RegexTestView: View { var string: String { let timecode = Regex { Optionally { OneOrMore(.whitespace) } // timecode section Optionally { Capture { OneOrMore(.digit) } transform: { match in Int(match) } ":" } TryCapture { OneOrMore(.digit) } transform: { match in Int(match) } ":" // seconds TryCapture { OneOrMore(.digit) Optionally { // miliseconds "." OneOrMore(.digit) } } transform: { match in TimeInterval(match) } // separator " - " Capture { // WHY CAN'T I GET THE REMAINING TEXT???? Any attempts I make to get this result in a crash. OneOrMore(.any) } transform: { match in String(match) } } let tests = [" 222:45:23.2 - foo","2:34:22 - bar"," 2:08 - baz","2:32.18","2:45:23.2 - what"] var results = "" for test in tests { guard let match = test.wholeMatch(of: timecode) else { results += "MATCH NOT FOUND" continue } results += """ •••• \(match.0) \(String(describing: match.1)) \(String(describing: match.2)) \(String(describing: match.3)) """ /* // Adding this line to the above crashes \(String(describing: match.4))\n */ } return results } var body: some View { Text(string) } } #Preview("Regex") { RegexTestView() }
1
0
633
Nov ’23
Unable to get WiFi rssi value
I want to create a function which will return the rssi value of currently connected WiFi network on my iPhone. I tried fetching the value from status bar but I guess that support is no longer provided in Swift. I have tried the following solution and other solutions with similar approach but it doesn't seem to work now. private func wifiStrength() -> Int? { let app = UIApplication.shared var rssi: Int? guard let statusBar = app.value(forKey: "statusBar") as? UIView, let foregroundView = statusBar.value(forKey: "foregroundView") as? UIView else { return rssi } for view in foregroundView.subviews { if let statusBarDataNetworkItemView = NSClassFromString("UIStatusBarDataNetworkItemView"), view .isKind(of: statusBarDataNetworkItemView) { if let val = view.value(forKey: "wifiStrengthRaw") as? Int { //print("rssi: \(val)") rssi = val break } } } return rssi }
1
0
1.1k
Nov ’23
EKEventStore().requestFullAccessToEvents() doesn't work with iOS 17
We don't need to add -- latest functions for iOS-17 as it introduces requestFullAccessToEvents. if #available(iOS 17.0, *) { eventStore.requestFullAccessToEvents(completion: { (granted: Bool, _: Error?) -> Void in completion(granted) }) } else { // Fallback on earlier versions eventStore.requestAccess(to: .event, completion: { (granted: Bool, _: Error?) -> Void in completion(granted) }) } Solution for iOS 17 Calendar request authorization is to add two keys in Info.plist file including previous key i.e. NSCalendarsUsageDescription <string>We need this permission in order to set reminders for you</string> <key>NSCalendarsWriteOnlyAccessUsageDescription</key> <string>We need this permission in order to set reminders for you</string>
0
0
429
Nov ’23
JsonElement
Hello! Kotlin has such a wonderful thing as JsonElement, which allows you to postpone the decoding of dynamic json fields until better times. Is there an analogue in Swift Decodable?
0
0
248
Nov ’23
Xcode's implementation of Localisation is so odd
Localising my app I found the entire process quite un-swift like (i.e. unsafe and unintuitive) and was not the only one, even the great Paul Hudson also thought it a strange implementation. Following a tutorial by Andrii Halabuda (https://www.youtube.com/watch?v=_PBA20ZVk1A) relying on an Enum to reduce errors, I found that the strings needed in the "Localizable.strings" strings file could not be avoided. I have not tried localisation in SwiftUI, I see that it is different, is it easier i.e. can Plists and such be avoided ? Is there any word on deeper integration that is just more like a normal method ?
2
0
653
Nov ’23
Deeper SVG integration into Xcode ?
Exporting SVG (XML) data for something like a six sided shape (hexagon) requires a piecemeal approach and very specific string needed for the XML to be rendered correctly: svgData += "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n" svgData += "<svg width=\"\(viewWidth)\" height=\"\(viewHeight)\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n" But there is also the addition of the obligatory prefix "M", followed by co-ordinate date, the postfixed "Z" finishing with path and colour data: <path d=\"\(svgPathData)\" fill=\"\(randomColourSVG)\" /> \n The trouble I ran into was the all important middling section that contained the x/y path data for multiple sides shapes. Could this not simply be a method to pass through from UIBezierPath or CAShapeLayer so that Xcode does this for me, much like the convenience of .cgColor used after a UIColor ? Basically, is SVG likely to get deeper integration ?
0
0
304
Nov ’23
UIBezierPath shape positioning
The following function creates a hexagon shape but as I collect the six pieces of the shape to plot into SVG co-ordinates, the x/y data is not positioning them correctly: var positionX: CGFloat = 0 var positionY: CGFloat = 0 if isFirstShape == true { // ENSURE USER SATISFACTION WITH ONE SHAPE PLACED WHERE FINGER TAPS positionX = touchLocation.x positionY = touchLocation.y } else { positionX = CGFloat.random(in: touchLocation.x - 200...touchLocation.x + 200) positionY = CGFloat.random(in: touchLocation.y - 200...touchLocation.y + 200) } let randomLength: CGFloat = CGFloat.random(in: 100...150) let randomColour: UIColor = getRandomColor(withProbabilities: colorProbabilities) let rectangle : CGRect = CGRect(x: positionX, y: positionY, width: randomLength, height: randomLength) let cornerRadius: CGFloat = 10.0 // ROUNDING CORNER VALUE var angle : CGFloat = CGFloat(0.5) // ROTATE HEXAGON 90º let sides : Int = 6 let path = UIBezierPath() var svgPathData = "M" // SVG : PATH DATA (START) let theta : CGFloat = CGFloat(2.0 * Double.pi) / CGFloat(sides) let radius : CGFloat = (rectangle.width + cornerRadius - (cos(theta) * cornerRadius)) / 2.0 let center : CGPoint = CGPoint(x: rectangle.origin.x + rectangle.width / 2.0, y: rectangle.origin.y + rectangle.width / 2.0) // DETERMINE STARTING POINT FOR DRAWING ROUNDED CORNERS let corner : CGPoint = CGPoint(x: center.x + (radius - cornerRadius) * cos(angle), y: center.y + (radius - cornerRadius) * sin(angle)) // MOVE PATH TO NEW POSITION ACCOUNTING FOR THE ROUNDED CORNER ANGLE path.move(to: CGPoint(x: corner.x + cornerRadius * cos(angle + theta), y: corner.y + cornerRadius * sin(angle + theta))) // SVG : POSITIONING DATA svgPathData += " \(corner.x) \(corner.y)" for _ in 0..<sides { angle += theta // POINT ON THE CIRCUMFERENCE OF THE CIRCLE : DETERMINED BY THE ANGLE let corner = CGPoint(x: center.x + (radius - cornerRadius) * cos(angle), y: center.y + (radius - cornerRadius) * sin(angle)) // ONE OF SIX POINTS : DETERMINED BY RADIUS AND ANGLE let tip = CGPoint(x: center.x + radius * cos(angle), y: center.y + radius * sin(angle)) let start = CGPoint(x: corner.x + cornerRadius * cos(angle - theta), y: corner.y + cornerRadius * sin(angle - theta)) let end = CGPoint(x: corner.x + cornerRadius * cos(angle + theta), y: corner.y + cornerRadius * sin(angle + theta)) path.addLine(to: start) // CONTROL POINT : INFLUENCE THE SHAPE / DIRECTION OF CURVE path.addQuadCurve(to: end, controlPoint: tip) svgPathData += " \(corner.x) \(corner.y)" // SVG : POSITIONING DATA } path.close() let bounds = path.bounds // MOVE POINTS IN RELATION TO ORIGINAL RECTANGLE DATA let transform = CGAffineTransform(translationX: -bounds.origin.x + rectangle.origin.x / 2.0, y: -bounds.origin.y + rectangle.origin.y / 2.0) path.apply(transform) // CREATE UIView WITH CAShapeLayer let hexagon = UIView(frame: rectangle) let shapeLayer = CAShapeLayer() shapeLayer.path = path.cgPath shapeLayer.fillColor = randomColour.cgColor hexagon.layer.addSublayer(shapeLayer) // SVG : COLOUR DATA let randomColourRGBA = getRGBAComponents(randomColour) let randomColourSVG = toSVGString(red : randomColourRGBA.red, green : randomColourRGBA.green, blue : randomColourRGBA.blue, alpha : randomColourRGBA.alpha) // SVG : PATH DATA (END) svgPathData += " Z" let pathElement = "<path d=\"\(svgPathData)\" fill=\"\(randomColourSVG)\" /> \n" svgPathStrings.append(pathElement) addSubview(hexagon) I tried but it just distorted the corners: let centerX = (corner.x + tip.x + start.x + end.x) / 4.0 let centerY = (corner.y + tip.y + start.y + end.y) / 4.0
0
0
314
Nov ’23
Does Apple documentation really help you learn Swift?
👋 I've been trying to learn Swift from scratch. I don't have technical developer/ computer science experience, but I have been a designer for some time. I completed reading the Swift language guide and completed the SwiftUI Concepts Tutorials. I am now about half way through the iOS App Dev Tutorial but it feels like a level beyond my current understanding. Is there any documentation that I should be looking at instead? What's the best way to learn Swift/ SwiftUI? Thanks!!
3
0
227
Nov ’23