swift5

SwiftUI: How to persist @Published variable using UserDefaults?

╄→гoц情女王★ 提交于 2019-12-03 20:43:59
I want a @Published variable to be persisted, so that it's the same every time when I relaunch my app. I want to use both the @UserDefault and @Published property wrappers on one variable. For example I need a ' @PublishedUserDefault var isLogedIn '. I have the following propertyWrapper import Foundation @propertyWrapper struct UserDefault<T> { let key: String let defaultValue: T init(_ key: String, defaultValue: T) { self.key = key self.defaultValue = defaultValue } var wrappedValue: T { get { return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue } set { UserDefaults.standard

UIAlertController's actionSheet gives constraint error on iOS 12.2 / 12.3 [duplicate]

☆樱花仙子☆ 提交于 2019-12-02 16:35:25
This question already has an answer here: Swift default AlertViewController breaking constraints 1 answer On iOS 12.2. while using UIAlertController's actionSheet Xcode, gives constraint error anyone having this problem This same code runs on iOS 12.1 with no error I have tested this code on Xcode 10.2 and 10.1 class ViewController: UIViewController { let Click : UIButton = { let button = UIButton(type: UIButton.ButtonType.system) button.translatesAutoresizingMaskIntoConstraints = false button.setTitle("OK", for: .normal) button.tintColor = UIColor.blue button.addTarget(self, action: #selector

Map value in particular key in array of dictionary

我们两清 提交于 2019-12-02 09:04:24
Map value the particular key wherever presents in array of dictionary and replace it in same array. We need to update pan_card key 0 to 1 , in occurence of array of dictionary. let keyToUpdate = "pan_card" var arrayOfDictionary = [[String:Any]]() var firstDict = [String:Any]() firstDict["passport"] = 0 firstDict["ration_card"] = 0 firstDict["pan_card"] = 0 var arrayDict = [String : Any]() arrayDict["currentObject"] = firstDict arrayDict["title"] = "Documents list" var secondDict = [String:Any]() secondDict["dl"] = 0 secondDict["voter"] = 0 secondDict["pan_card"] = 0 //let dic = secondDict

Swift 5: Index of a Character in String

扶醉桌前 提交于 2019-12-01 06:12:42
Before Swift 5, I had this extension working: fileprivate extension String { func indexOf(char: Character) -> Int? { return firstIndex(of: char)?.encodedOffset } } Now, I get a deprecated message: 'encodedOffset' is deprecated: encodedOffset has been deprecated as most common usage is incorrect. Use `utf16Offset(in:)` to achieve the same behavior. Is there a simpler solution to this instead of using utf16Offset(in:) ? I just need the index of the character position passed back as an Int. And what is wrong with utf16Offset(in:) ? This is way to go with Swift 5 fileprivate extension String {

Swift 5: Index of a Character in String

Deadly 提交于 2019-12-01 03:46:48
问题 Before Swift 5, I had this extension working: fileprivate extension String { func indexOf(char: Character) -> Int? { return firstIndex(of: char)?.encodedOffset } } Now, I get a deprecated message: 'encodedOffset' is deprecated: encodedOffset has been deprecated as most common usage is incorrect. Use `utf16Offset(in:)` to achieve the same behavior. Is there a simpler solution to this instead of using utf16Offset(in:) ? I just need the index of the character position passed back as an Int. 回答1:

Delegate methods in child class sometimes not called with Swift 5 compiler

强颜欢笑 提交于 2019-11-30 11:48:03
问题 EDIT: As sunshinejr pointed out here, this has been fixed and will be released together with the next Xcode/Swift version. I've seen a lot of weird behaviour after updating Xcode 10.1 to Xcode 10.2, both with Swift 4 and Swift 5 codebases. One of the problems is that on one ViewController the ScrollView delegate methods are no longer called. The simplified view hierarchy is as follows: | ScrollView (ParentScrollView) | -- Stack View | ---- ScrollView (ChildScrollView) | ---- ScrollView

Delegate methods in child class sometimes not called with Swift 5 compiler

≡放荡痞女 提交于 2019-11-30 02:27:15
EDIT: As sunshinejr pointed out here , this has been fixed and will be released together with the next Xcode/Swift version. I've seen a lot of weird behaviour after updating Xcode 10.1 to Xcode 10.2, both with Swift 4 and Swift 5 codebases. One of the problems is that on one ViewController the ScrollView delegate methods are no longer called. The simplified view hierarchy is as follows: | ScrollView (ParentScrollView) | -- Stack View | ---- ScrollView (ChildScrollView) | ---- ScrollView (ChildScrollView) | ---- ScrollView (ChildScrollView) It acts as a view with several pages: ParentScrollView

SwiftUI Present Alert with Input Field?

北城以北 提交于 2019-11-29 17:48:20
In SwiftUI I'm trying to make an alert message with input, and then get the value from the input. I've found many good tutorials how to make the Alert View in SwiftUI, but dont found how to add TextField() in Alert. This alert will update Categories table on Server, i will call a put request with this alert. @State var isInsertAlertVisible = false // state var is define in ContentView // Button is define in body Button(action: { self.isInsertAlertVisible = true } , label: { Text("Create") .multilineTextAlignment(.center) .foregroundColor(Color.white) }) .presentation($isInsertAlertVisible) {

Swift: 'Hashable.hashValue' is deprecated as a protocol requirement;

南笙酒味 提交于 2019-11-29 03:22:56
I've been facing following issue (it's just a warning) with my iOS project. 'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'ActiveType' to 'Hashable' by implementing 'hash(into:)' instead Xcode 10.2 Swift 5 Source Code: public enum ActiveType { case mention case hashtag case url case custom(pattern: String) var pattern: String { switch self { case .mention: return RegexParser.mentionPattern case .hashtag: return RegexParser.hashtagPattern case .url: return RegexParser.urlPattern case .custom(let regex): return regex } } } extension ActiveType: Hashable, Equatable {

SwiftUI Present Alert with Input Field?

泪湿孤枕 提交于 2019-11-28 12:16:29
问题 In SwiftUI I'm trying to make an alert message with input, and then get the value from the input. I've found many good tutorials how to make the Alert View in SwiftUI, but dont found how to add TextField() in Alert. This alert will update Categories table on Server, i will call a put request with this alert. @State var isInsertAlertVisible = false // state var is define in ContentView // Button is define in body Button(action: { self.isInsertAlertVisible = true } , label: { Text("Create")