swift5

Cannot store custom class in UserDefaults - Attempt to insert non-property list

情到浓时终转凉″ 提交于 2021-02-11 13:01:44
问题 I'm trying to store a custom class in UserDefaults , however I'm getting the "Attempt to insert non-property list" error as soon as the app starts. I have successfully stored arrays of custom classes, so I followed the same principle, but something is wrong. This is my class: import Foundation public class PlanRouteFilters: NSObject, NSCoding, Codable { var favoriteLines: Bool var routeType: Int var bus: Bool var train: Bool var subway: Bool var electric: Bool var sharedCar: Bool var bike:

How should I get and set a value of UserDefaults?

落花浮王杯 提交于 2021-02-11 13:00:44
问题 I'm currently developing an application using SwiftUI. I want to use a UserDefaults value in this app. So I made a code below. But in this case, when I reboot the app(the 4'th process in the process below), I can't get value from UserDefaults... Build and Run this project. Pless the home button and the app goes to the background. Double-tap the home button and remove the app screen. press the app icon and reboot the app. Then I want to get value from UserDefaults. to resolve this problem how

How should I get and set a value of UserDefaults?

半腔热情 提交于 2021-02-11 12:59:07
问题 I'm currently developing an application using SwiftUI. I want to use a UserDefaults value in this app. So I made a code below. But in this case, when I reboot the app(the 4'th process in the process below), I can't get value from UserDefaults... Build and Run this project. Pless the home button and the app goes to the background. Double-tap the home button and remove the app screen. press the app icon and reboot the app. Then I want to get value from UserDefaults. to resolve this problem how

Open a short video using QuickTime Player

戏子无情 提交于 2021-02-11 12:02:57
问题 I have a short video on my local machine. I am trying to open it using NSWorkspace on QuickTime Player. let stringUrl = "~/Desktop/myrec.mov" let url = URL(string: stringUrl)! let config = NSWorkspace.OpenConfiguration() config.activates = true NSWorkspace.shared.open( [url], withApplicationAt: URL(string: "~/Applications/QuickTime Player")!, configuration: config ) The error I am getting is: Fatal error: Unexpectedly found nil while unwrapping an Optional value: file cap/cap.swift Which has

Open a short video using QuickTime Player

为君一笑 提交于 2021-02-11 12:02:05
问题 I have a short video on my local machine. I am trying to open it using NSWorkspace on QuickTime Player. let stringUrl = "~/Desktop/myrec.mov" let url = URL(string: stringUrl)! let config = NSWorkspace.OpenConfiguration() config.activates = true NSWorkspace.shared.open( [url], withApplicationAt: URL(string: "~/Applications/QuickTime Player")!, configuration: config ) The error I am getting is: Fatal error: Unexpectedly found nil while unwrapping an Optional value: file cap/cap.swift Which has

How do you find the top 3 maximum values in a Swift dictionary?

ε祈祈猫儿з 提交于 2021-02-08 11:38:04
问题 I learned that I can show the key and value that is the highest in the dictionary by the code below // champions dictionary var champions = ["Ekko": 20, "Ahri": 10, "Vayne": 2, "Neeko": 25, "Zed": 6] let greatestChampion = champions.max { a, b in a.value < b.value } print greatestChampion // optional(("Ekko": 20)) My question is how can I show 3 champions with the highest value? Example result would be print greatestChampion // optional(("Ekko": 20, "Neeko": 25, "Ahri": 10)) I would love to

Adding Time Interval. e.g hours, minutes and seconds

我是研究僧i 提交于 2021-02-08 11:28:26
问题 I am getting time from the response in hours, minutes and seconds like "01:32:34" of multiple objects. I have saved it in a custom date object, where i am saving the date value and the string value, there are a large number of records so i am saving it in my local db and upon retrieving i get the date value in the format 1999-12-31 19:01:04 +0000 whereas my string value which i am getting from response as well is 19:01:04 . now i would like to add all of these values to return a string e.g 1

Adding Time Interval. e.g hours, minutes and seconds

梦想与她 提交于 2021-02-08 11:27:31
问题 I am getting time from the response in hours, minutes and seconds like "01:32:34" of multiple objects. I have saved it in a custom date object, where i am saving the date value and the string value, there are a large number of records so i am saving it in my local db and upon retrieving i get the date value in the format 1999-12-31 19:01:04 +0000 whereas my string value which i am getting from response as well is 19:01:04 . now i would like to add all of these values to return a string e.g 1

How to detect if Local Network permissions are granted in iOS 14

最后都变了- 提交于 2021-02-07 19:14:46
问题 How to detect if the user has granted the local network permission in the app for iOS 14? I have to show an error screen if the user has denied permission and redirects to os settings to grant permission. Has Apple provided any way to find out just like location permission? 回答1: I wrote up this class that can be used if you're not on iOS 14.2. This class will prompt user for permission to access local network (first time). Verify existing permission state if already denied/granted. Just

replacingOccurrences throws an error by using an array for options

為{幸葍}努か 提交于 2021-02-05 12:32:16
问题 Can someone explain me the difference between the following code snippets? I do not understand why the second one throws an error. This one works: "Anno 1800".replacingOccurrences(of: "N", with: "#", options: [.regularExpression, .caseInsensitive], range: nil) But this one gives an error: let optionsArr = [NSString.CompareOptions.regularExpression, NSString.CompareOptions.caseInsensitive] "Anno 1800".replacingOccurrences(of: "N", with: "#", options: optionsArr, range: nil) error: