How to show Alert when I encountered API request error in SwiftUI via ViewModel? [closed] - swift

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Below is the NewsViewModel. What property should I add in this viewModel to trigger an Alert in its corresponding View? Thanks in advance.

I would create an enum type for this. In your view model, I might add something like:
enum Error {
case noNetwork
case noNewsFound
case missingImages
var alert: Alert {
switch self {
// Build SwiftUI.Alert instances for each error case
}
}
}
I then would add this property to your view model:
#Published var error: Error?
Lastly, in your view, I would add this:
// Your view here
.alert(item: $viewModel.error) { error in
error.alert // This returns the computed Alert you added to your Error type
}
Then in your view model, you just set the error property to the appropriate error, and your view will show the alert.

Related

How to fix button clicked not updating but others do [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm making a math game. Every time I click a button a new question should appear with different results to choose from.
However every time I click the button which I think is the result it doesn't update the text but the others do.
See github: https://github.com/Combii/BrainTrainerSwift
Use UIButton.setTitle(, for:) method. I suggest you to change code in ViewController.swift:
func setNumbers(numberDic: Dictionary<String, Int>) {
let btns = [bt1, bt2, bt3, bt4]
UIView.performWithoutAnimation {
for (position, number) in numberDic {
btns[(Int(position) ?? 0) - 1]?.setTitle(String(number), for: .normal)
}
}
}

How do I update this variable with data [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
New programmer here! I'm working with a network request and below is a snippet of my code. After I complete the network request I want to store a value from the request into a variable. However, when the variable is called outside of the if let statement it is NOT updated. Essentially, the print statement inside the if let statement prints the value I'm looking for, but the print statement outside of the if let statement prints the default value I set for that variable. The variable is global. I need to use that updated variable elsewhere in my code, any ideas?
if let data = data {
self.nameLabel.text = data.name
person = data.name
print(person)
}
print(person)
First of all you didn't showed complete code so I am guessing you need main queue to update your UILabel first as shown in below code:
DispatchQueue.main.async {
self.nameLabel.text = data.name
}
next thing is if you want to access the value somewhere else once your request completes and UILabel updated then you can get the value from it this way:
let value = self.nameLabel.text!
And if you want to access the value once request complete then you can use closure and HERE you can check example of it.

App crashes on button press signal:Sigbart

I'm trying to build city guide app from template but I'm unable to switch from sections view to just map view. As I understand this error means that code isn't quite finished and its missing something. Would gladly appreciate if anyone could help me with this one.
https://i.stack.imgur.com/cUCMO.png
As the debugger console has indicated that:
You're trying to cast value of type appyCity.SelectionsViewController to appyCity.MapPoiViewController. It's impossible => Crash
If you're not sure about the value of a property, let's use the Optional Chaining and Optional Binding instead of Forced Unwrapping To minimize the possibility of a crash.
Your code should look like this:
if let MPVC = segue.destination as? MapPoiViewController {
// MPVC is MapPoiViewController type
} else {
// MPVC isn't MapPoiViewController type
}
See more information here.
Hope this helps you!

In Unity know which button is clicked [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have two buttons create and join. Both button's on click event I move to next scene, but in next scene I have two scripts networkmanager and networkclient.
Now if I press create button I want to call only networkmanger not networkclient. So how can I disable the networkclient script and join I want to disabled networkmanger?
How can I know which button is been pressed?
public void OnPress(){
string name = this.gameObject.name;
switch(name){
case "Create":break;
case "Join":break;
}
}
You can read the name of the object the script is attached to.
If this is not suitable for any given reason (both scripts are on same object), you can pass a parameter to the method.
public void OnPress(string name){
switch(name){
case "Create":break;
case "Join":break;
}
}
then you give the name in the inspector.
The final way is to use two different methods for each button.

Forcing update to placeholder complication on Apple Watch

Here a simple issue I am facing while starting to experiment with WatchKit and complications.
I created a simple app which is showing a complication with a public string “Y” and by clicking it, the Apple Watch app is shown with a simple switch.
I wrote my code in getPlaceholderTemplateForComplication in ComplicationController.swift and added a switch IBAction in InterfaceController.swift.
By changing the value of the switch, the public string cycles between “N” and “Y”. I would like to have it changed in the complication as well. However I am noticing that the complication stays as it is initially at “Y”.
I found a similar question about forcing complication updates, but it was related to a TimeLine complication func and not placeHolder.
func updateComplication() {
let complicationServer = CLKComplicationServer.sharedInstance()
for complication in complicationServer.activeComplications {
complicationServer.reloadTimelineForComplication(complication)
}
}
It is not clear to me on where and how to use this in my case.
As suggested I worked on getCurrentTimelineEntryForComplication .
In order to test a ModularComplication only, I used:
switch complication.family {
case .ModularSmall:
let modularSmallTemplate =
CLKComplicationTemplateModularSmallRingText()
modularSmallTemplate.textProvider =
CLKSimpleTextProvider(text: stringa)
modularSmallTemplate.fillFraction = 0.95
modularSmallTemplate.ringStyle = CLKComplicationRingStyle.Closed
let template = CLKComplicationTimelineEntry(
date: NSDate(), complicationTemplate: modularSmallTemplate)
handler(template)
default:
handler(nil)
}
I have the switch IBAction in InterfaceController.swift.
I am encountering issues in using:
func updateComplication() {
let complicationServer = CLKComplicationServer.sharedInstance()
for complication in complicationServer.activeComplications {
complicationServer.reloadTimelineForComplication(complication)
}
}
Where do I have to write the above mentioned func, in order to be able to call it from inside the IBAction?
If I write it in ComplicationController.swift, by calling it from the IBAction in InterfaceController.swift as:
ComplicationController.updateComplication()
I get the error “Missing argument for parameter #1 in call”,
while if I write it in InterfaceController and call it in the IBAction, although the build is successful, when running the App and changing the value of the switch I get the following error: “fatal error: unexpectedly found nil while unwrapping an Optional value” on the line:
for complication in complicationServer.activeComplications
of func updateComplication.
It's not the placeholder text that you want to update. The placeholder template is a static template that's displayed in the selection screen for your complication while you are customizing your watch face. It's only called once to get the placeholder text, and doesn't get called when a timeline is reloaded.
The complication's current (actual, live) timeline entry is provided by getCurrentTimelineEntryForComplication. This function is where you should use your model's on state of the switch to create a CLKTextProvider containing a "N" or "Y" for the active complication family.
Finally, you should call updateComplication() from your switch IBAction to reload your complication timeline, which would update the current entry to show the new state of the switch on the watch face.
Since your complication is updated manually, you should return a nil updateDate in getNextRequestedUpdateDateWithHandler to avoid scheduling your complication for any regular updates.
Update for your edited question:
You should include the updateComplication function in your interface controller, as your complication controller isn't meant to be instantiated by you.
In regard to the "Unexpectedly found nil while unwrapping an Optional value" error, this is an issue that has been mentioned on the Apple Developer Forums.
If you are already running watchOS 2.1, you could try the latest watchOS beta to see if the issue has been fixed yet for 2.2. You should also file a bug report and dupe radar 22947535.