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
Reply to How can I remove the healthkit framework and source(Metadata Rejected)
e. pod update If any of the pods are using HealthKit internally, updating is not sufficient and you may need to remove them. You might be able to check if your app binary contains HK- or Health-something in the similar way as shown in this post.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Sep ’21
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Why do Let Integer constants change values or always equal Zero?
@blaine l, thanks for clarifying. And that is the one I was to propose to you. I will take time to check it on some actual devices. You should better send a bug report to Apple, attaching a project which reproduces the issue and the precise condition and steps-to-reproduce may be needed.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Why do Let Integer constants change values or always equal Zero?
Yes. The ones I showed in the video were all initialized. I cannot find anything in the video which confirms all the static let variables are initialized. Do you know how to check static let variable is initialized?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to App Crashing
Hard to say something sure with just shown EXC_BAD_ACCESS. Check all the info shown in Xcode, stack traces, debug prints and logs, all such things.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Why do Let Integer constants change values or always equal Zero?
static let variables (or you want to call them constants) are initialized dynamically on the first use. Did you check them in the debugger after all of them are initialized?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Sep ’21
Reply to @StateObject and view installation
Can you show a complete code to reproduce the issue?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Getting The Right Data Back
Your save(_:) does not make sense considering the fact prefArr is made of fetched results. Please clarify what you expect on calling save(_:)?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Converting UIImage to jpegData - [Metal] 9072 by 12198 iosurface is too large for GPU
Can you share the image which causes the issue?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’21
Reply to Count the number of managed objects in a @SectionedFetchRequest
According to the doc, SectionedFetchResults is a RandomAccessCollection of Section and Section is a RandomAccessCollection of Result (Result == CoreDataEntityClass, in your case). So, Please try something like this to get the total count: items.reduce(0, {$0 + $1.count})
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Sep ’21