one way is to call function from Task, that will set new value for personName
let person = Person()
var personName: String
print("start")
let nameTask = Task {
return await person.name
}
Task {
do {
let getNewName = try await nameTask.result.get()
changeName(newName: getNewName)
// Error: Mutation of captured var 'personName' in concurrently-executing code
} catch {
print("error!!!")
}
}
func changeName(newName: String){
personName = newName
}
print("The person's name is \(personName)")
}
Post
Replies
Boosts
Views
Activity
Yes I run it as macOS app.
Run the app. YES
Press the "TABLE" link. YES
Press the "Generate new data" button. This is when you see the error? YES
The message is related to table refresh. If you don't see it then probably there are not enough items in table. So pls keep increasing the rows. instead of while x < 10000 try to increase it to 100 000 000.
Different hardware acts differently. less powerful hardware needs less items in table for message to appear.
But even then my concern is about architecture. is there something obviously wrong in the code, how the table items are refreshed/renewed?
I solved it finally so.
struct Piiker: View {
@State var selectOption: [String] = ["1","2","3"]
@State var fieldValue: String = ""
var body: some View {
Picker("Select number", selection: $fieldValue){
Text("").tag("") //basically added empty tag and it solve the case
ForEach(selectOption, id: \.self){
Text($0)
}
}
}
}
But it still odd, that Picker need that initial value tag for Dynamic dataset. Previously Picker was able to handle ForEach cases the tags, by itself.
Ok I found the solution so case closed (despite I get warning from CGIMAGE 'kUTTypeJPEG' was deprecated in macOS 12.0: Use UTTypeJPEG instead.) :)
so I have method (from apple textbook)
// https://developer.apple.com/documentation/quicklookthumbnailing/creating_quick_look_thumbnails_to_preview_files_in_your_app
func giveThumbnail(for fileURL: URL, size: CGSize, scale: CGFloat) -> String{
let request = QLThumbnailGenerator
.Request(fileAt: fileURL, size: size, scale: scale,
representationTypes: .lowQualityThumbnail)
QLThumbnailGenerator.shared.generateRepresentations(for: request)
{ (thumbnail, type, error) in
DispatchQueue.main.async {
if thumbnail == nil || error != nil {
// Handle the error case gracefully.
return "ERROR"
} else {
// Display the thumbnail that you created.
let fileInBase64 = thumbnail?.cgImage.jpegData?.base64EncodedString()
return fileBase64
}
}
}
}
and extension for CGImage (found with google)
extension CGImage {
//here I Still get warning message 'kUTTypeJPEG' was deprecated in macOS 12.0: Use UTTypeJPEG instead.
var jpegData: Data? {
guard let mutableData = CFDataCreateMutable(nil, 0),
let destination = CGImageDestinationCreateWithData(mutableData, UTType.jpeg.identifier as CFString, 1, nil)
else {
return nil
}
CGImageDestinationAddImage(destination, self, nil)
guard CGImageDestinationFinalize(destination) else { return nil }
return mutableData as Data
}
}
and it works ...
well turned out that I can use Simulator for it. And I don't need to have actual device for testing.
Thank you :)
I also reached to the same conclusion.
However I solved it so:
struct testtable: View {
struct Person: Identifiable {
let givenName: String
let familyName: String
let index: String
let id = UUID()
}
@State private var people = [
Person(givenName: "Juan", familyName: "Chavez", index: "0"),
Person(givenName: "Mei", familyName: "Chen", index: "1"),
Person(givenName: "Tom", familyName: "Clark", index: "2, id: 2"),
Person(givenName: "Gita", familyName: "Kumar", index: "3, id: 3"),
]
@State private var sortOrder = [KeyPathComparator(\Person.givenName)]
@State private var selection : Person.ID?
var body: some View {
Button(action: {
if self.selection != nil {
guard let select = people.first(where: { $0.id == self.selection
}) else {
fatalError()
}
print("array[\(self.selection)]")
print(select)
}
}){
Label("Print Array Items", systemImage: "doc.text.magnifyingglass")}
Table(people, selection: $selection, sortOrder: $sortOrder) {
TableColumn("Given Name", value: \.givenName)
TableColumn("Family Name", value: \.familyName)
}
.onChange(of: sortOrder) {
people.sort(using: $0)
}
}
}