Post

Replies

Boosts

Views

Activity

Stuck
I have a big problem with an AI app. It's supposed to give me a color according to the text entered into a TextField. But every time it gives me the same output. I'm new in developing, and I know it must be a tiny bug but I just couldn't find out why. Should I just ... paste all of my code here? I can't upload a .zip :(
6
0
554
May ’22
No idea
I have a structure TodoItem struct TodoItem: Codable, Hashable, Identifiable {     var id: UUID = UUID()     var item: String     var done: Bool = false } And I have an array of them, and I’d like to find out a way to calculate(in a array) how many are done; that is, myArray.count / itemsDone .count is easy, itemsDone can be achieved like this: extension Array where Element == TodoItem {     var itemsDone: Double {         var done: Double = 0         for i in self {             if i.done { done += 1 }         }         return done     } } but it didn’t. struct ContentView: View { @State var todos: [TodoItem] … ProgressView(value: todos.itemsDone / todos.count) // Error My Playground won’t let me copy the error(no idea why) and my family’s iPad is in Chinese so one is Chinese. I’ll try it on Xcode and see if I can find out more. will update with the error. EDiT nor will this work: ProgressView(value: todos.itemsDone, total: todos.count)
5
0
709
Jul ’22
Strange SwiftUI bug (just making sure if it's a bug or not)
My code: struct PopoverMessage: View {     var body: some View {         VStack(alignment: .leading) {             Text("You need to launch this game at Teminal.")                 .font(.title)                 .fontWeight(.light)             Divider()             HStack {                 Text("Help me generate launch command ")                     .fontWeight(.thin)                 Image(systemName: "rectangle.portrait.and.arrow.right.fill")                     .resizable()                     .foregroundColor(.gray)                     .frame(width: 10, height: 10)             }.padding(10)         }     } } works while struct PopoverMessage: View {     var body: some View {         VStack(alignment: .leading) {             Text("You need to launch this game at Teminal.")                 .font(.title)                 .fontWeight(.light)             Divider()             HStack {                 Text("Help me generate launch command ")                     .fontWeight(.thin)                 Image(systemName: "rectangle.portrait.and.arrow.right.fill") .weigh                     .resizable()                     .foregroundColor(.gray)                     .frame(width: 10, height: 10)             }.padding(10)         }     } } don't and displays a error message Trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure Here's the text in the diagnostics sheet: trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure ---------------------------------------- CompileDylibError: Failed to build ContentView.swift Compiling failed: trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure /Users/wangkeshijian/Documents/.myFolder/.system/TerminalAdventure/TerminalAdventure/ContentView.swift:12:37: error: trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure         VStack(alignment: .leading) {                                     ^ SwiftUI.VStack:4:12: note: 'init(alignment:spacing:)' declared here     public init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil)            ^ // my comment here: it seems that SwiftUI, under some sort of strange bug, did notrecognize my initializer ================================== |  BuildInvocationError |   |  /Users/wangkeshijian/Documents/.myFolder/.system/.Downloads/Xcode14-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -F /Users/wangkeshijian/Documents/.myFolder/.system/.Downloads/Xcode14-beta.app/Contents/SharedFrameworks -enforce-exclusivity=checked -DDEBUG -sdk /Users/wangkeshijian/Documents/.myFolder/.system/.Downloads/Xcode14-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -target x86_64-apple-macos12.4 -module-cache-path /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -Xfrontend -serialize-debugging-options -enable-testing -swift-version 5 -enable-bare-slash-regex -I /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Products/Debug -F /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Products/Debug -emit-localized-strings -emit-localized-strings-path /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/Objects-normal/x86_64 -c -j4 -serialize-diagnostics -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-generated-files.hmap -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-own-target-headers.hmap -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-all-target-headers.hmap -Xcc -iquote -Xcc /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-project-headers.hmap -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Products/Debug/include -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/DerivedSources-normal/x86_64 -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/DerivedSources/x86_64 -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/DerivedSources -Xcc -DDEBUG=1 -working-directory /Users/wangkeshijian/Documents/.myFolder/.system/TerminalAdventure /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/Objects-normal/x86_64/ContentView.1.preview-thunk.swift -o /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/Objects-normal/x86_64/ContentView.1.preview-thunk.o -module-name TerminalAdventure_PreviewReplacement_ContentView_1 -Onone -Xfrontend -disable-modules-validate-system-headers -gline-tables-only
4
1
2.3k
Jul ’22
Issue with split(or at least I think so)
I'm trying to make an app that'll suggest a color based on a keyword the user inputs. I store prediction from the model to a [String: Double] array and then compute the output color. However, I'm stuck on two strange errors. Here's my code: extension Array where Element == [String: Double]{ func average() -> RGBList{ var returnValue = (r: 0, g: 0, b: 0) var r = 0 var g = 0 var b = 0 for key in self{ r = Int(key[0].split(",")[0]) * key[1] ... } ... } }
3
0
1.3k
May ’22
Settings not showing
I'm using SwiftUI on Mac Catalyst: @main struct TodoApp: App {     @StateObject private var eventData = TodoData()     var body: some Scene {         WindowGroup {             ContentView().environmentObject(eventData)         }         #if os(macOS)         Settings{             TodoSettings()         }         #endif     } }
3
0
399
May ’22
'self' used before all stored properties are initialized and I have not idea why
No idea why this error occurred, and I suspect that it's me putting initialization in the wrong place. struct TodosDocument: FileDocument { var todo: Context @AppStorage("defaultText") var defaultText: String = defaultContent var content: String var erroreousFileContent: Bool = false init(content: String = defaultContent, defaultText: String = defaultContent) { self.defaultText = defaultText self.content = content do { self.todo = try JSONDecoder().decode(Context.self, from: content.data(using: .utf8)!) } catch { print("ERROR \(error)") fatalError() } } static var readableContentTypes: [UTType] { [.TodoType] } init(configuration: ReadConfiguration) throws { guard let data = configuration.file.regularFileContents, let string = String(data: data, encoding: .utf8) else { throw CocoaError(.fileReadCorruptFile) } self.content = string do { self.todo = try JSONDecoder().decode(Context.self, from: data) } catch { self.todo = try JSONDecoder().decode(Context.self, from: defaultText.data(using: .utf8)!) // <- here } } func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper { let data = content.data(using: .utf8)! let i = JSONEncoder() i.outputFormatting = .prettyPrinted let content = Context(title: todo.title, items: todo.items) do { let dat = try i.encode(content) return .init(regularFileWithContents: dat) } catch { fatalError("Cannot write content \"\(data)\" with \(configuration): \(error)") } } }
3
0
1.2k
May ’22
ML
I've restarted my ColorProposer app and renamed it Chroma and the app suggests a color for a string. My main function is here: import Foundation import NaturalLanguage import CoreML func predict(for string: String) -> SingleColor? {     var model: MLModel     var predictor: NLModel     do {         model = try ChromaClassifier(configuration: .init()).model     } catch {         print("NIL MDL")         return ni     }     do {         predictor = try NLModel(mlModel: model)     } catch {         print("NIL PREDICT")         return nil     }     let colorKeys = predictor.predictedLabelHypotheses(for: string, maximumCount: 1) // set the maximumCount to 1...7     print(colorKeys)     var color: SingleColor = .init(red: 0, green: 0, blue: 0)     for i in colorKeys {         coor.morphing((ColorKeys.init(rawValue: i.key) ?? .white).toColor().percentage(of: i.value))         print(color)     }     return color } extension SingleColor {     mutating func morphing(_ color: SingleColor) {         self.blue += color.blue         self.green += color.green         self.red += color.red     }     func percentage(of percentage: Double) -> SingleColor {         return .init(red: slf.red * percentage, green: self.green * percentage, blue: self.blue * percentage)     } } struct SingleColor: Codable, Hashable, Identifiable {     var id: UUID {         get {             return .init()         }     }     var red: Double     var green: Double     var blue: Double     var color: Color {         get {             return Color(red: red / 255, green: green / 255, blue: blue / 255)         }     } } enum ColorKeys: String, CaseIterable {     case red = "RED"     case orange = "ORG"     case yellow = "YLW"     case green = "GRN"     case mint = "MNT"     case blue = "BLU"     case violet = "VLT"     case white = "WHT" } extension ColorKeys {     func toColor() -> SingleColor {         print(self)         switch self {         case .red:             return .init(red: 255, green: 0, blue: 0)         case .orange:             return .init(red: 255, green: 125, blue: 0)         case .yellow:             return .init(red: 255, green: 255, blue: 0)         case .green:             return .init(red: 0, green: 255, blue: 0)         case .mint:             return .init(red: 0, green: 255, blue: 255)         case .blue:             return .init(red: 0, green: 0, blue: 255)         case .violet:             return .init(red: 255, green: 0, blue: 255)         case .white:             return .init(red: 255, green: 255, blue: 255)         }     } } here's my view, quite simple: import SwiftUI import Combine struct ContentView: View {     @AppStorage("Text") var text: String = ""     let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()     @State var color: Color? = .white     var body: some View {       TextField("Text...", text: $text).padding().background(color).onReceive(timer) { _ in             color = predict(for: text)?.color             print(color)         }     } } But the problem of not updating the view still persists. In prints, I discovered a really strange issue: The line of print(colorKeys) is always the same.
3
0
1.2k
Aug ’22
SwiftUI Refreshing Mechanism
SwiftUI's Refreshing mechanism is driving me crazy. Say that I have this view: NavigationView { List($datasource) { $item in NavigationLink { SubView(item: $item) } label: { Text(item.someAttribute) } } } SubView: TextField("Placeholder", text: $item.someAttribute) And each time I edit the value in the SubView, the SwiftUI's navigation controller retreats the view to the home page(the initial view). Why?
3
0
1.3k
Sep ’22
Delayed Return in Swift
So I have an app that users can create utilities with some shell script I have a feature that the user can experiment with their scripts (a shell REPL) But if I type zsh in the REPL the whole app went stuck and the zsh shell outputs in Xcode: (This is my zsh theme) I've added detection for these: But what I really want is to let the process run in the background while the REPL output "Time out waiting for pipeline output after xxx secs" after xxx secs I've thought about letting them run in two separate asynchronous tasks so that if one task was completed it could first return the function but I just can't manage it: func run(launchPath: String? = nil, command: String) -> String { let task = Process() let pipe = Pipe() task.standardOutput = pipe task.standardError = pipe task.arguments = ["-c", command] task.launchPath = launchPath ?? "/bin/zsh/" task.launch() Task { let data = try? pipe.fileHandleForReading.readToEnd()! return String(data: data!, encoding: .utf8)! } DispatchQueue.main.asyncAfter(deadline: .now + 30) { return "Time out" // ERROR } } So How can I create two separate asynchronous tasks, one receiving the pipe output, and one, after a few seconds, returns the function?
3
0
1.9k
Sep ’22
Build input file not found
I know there should be a easy solution to this mess but I JUST CAN'T FIND IT AHHHHHHHHHH     cd ...... error: Build input file cannot be found: '/Users/xxx/Library/Developer/Xcode/DerivedData/ColorLibrary-hakdntgphjogwuawcvuxomfrjzex/Build/Products/Debug-iphonesimulator/ColorKit iOS.app/ColorKit iOS' (in target 'ColorKit iOS' from project 'ColorLibrary')
2
0
657
May ’22
sth wrong with .searchable
I add searchable to my SwiftUI List But the search TextField isn't showing Here's my code NavigationView { List(searchResults, id: \.self) { item in NavigationLink { LegendDetailView(item: item) } label: { HStack { Text(item.name).padding(1) Spacer() Image(systemName: "chevron.right").imageScale(.small) } } } }.searchable(text: $searchText)
2
1
879
May ’22