As shown above, in the Xcode playground, I am trying to extend the protocol Exercise to have two more computed properties, caloriesBurnedPerMinute and description. When I want to run this part of code in the playground, the "play button" at the extreme bottom left corner appears as grey, indicating that it cannot run the code until line 20. However, if I just want to run the code until line 14, as shown below, the "play button" appears as blue, indicating that it can run the code until line 14.
May I know if there is anything wrong with the second extension of protocol Exercise?
edited: the following is the code.
import Cocoa
protocol Exercise: CustomStringConvertible {
var name: String { get }
var caloriesBurned: Double { get }
var minutes: Double { get }
}
extension Exercise {
var caloriesBurnedPerMinute: Double {
return caloriesBurned / minutes
}
}
extension Exercise {
var description: String {
return "Exercise(\(name), burned \(caloriesBurned) calories in \(minutes) minutes)"
}
}
There is nothing wrong with your code, That is just Xcode 10's new feature. See here.
It's a way of running your code line by line, but there are some limitations. If you hover over the line number "20", you will see a grey line:
That means Xcode can't only run that part of the code, presumably because how Xcode handles extension declarations.
Just add some code that actually runs, as opposed to just declarations, and press the play button above the bottom panel. Your code will run just fine.
You need to wrap the Double variables in a String cast like String(caloriesBurned):
extension Exercise {
var description: String {
return "Exercise(\(name), burned \(String(caloriesBurned)) calories in \(String(minutes)) minutes)"
}
}
I don't know why, but it helps
Related
I am writing a Focus system extension (sandboxed, Cocoa, entitlements set) for macOS 13/Ventura using Xcode 14.2
I have the extension loading it's UI into the macOS system settings > Focus pane.
so here are the issues:
Even though it is loaded, it doesn't seem to ever run the perform() function when the UI is changed by the user or the user invokes Focus > Do Not Disturb.
What can be done in the perform() function? Like, what is supposed to go there? Nothing seems to work.
import AppIntents
struct MacOSFocus: SetFocusFilterIntent {
static var title: LocalizedStringResource {
return "Focus Settings"
}
// The description as it appears in the Settings app
static var description: LocalizedStringResource? = "Focus Settings" // name under Minus icon in options list
// How a configured filter appears on the Focus details screen
var displayRepresentation: DisplayRepresentation {
return DisplayRepresentation(stringLiteral: "Focus Settings") // name under filter once added to Foucs
}
#Parameter(title: "Show Task Bar", default: false)
var showDefaultTaskBar: Bool
#Parameter(title: "Start Timer")
var startTimer: Bool
func perform() async throws -> some IntentResult {
// This doesnt seem to run
// What can I put here?
// I need to write string data to a text file somewhere or communicate with the host app in some way.
return .result()
}
}
Just trying to get unstuck. Thanks for any help.
Tried adding an NSLog() call in the perform() function for debugging. Even tried using NSSound.beep() just to check that it is getting called. Didn't work.
I have an array containing some data that I want to present in a View. It should be possible to rearrange the data by holding and dragging it to its new position. I have used the .onMove modifier on a List generated using ForEach to iterate over every element in the list. My code works exactly as expected, but throws the following error/warning:
2022-10-12 18:48:52.833139+0200 Playground[1212:207280]
[EventDispatcher] Found no UIEvent for backing event of type: 11;
contextId: 0xFEEA6094
This only happens when using an actual device (iPhone 12 Pro Max on iOS 16.0.2), not when using the simulator (Xcode Version 14.0.1 (14A400), build in simulator of any device that runs iOS 16).
The code (the whole project is way bigger, but this is enough to trigger the error/warning):
struct ContentView: View {
#State var stuffList = ["Beer", "Chips", "Boardgames"]
var body: some View {
NavigationStack {
List {
ForEach(stuffList, id: \.self) { stuff in
Text(stuff)
}
.onMove { stuffList.move(fromOffsets: $0, toOffset: $1) }
}
}
}
}
Edit:
I have now tried different code snippets from online tutorials, e.g. this one (Hacking in Swift) and I still get this message no matter what I try. As my code seems to work as intended, I will continue my project and treat this as "log noise". If someone knows more about this than I do or simply can confirm having the same issue, it would be highly appreciated.
SwiftUI View file
struct NoteListView: View {
#EnvironmentObject var presenter: NoteListPresenter
var body: some View {
NavigationView{
AddNoteButton()
Text(presenter.noteViewModels[0].title)
}
}
}
struct NoteListView_Previews: PreviewProvider {
static var previews: some View {
NoteListView()
}
}
I first got this error when I finished writing the logic and started to writing Views. After add my custom button AddNoteButton() to main view, it gave the following error, although it worked before I added this button. I tried to rewrite my views, but although it works at the beginning at some random point of time it throws this error again(by other words sometimes it works, but when i add some irrelevant code it stops working and discarding changes doesn't solve the problem). It always highlights line, where I use noteViewModels array inside of the NoteListView.
Also I added method which prints array to presenter and it always prints everything.
func printAll(){
for element in self.noteViewModels{
print(element.title)
}
}
I can not add other files to question, because there is too many of them. But i hope that I gave enough information, If you think that there is not enough information you are free to ask me or you can check out github:
https://github.com/ShynggysSaparbek/ViperNotesWithRealm/
Xcode version 11.3
Mac OS version Catalina 10.15.2
This is my first project in Swift so please bear with me.
punkteLimit should be initialized with value 30. The value of the variable as well as a label should be updated everytime a slider value is changed.
var punkteLimit: Int = 30
#IBAction func sliderPunktelimitChanged(_ value: Float) {
punkteLimit = Int(value)
labelPunktelimit.setText("Punkte-Limit: \(punkteLimit)")
}
This seems to work fine. The label updates correctly, i.e. when I change the slider to 28, it says "Punkte-Limit: 28". However, punkteLimit is stuck at the initial value of 30 in the following part (the same is true for considerPunktelimit but the solution should be identical). The haptic feedback will be triggered at gesamtPunkte == 30 regardless of the changes above.
I use a button that performs gesamtPunkte += 1 to adjust the value, if it matters.
var gesamtPunkte: Int = 0 {
didSet {
if gesamtPunkte == punkteLimit && considerPunktelimit == true {
WKInterfaceDevice.current().play(WKHapticType.stop)
}
...
}
}
I'm not exactly sure where to go from here.
Help is much appreciated.
I solved the problem by moving the variable(s) in question outside of the InterfaceController class (making them global?). I have read mixed opinion about this approach, if anyone wants to comment on why it didn't work inside of the class and whether or not there are problems with making them global, feel free to.
I am trying to implement Speech-to-text feature for watchkit app.
I referred this question which has sample code.
Following is the code I tried:
self.presentTextInputControllerWithSuggestions(["Start it", "Stop it"], allowedInputMode: .Plain, completion: { (selectedAnswers) -> Void in
if selectedAnswers.count > 0 {
if let spokenReply = selectedAnswers[0] as? String {
self.label.setText("\(spokenReply)")
}
}
})
label is a label to display text I speak.
When I run it, it shows the screen where you are supposed to speak (Siri kind of screen) and you have two options on top: ‘Cancel', and ‘Done'. Once I am done speaking, I tap on ‘Done’ but screen doesn’t go away or shows me initial screen, I always have to tap on ‘Cancel’ to go back, and I don’t get any speech data in form of text. I checked it and seems like selectedAnswers is always an empty array, unless I tap on the "Start it"/"Stop it" options.
Can anyone help me with this? I want to show the spoken message on label. I have code inside awakeWithContext method in InterfaceController.swift file, am I supposed to put it somewhere else?
I am using iPhone with iOS 9 beta 2 and watchOS 2 beta on AppleWatch.
Thanks!
You can ask for user input and give him suggestion (see Swift example bellow).
self.presentTextInputControllerWithSuggestions(["suggestion 1", "suggestion 2"] allowedInputMode: .Plain, completion: { (answers) -> Void in
if answers && answers.count > 0 {
if let answer = answers[0] as? String {
println("\answer")
}
}
})
If suggestion is nil it goes directly to dictation. It is not working on the simulator but it is on real watch.
Your approach is correct but something is wrong with your SIRI , try changing the language.
It should work like these.