Post

Replies

Boosts

Views

Activity

Reply to Toggling Values on EnvironmentValue (EditMode)
You can use toggle() on Bool, because Bool has a method named toggle(): toggle() As far as I checked, EditMode does not have a method named toggle() nor any other method of similar functionalities. EditMode If you do want it, you can define your own extension for EditMode: struct ContentView: View { @State var users = ["Susan", "Kate", "Natalie", "Kimberly", "Taylor", "Sarah", "Nancy", "Katherine", "Nicole", "Linda", "Jane", "Mary", "Olivia", "Barbara"] @State var editMode: EditMode = .inactive var body: some View { NavigationView { List { ForEach(users, id: \.self) { user in Text(user) } } .navigationBarTitle("Friends") .environment(\.editMode, $editMode) .navigationBarItems(leading: Button("Edit", action: { self.editMode.toggle() })) } } } extension EditMode { mutating func toggle() { if self.isEditing { self = .inactive } else { self = .active } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Checkmarks are not removed from the table cell
I use this if condition inside of didSelectRowAt method That seems to be the critical part of your code. In your code, item is a local variable. Changing the property of it does not affect the instance property food. Please try something like this: override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { foods[indexPath.row].isSelected.toggle() tableView.reloadRows(at: [indexPath], with: .automatic) } UITableView manages cell selection internally. So, you may need to add some code to make your isSelected work with such internal selection management, but anyway, please try the code above and tell us what happens.
Topic: UI Frameworks SubTopic: UIKit Tags:
Sep ’21
Reply to Completion with JSON
You are putting completion() at the wrong place: func first(completion: @escaping ()->()) { //<- let url = URL(string: "https://jsonplaceholder.typicode.com/users")! URLSession.shared.dataTask(with: url) { data, _, _ in if let data = data { let result = try? JSONDecoder().decode([User].self, from: data) self.result2 = result![0].name completion() //<- here } }.resume() //completion() //<- Not here }
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’21
Reply to Swift: how to check group membership between two ODRecord objects?
I'm guessing due to the idiosyncratic implementation of the underlying BOOL-returning NSError-taking method on the Objective-C side? Your guess seems to be right. The original method isMemberRecord:error: does not follow the convention of error throwing functions. To import error throwing functions correctly in Swift, the original Objective-C method should return false or nil only when error is thrown. You should better send a bug report to Apple. To work this around, you may need to write some Objective-C wrapper, something like this: ODRecord+hasMember.h #import <OpenDirectory/OpenDirectory.h> #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface ODRecord (hasMember) - (NSNumber * _Nullable)hasMember: (ODRecord *)record error: (NSError **)error; @end NS_ASSUME_NONNULL_END ODRecord+hasMember.m #include "ODRecord+hasMember.h" @implementation ODRecord (hasMember) - (NSNumber * _Nullable)hasMember: (ODRecord *)record error: (NSError **)error { NSError *resultError = nil; BOOL result = [self isMemberRecord:record error:&resultError]; if( resultError != nil ) { if( error != nil ) { *error = resultError; } return nil; } return [NSNumber numberWithBool:result]; } @end {YourProjectName}-Bridging-Header.h #include "ODRecord+hasMember.h" And you can use it like this: func myIsMember_attempt3(_ r: ODRecord, ofGroup g: ODRecord) -> Bool? { do { let isM = try g.hasMember(r).boolValue return isM } catch { print("Error: \(error)") return nil } }
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’21
Reply to How to Use a Button in navigationBarItems to Work with List
You can try something like this: struct ContentView: View { @State private var users = ["George", "Kenny", "Susan", "Natalie"] @State var editMode: EditMode = .inactive //<- var body: some View { NavigationView { List { ForEach(users, id: \.self) { user in Text(user) } .onDelete(perform: delete) } .environment(\.editMode, $editMode) //<- .navigationBarTitle("My family") .navigationBarItems(trailing: Button(action: { print("Edit button pressed...") self.editMode = .active //<- }) { Text("Edit") } ) } } func delete(at offsets: IndexSet) { users.remove(atOffsets: offsets) } } Create a state representing EditMode and pass the Binding of it to the environment \.editMode of the inner view (List). You may want to add animating or change the button to Done, but I think you can do it yourself.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Sep ’21
Reply to Some emojis are beyond the normal emoji unicode range.
As you know, Unicode consortium will add many emojis every year. https://unicode.org/emoji/charts/emoji-list.html I can find some more characters in range U+1FAD3...U+1FAD6. I'm not sure how many of them are already include in the Apple's emoji font, but eventually Apple may cover all. Which means, you may need to update the range list every time Unicode emojis are updated. One more, the code you found in the linked thread is far from complete. As you see in the whole list of emojis, some emojis are made of multiple code points. But the method isEmoji(_:) detects all the variation selectors as emoji, although there are many cases non-emoji character having variation selector. Find a better solution which detects emojis more completely, and which will be updated when Unicode standard is updated.
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’21