Post

Replies

Boosts

Views

Activity

CryptoKit to create JWT
Hi,I would like to create a JWT to interact with Apple's Webservices.As the first step, I have a private key file, I have loaded it as Data.I would like to create an instance of Private Key instance (not sure what is the correct struct to use, tried HMAC, P256.Signing.PrivateKey etc)Every time an exception is thrown.Any help and direction would greatly help. Many thanks.let privateKeyPath = URL(fileURLWithPath: "some valid path") let privateKeyData = try Data(contentsOf: privateKeyPath, options: .alwaysMapped) let message = "abcd" let messageData = message.data(using: .utf8)! do { //Option1: //This throws: Error: incorrectParameterSize let signature1 = try P256.Signing.ECDSASignature(rawRepresentation: privateKeyData) //Option2: let key2 = SymmetricKey(data: privateKeyData) //This throws: Error: incorrectKeySize var encryptedContent2 = try AES.GCM.seal(messageData, using: key2).combined } catch { print("Error: \(error)") }
8
0
4.3k
Feb ’20
Binding value from an ObservableObject
Aim:I have a model which is an ObservableObject. It has a Bool property, I would like to use this Bool property to initialise a @Binding variable.Questions:How to convert an @ObservableObject to a @Binding ?Is creating a @State the only way to initialise a @Binding ?Note:I do understand I can make use of @ObservedObject / @EnvironmentObject, and I see it's usefulness, but I am not sure a simple button needs to have access to the entire model.Or is my understanding incorrect ?Code:import SwiftUI import Combine import SwiftUI import PlaygroundSupport class Car : ObservableObject { @Published var isReadyForSale = true } struct SaleButton : View { @Binding var isOn : Bool var body: some View { Button(action: { self.isOn.toggle() }) { Text(isOn ? "On" : "Off") } } } let car = Car() //How to convert an ObservableObject to a Binding //Is creating an ObservedObject or EnvironmentObject the only way to handle a Observable Object ? let button = SaleButton(isOn: car.isReadyForSale) //Throws a compilation error and rightly so, but how to pass it as a Binding variable ? PlaygroundPage.current.setLiveView(button)
3
4
23k
Dec ’19
KVC / KVO for Operation (Swift 4)
I have created an AsyncOperation which is a subclass of Operation.I am overriding the following methods to use KVC:- isExecuting- isFinishedProblem:When I use String in place of KeyPath, my completion block gets executed. If I use KeyPath the completion block of the operation is not getting executed.Works ok:willChangeValue(forKey: "isExecuting")didChangeValue(forKey: "isExecuting")willChangeValue(forKey: "isFinished")didChangeValue(forKey: "isFinished")Doesn't work (Compiles ok, but at runtime the completion block doesn't get executed):willChangeValue(for: \.isExecuting)didChangeValue(for: \.isExecuting)willChangeValue(for: \.isFinished)didChangeValue(for: \.isFinished)Note: I am using the following:- macOS 10.12.5 (16F73)- Xcode 9.0 beta (9M136h)Questions:1. Why isn't KeyPath working when using the String works ?2. How to fix using KeyPath ?Complete Code:class AsyncOperation : Operation { private var _executing = false private var _finished = false override var isExecuting : Bool { get { return _executing } set { willChangeValue(for: \.isExecuting _executing = newValue didChangeValue(for: \.isExecuting) } } override var isFinished : Bool { get { return _finished } set { willChangeValue(for: \.isFinished) _finished = newValue didChangeValue(for: \.isFinished) } } override var isAsynchronous : Bool { return true } func markAsCompleted() { isExecuting = false isFinished = true } }
8
0
5.1k
Jun ’17