How to pass uiviewcontroller data to swiftui widget class i am unable to do it with app groups also write now i am sending it with userdefaults but unable to do it
if let userDefaults = UserDefaults(suiteName: "group.com.soup.ios.app") {
UserDefaults.standard.set(self.dat ?? "", forKey: "date")
UserDefaults.standard.set(self.textData.text ?? "", forKey: "text")
}
if let userDefaults = UserDefaults(suiteName: "group.com.soup.ios.app") {
let text = UserDefaults.standard.object(forKey: "text") as? String
Text(text ?? "Add Data").lineLimit(2)
.truncationMode(.middle)
}
I managed it to do it like this and it worked
extension UserDefaults {
static let group = UserDefaults(suiteName: "group.com.your.domain")!
}
setting data:
UserDefaults.group.set("objectToShare", forKey: "keyForTheObject")
getting data:
let object = UserDefaults.group.object(forKey: "keyForTheObject")
This solution is in swift 5.
In order to pass data between targets, you need to:
create your sharedDefaults structure
declare your global data variable
change the variable's data from your target\viewcontroller using support variables.
First of all create a new .swift file. Go on inspector>file>target membership and select the targets you want to comunicate.
SharedDefaults helper
//This goes in your new swift file
let sharedUserdefaults = UserDefaults(suiteName: SharedDefault.suitName)
struct SharedDefault {
static let suitName = "group.com.soup.ios.app"
struct Keys{
static let Key = "text"
}
}
Global data variable
//This goes in your new swift file
//This is an array of strings change the type by changing what's after "data:"
var data: [String] {
get {
return sharedUserdefaults?.stringArray(forKey: Key) as? [String] ?? [String]()
} set {
}
}
So what's above is a get/set variable which stores and receives the data you save/get from your widget and your parent app.
Actually saving/retrieving stuff
Now to use this stuff either in your app or target, you have to
Declare the support variables:
var myData: [String] = [] //This is not required but if you need to display contents in real time I'd suggest you use this.
var currentData = data //Helper variable to pass data to the sharedVariable
override func viewDidLoad() {
super.viewDidLoad()
myData.append(contentsOf: data)
}
Save stuff:
//Update both data and current data
myData.append("ok")
currentData.append("ok")
sharedUserdefaults?.set(currentData, forKey: SharedDefault.Keys.Key)
Delete stuff:
//Update both data and current data
mydata.remove(at: 0)
currentData.remove(at: 0)
sharedUserdefaults?.set(currentData, forKey: SharedDefault.Keys.Key)
That's it!
Related
I'm new with SwiftUI and i want to convert basically my computed property for being used in SwiftUI views with combine and all that. I couldn’t use it like that because "get set" doesn't work for my SwiftUI views and I kinda struggled here.
Maybe someone has a good solution how can i convert it with with combine to use in swift ui.
Storage service saves the Authdata into userdefaults.
var currentAuthData: AuthData? {
get {
return self.storageService.get(AuthData.self, forKey: authDataStorageKey)
}
set {
if let value = newValue {
self.storageService.store(value, forKey: authDataStorageKey)
}
}
}
This is how you would turn a computed property in SwiftUI, by making it a #propertyWrapper. I have added a solution to read the data using Combine if you need it. I also make you property optional.
I assume this is the model you want to save.
struct AuthData {
var name: String
var email: String
}
Prepare the protocol for your property wrapper to be able to be set to nil using Optional.
public protocol AnyOptional {
var isNil: Bool { get }
}
extension Optional: AnyOptional {
public var isNil: Bool { self == nil }
}
Extend UserDefaults to conform to this protocol and be optional.
extension UserDefault where Value: ExpressibleByNilLiteral {
init(key: String, _ container: UserDefaults = .standard) {
self.init(key: key, defaultValue: nil, container: container)
}
}
Create the property wrapper that is the same thing as a getter and setter for SwiftUI views. This is a generic one and can be use to any type. You can set them in the UserDefaults extension after this bloc of code.
import Combine
#propertyWrapper
struct UserDefault<Value> {
let key: String
let defaultValue: Value
var container: UserDefaults = .standard
// Set a Combine publisher for your new value to
// always read its changes when using Combine.
private let publisher = PassthroughSubject<Value, Never>()
var wrappedValue: Value {
get {
// Get the new value or nil if any.
container.object(forKey: key) as? Value ?? defaultValue
}
set {
// Check if the value is nil and remove it from your object.
if let optional = newValue as? AnyOptional, optional.isNil {
container.removeObject(forKey: key)
}
else {
// Set your new value inside UserDefaults.
container.set(newValue, forKey: key)
}
// Add the newValue to your combine publisher
publisher.send(newValue)
}
}
var projectedValue: AnyPublisher<Value, Never> {
publisher.eraseToAnyPublisher()
}
}
Create the extension in UserDefaults to use your property wrapper in your code.
extension UserDefaults {
#UserDefault(key: "authDataStorageKey", defaultValue: nil)
static var savedAuthData: AuthData?
// You can create as many #propertyWrapper as you want that fits your need
}
examples of use
// Set a new value
UserDefaults.savedAuthData = AuthData(name: "Muli", email: "muli#stackoverflow.com")
// Read the saved value
print(UserDefaults.savedAuthData as Any)
// When using combine
var subscriptions = Set<AnyCancellable>()
UserDefaults.$savedAuthData
.sink { savedValue in
print(savedValue as Any) } // Yours saved value that changes over time.
.store(in: &subscriptions)
I wanna have an String Array in the Userdefaults, which I can edit. That means I want to append new Items to this Array.Here is some example Code
var defaults = UserDefaults.standard
defaults.array(forKey: "myArray")?.append("NewElement")
But I get this Error Code:
Cannot use mutating member on immutable value: function call returns immutable value
How can I handle that? Do you need more information? Thanks for your time
Boothosh
You need to assign the result of the function to variable before you can modify it
if var array = defaults.array(forKey: "myArray") {
array.append("NewElement")
}
You can extend UserDefaults and create a computed property with a getter and a setter. This way you can directly access your array straight from disk:
extension UserDefaults {
var myStringArray: [String] {
get { stringArray(forKey: "myStringArray") ?? [] }
set { set(newValue, forKey: "myStringArray") }
}
}
Usage:
UserDefaults.standard.myStringArray.append("NewElement")
UserDefaults.standard.myStringArray // ["NewElement"]
You can save or read an array, but you should make your job finish before saving it, and then you can start using or adding new data after reading.
PS: I used SwiftUI for show case, but saveData() and readData() functions are usable in UIKit as well.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(spacing: 20) {
Button("saveData") { saveData() }
Button("readData") { readData() }
}
.font(Font.body.weight(Font.Weight.bold))
}
}
func saveData() {
var array: [String] = [String]()
array.append("a")
array.append("b")
array.append("c")
UserDefaults.standard.set(array, forKey: "array")
print("saved!")
}
func readData() {
if let unwrappedData = UserDefaults.standard.stringArray(forKey: "array") {
var array: [String] = unwrappedData
array.append("d")
print(array)
}
}
try to save user setting, but UserDefaults is not working, Xcode 12.3, swiftui 2.0, when I am reload my app, my setting not updating for new value)
class PrayerTimeViewModel: ObservableObject {
#Published var lm = LocationManager()
#Published var method: CalculationMethod = .dubai {
didSet {
UserDefaults.standard.set(method.params, forKey: "method")
self.getPrayerTime()
}
}
func getPrayerTime() {
let cal = Calendar(identifier: Calendar.Identifier.gregorian)
let date = cal.dateComponents([.year, .month, .day], from: Date())
let coordinates = Coordinates(latitude: lm.location?.latitude ?? 0.0, longitude: lm.location?.longitude ?? 0.0)
var par = method.params
par.madhab = mashab
self.times = PrayerTimes(coordinates: coordinates, date: date, calculationParameters: par)
}
and view.. update with AppStorage
struct MethodView: View {
#ObservedObject var model: PrayerTimeViewModel
#Environment(\.presentationMode) var presentationMode
#AppStorage("method", store: UserDefaults(suiteName: "method")) var method: CalculationMethod = .dubai
var body: some View {
List(CalculationMethod.allCases, id: \.self) { item in
Button(action: {
self.model.objectWillChange.send()
self.presentationMode.wrappedValue.dismiss()
self.model.method = item
method = item
}) {
HStack {
Text("\(item.rawValue)")
if model.method == item {
Image(systemName: "checkmark")
.foregroundColor(.black)
}
}
}
}
}
}
You have two issues.
First, as I mentioned in my comment above that you are using two different suites for UserDefaults. This means that you are storing and retrieving from two different locations. Either use UserDefaults.standard or use the one with your chosen suite UserDefaults(suitName: "method") - you don't have to use a suite unless you plan on sharing your defaults with other extensions then it would be prudent to do so.
Secondly you are storing the wrong item in UserDefaults. You are storing a computed property params rather than the actual enum value. When you try to retrieve the value it fails as it is not getting what it expects and uses the default value that you have set.
Here is a simple example that shows what you could do. There is a simple enum that has a raw value (String) and conforms to Codable, it also has a computed property. This matches your enum.
I have added an initialiser to my ObservableObject. This serves the purpose to populate my published Place from UserDefaults when the Race object is constructed.
Then in my ContentView I update the place depending on a button press. This updates the UI and it updates the value in UserDefaults.
This should be enough for you to understand how it works.
enum Place: String, Codable {
case first
case second
case third
case notPlaced
var someComputedProperty: String {
"Value stored: \(self.rawValue)"
}
}
class Race: ObservableObject {
#Published var place: Place = .notPlaced {
didSet {
// Store the rawValue of the enum into UserDefaults
// We can store the actual enum but that requires more code
UserDefaults.standard.setValue(place.rawValue, forKey: "method")
// Using a custom suite
// UserDefaults(suiteName: "method").setValue(place.rawValue, forKey: "method")
}
}
init() {
// Load the value from UserDefaults if it exists
if let rawValue = UserDefaults.standard.string(forKey: "method") {
// We need to nil-coalesce here as this is a failable initializer
self.place = Place(rawValue: rawValue) ?? .notPlaced
}
// Using a custom suite
// if let rawValue = UserDefaults(suiteName: "method")?.string(forKey: "method") {
// self.place = Place(rawValue: rawValue) ?? .notPlaced
// }
}
}
struct ContentView: View {
#StateObject var race: Race = Race()
var body: some View {
VStack(spacing: 20) {
Text(race.place.someComputedProperty)
.padding(.bottom, 20)
Button("Set First") {
race.place = .first
}
Button("Set Second") {
race.place = .second
}
Button("Set Third") {
race.place = .third
}
}
}
}
Addendum:
Because the enum conforms to Codable it would be possible to use AppStorage to read and write the property. However, that won't update the value in your ObservableObject so they could easily get out of sync. It is best to have one place where you control a value. In this case your ObservableObject should be the source of truth, and all updates (reading and writing to UserDefaults) should take place through there.
You write in one UserDefaults domain but read from the different. Assuming your intention is to use suite only UserDefaults, you should change one in model, like
#Published var method: CalculationMethod = .dubai {
didSet {
UserDefaults(suiteName: "method").set(method.params, forKey: "method")
self.getPrayerTime()
}
}
or if you want to use standard then just use AppStorage with default constructor, like
// use UserDefaults.standard by default
#AppStorage("method") var method: CalculationMethod = .dubai
I'm trying to do a set and get method to this Data manager class, from a dictionary and I don't know how to insert the values of the dictionary' in the set and get method (i have changed it from an array to Dic) thanks
class DataManager {
private static var sharedInstance: DataManager?
private var recordsArray: [[String:String]] = []
private let defaults = UserDefaults.standard
let userRecord: String = "userRecord";
private init(){}
public static func getInstance()-> DataManager{
if DataManager.sharedInstance == nil {
DataManager.sharedInstance = DataManager()
}
return DataManager.sharedInstance!
}
//here is my problem - set and get methods
//I don't know how to move the parameters
public func setRecordsArray([_:String,path:String]) {
self.recordsArray.append(?);
defaults.set(self.recordsArray, forKey: self.userRecord)
}
// again the same problem
public func getRecordsArray() -> [String] {
let a = self.defaults.array(forKey: self.userRecord);
return a as! [String];
}
}
The key to answering your question is to know the type of variable you want to set and get.
In this case, the recordsArray variable is an array of dictionaries of string values and keys: [[String:String]]
So, one way to pass this parameter is to create a variable of the same type as it should be set:
public func setRecordsArray(array:[[String:String]]) {
self.recordsArray = array
defaults.set(self.recordsArray, forKey: self.userRecord)
}
It simply updates the value of the self.recordsArray variable and sets the value in user defaults.
The get method works similar, however it returns a variable with the same type that should be returned.
A ? was added in the return because if there is no saved user defalts, the method returns nil:
public func getRecordsArray() -> [[String:String]]?{
if let array = defaults.object(forKey: self.userRecord) as? [[String:String]]{
self.recordsArray = array
return array
}
return nil
}
Also, you can make a set method for insert elements inside the array.
in this case, parameter type must be like [String:String], the element type of this array:
public func setRecord(record:[String:String]) {
if let array = defaults.object(forKey: self.userRecord) as? [[String:String]]{
self.recordsArray = array
self.recordsArray.append(record)
defaults.set(self.recordsArray, forKey: self.userRecord)
} else{
self.recordsArray.append(record)
defaults.set(self.recordsArray, forKey: self.userRecord)
}
}
Hello I'm new at swift programming and i want to get label value from loadData() to use use for my path (reference) to my database on dbRef!.child(place+"/placeLabel"). What I mean?! I read data that are on "Dio Con Dio" node. That happens because the value place is let place = "Dio Con Dio". So, my application doesn't load data from "Paradosiako - Panorama" node. I want to load everything from database and i thought that if i could make place value to change to the next node, it could read all the data.
import UIKit
import FirebaseDatabase
class PlacesTableViewController: UITableViewController {
//MARK: Properties
#IBOutlet weak var placesTableView: UITableView!
//database reference
var dbRef:FIRDatabaseReference?
var places = [Places]()
private var loadedLabels = [String: String]()
private var loadedRatings = [String: Int]()
//handler
var handle:FIRDatabaseHandle?
override func viewDidLoad() {
super.viewDidLoad()
dbRef = FIRDatabase.database().reference()
// Loads data to cell.
loadData()
}
private func loadData() {
let place = "Dio Con Dio"
dbRef!.child(place+"/placeLabel").observe(.childAdded, with: {
(snapshot) in
let label = snapshot.value as! String
self.updatePlace(snapshot.key, label: label)
})
dbRef!.child(place+"/rating").observe(.childAdded, with: {
(snapshot) in
let rating = snapshot.value as! Int
self.updatePlace(snapshot.key, rating: rating)
})
}
private func updatePlace(_ key: String, label: String? = nil, rating: Int? = nil) {
if let label = label {
loadedLabels[key] = label
}
if let rating = rating {
loadedRatings[key] = rating
}
guard let label = loadedLabels[key], let rating = loadedRatings[key] else {
return
}
if let place = Places(name: label, rating: rating) {
places.append(place)
placesTableView.reloadData()
}
}
}
(This question is a follow up from this one.)
It's difficult to explain this in a comment, so I'll address your current issue here, but Paulo answered your question, I believe. You could even open a new question on this issue.
Here's the code you're having an issue with:
for (key, rating) in ratings.value as! [String: Int] {
self.updatePlace(key, rating: rating)
}
There's nothing wrong with this code, as long as ratings.value is filled with string: integer pairs. Your error occurs because somehow a there is a key missing. (the runtime found a NSNull, so it couldn't convert the data to a dictionary)
You need to set a breakpoint where you are constructing the array in:
placeSnapshot.childSnapshot(forPath: "rating")
(which is called ratings here/locally). If is ok to ignore entries with no key, then skip them and don't append them to your array. But it's more likely you have an error and need to find out why you're adding a null value...
Does that make more sense? Please accept Paulo's answer if he solved an issue, and open a new question with more information about childSnapshot (the code), and someone will be sure help you. (I know I'll try.)
For your new database schema, try this instead:
private func loadData() {
dbRef!.observe(.childAdded) {
(placeSnapshot) in
print("Adding place \(placeSnapshot.key)...")
// Load each label on the "placeLabel" list.
let labels = placeSnapshot.childSnapshot(forPath: "placeLabel")
for (key, label) in labels.value as! [String: String] {
self.updatePlace(key, label: label)
}
// Load each rating in the "rating" list.
let ratings = placeSnapshot.childSnapshot(forPath: "rating")
for (key, rating) in ratings.value as! [String: Int] {
self.updatePlace(key, rating: rating)
}
}
}
The updatePlace function simply groups the label and rating properties, of a given place, that were separately loaded by loadData.
Places removal. Of course, the above code won't handle place removal. To do so, you will need to listen for the childRemoved event as well.