Change variable value with what is in a function - swift

I have values in my function that I want to put into the labels that I have linked to my viewController. I tried to do currentTempLabel.text = result.main.temp but it did not want to do that in the function. So I moved that to the viewDidLoad and made some variables, var temp: String = "". In my function I have it set the variable to the value that I get from the API in that function but it doesn't set it to the value. When I run the app it just comes up with the default values that I put into it.
Right now I have the values printing to the console but I need the to go to the actual app and display. This is probably something very simple but I just can't figure it out.
In the function I have:
self.temp = String(result.main.temp)
And in viewDidLoad I have:
currentTempLabel.text = temp
In my mind this should work but not in Swift's mind.

The API to get the weather data works asynchronously. Assign the value to the label in the completion handler on the main thread
DispatchQueue.main.async {
self.currentTempLabel.text = String(result.main.temp)
}

Related

I am passing two query result inside view but only one result work.please give me a solution

I am passing two result inside view but only one result is working.
public function editCustomer(){
$custId = $this->uri->segment(3);
$desResult['designation'] = $this->ecd->hrmDesignationId();
$result['editDetails'] = $this->ecd->editDetails($custId);
$this->load->view('editCustomerDetails',$result,$desResult);
}
The second argument of the $this->load->view() method should be an array.
The proper way to do this is to build your array before calling the load method:
$result['designation'] = $this->ecd->hrmDesignationId();
$result['editDetails'] = $this->ecd->editDetails($custId);
$this->load->view('editCustomerDetails',$result);
The two variables will then be available in your view. You can write them down to check :
var_dump($designation);
var_dump($editDetails);

One way data binding not working when using date pipe

I'm using a date object to keep track of the current date in an application.
In my view I have a one way binding like this:
<h3>{{ currentDate | date }}</h3>
And in the component, I have functions to change this date, like this:
previousMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() - 1);
}
nextMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() + 1);
}
But when these functions are triggered, the currentDate value doesn't update on the view.
I made sure the date object is being updated, just not on the view.
Whenever I remove the date pipe, it works.
Anyone has any idea how to fix this?
Thanks!
The value is not updating in the view because the pipes in angular are so called pure (or, stateless) by default. That means that the input will not be re-evaluated if the input object changes, but only if it's replaced.
From the documentation (see section Pure and Impure pipes):
Angular executes a pure pipe only when it detects a pure change to the
input value. A pure change is either a change to a primitive input
value (String, Number, Boolean, Symbol) or a changed object reference
(Date, Array, Function, Object).
Try the following code instead:
previousMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() - 1);
this.currentDate = new Date(this.currentDate);
}
nextMonth(){
this.currentDate.setMonth(this.currentDate.getMonth() + 1);
this.currentDate = new Date(this.currentDate);
}

Slow swift code after dynamically calling class method

I'm trying to write a Swift class (in Playground) that takes a dictionary of strings (such as PixelGreyScale) and applies each sequentially to each pixel in an image.
I have it working with a dictionary of class methods, but it's very slow and I can't pass parameters.
In the code below, my class is instantiated and then the Process method is run, taking in a dictionary of methods (a technique I saw on SO - is there a better way to dynamically run a method from a string variable?):
// Create an instance of the class, passing in the raw image
let p = ImageProcessor(src: rawImage);
// use a dictionary of method calls to queue up the process
let stack = [
{ p.PixelGreyScale() }
]
// run the process
p.Process(stack)
The Process method calls each method in the stack and sets the current pixel to the return value:
func Process(stack: [() -> Pixel]) {
for y in 0..<rgba.height {
for x in 0..<rgba.width {
let index = y * rgba.width + x
currentIndex = index
rgba.pixels[index] = stack[0]()
}
}
}
This code runs, but I'm clearly not doing it right! I'm having the following problems:
It takes an age to run, unless the stack dictionary is empty. Removing the stack[0]() call doesn't speed things up - it seems that having something in the stack causes a slowdown when getting the pixel at a given index.
Because of how I'm using the stack dictionary to call methods, I can't pass parameters and have to resort to moving things into and out of class variables. I've tried using performSelector but got exceptions every time I tried to use parameters.
Is there a way of passing in a dictionary of strings and using this to control which methods are executed, with parameters and without speed issues?

Swift - Detect when variable goes past a point

I've searched on Stack Overflow and of course Google and nothing has come up (which surprises me).
I have an integer variable which constantly changes, and I want to run a certain function when the variable goes past a certain number. Infinite while loops will obviously just slow the phone down and possibly crash.
This variable is the acceleration variable from CMMotionManager, which gives the accelerometer data when displayed/printed. It is NOT a variable set by me.
For example:
if Example > 2 { // Example being the integer variable from another module
// do stuff here
}
Edit: No, waiting a few seconds and then checking again will NOT work in my situation.
Edit2: It is not a variable set by me, but a variable from another module.
Have you checked willSet and didSet options?
var _test : Int = 0 {
//First this
willSet {
println("Old value is \(_test), new value is \(newValue)")
}
//value is set
//Finaly this
didSet {
println("Old value is \(oldValue), new value is \(_test)")
}
}
More details here.

variable scope when dealing with event handlers

function World:draw()
--draw the tiles based on 2d int array
--draw the player
--draw the monsters
--show what you need to based on camera
self.map[0][0]=display.newImage("dirt_tile.png",i,j)
end
I can't access any of my world object's properties in there when I use the event handler:
Runtime:addEventListener("enterFrame",World.draw)
Is there a different kind of eventListener I can use, or is there a different way to instantiate the eventListener so the self-referencing context stays intact?
Here you go:
World = { count=0 }
function World:enterFrame()
self.count = self.count + 1
print("count = " .. self.count)
end
Runtime:addEventListener("enterFrame", World)
See this API reference page for details.
In you named your table local self = {}, then it may collide with the implicit self parameter.
The function World:draw() is just a Lua syntax sugar for World.draw = function(self). By working with self, you are using whatever first parameter the runtime event handler is passing to your function (judging from the API, it passes event, which you will get as self).
Try naming the table you want to access differently (like local this) and see if it works.