[ActionScript][ERROR]Left side of assignment operator must be variable or property - actionscript

I've been working on a Flash Register but I keep getting an error that says Line 170 and Line 174 Left side of assignment operator must be variable or property.
Here are the functions
function nextView()
{
this.__set__currentViewIndex(++this.__get__currentViewIndex()); //Line 170
}
function prevView()
{
this.__set__currentViewIndex(--this.__get__currentViewIndex()); //Line 174
}

Nvm I fixed it. this.__set__currentViewIndex(this.__get__currentViewIndex()+ 1); was the fix

Related

Stack of Plates: Cracking the coding interview

Stack of Plates: Imagine a (literal) stack of plates. If the stack gets too high, it might topple.
Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of several stacks and should create a new stack once the previous one exceeds capacity.
SetOfStacks. push () and SetOfStacks. pop() should behave identically to a single stack (that is, pop ( ) should return the same values as it would if there were just a single stack).
FOLLOW UP
Implement a function popAt (int index) which performs a pop operation on a specific substack.
The book solution for pop at index:
public int popAt(int index) {
21 return leftShift (index, true);
22 }
23
24 public int leftShift(int index, boolean removeTop) {
25 Stack stack = stacks.get(index);
26 int removed_item;
27 if (removeTop) removed_item = stack.pop();
28 else removed_item = stack.removeBottom( );
29 if (stack.isEmpty(» {
30 stacks.remove(index);
31 } else if (stacks.size() > index + 1) {
32 int v = leftShift(index + 1, false);
33 stack . push(v);
34 }
35 return removed_item;
36 }
the explanation of line 32 i nt v = leftShift(index + 1, false); is missing. Can anyone please help me with that?
Well, the leftShift function's objective is to pop from the stack given in the "index" input. After this, it does the adjustment between the other stacks.
So, in line 27 pops the element from the specified stack.
After this, in line 32, it calls itself recursively, giving this time as a first argument the index of the next stack, and as a second false. The first argument indicates that in the next recursive call, it will use the next stack. The second argument, being set as false, means that the required action (pop) has been done, and now only the adjustment is left.
The function when the flag removeTop is set as false does the adjustment as I referred above. The difference, in this case, is that instead of the top element it removes the bottom one from each of the following stacks. This way the needed adjustment is done.
With the term adjustment, I mean the elements that need to be moved from the next stacks to the previous ones each time so as to fill any empty spaces.
I hope it helps.

Rounding half to even

What's the formula for Banker's rounding in Swift language?
For example: 134.5675 becomes 134 and 135.5345 becomes 136
Thus far I've tried something like this:
extension Double {
func roundHalfToEven() -> Double {
return round(self * 100) / 100
}
}
But it's nowhere near returns what I need.
Remove multiplying and dividing by 100. Then use lrint (instead of round), which rounds the input to the nearest integer.

Is it possible to overload += operator in Swift?

Is it possible to overload += operator in Swift to accept for example CGFloat arguments? If so, how?
My approach (below) does not work.
infix operator += { associativity left precedence 140 }
public func +=(inout left: CGFloat, right: CGFloat) {
left = left + right
}
(Edit) Important:
The coding approch above actually works. Please see my answer below for explanation why I thought it did not.
I am sorry, my bad. The operator += does not need to be overloaded for CGFloat arguments as such overload is included in Swift. I was trying to do something like
let a: CGFloat = 1.5
a += CGFloat(2.1)
This failed because I cannot asign to let and the error displayed by XCode confused me.
And of course, approach like in my original question (below) works for overloading operators.
infix operator += { associativity left precedence 140 }
public func +=(inout left: CGFloat, right: CGFloat) {
left = left + right
}
Please feel free to vote to close this question.
The += operator for CGFloat is already available, so you just have to use it - the only thing you can do is override the existing operator if you want it to behave in a different way, but that's of course discouraged.
Moreover, the += operator itself already exists, so there is no need to declare it again with this line:
infix operator += { associativity left precedence 140 }
You should declare a new operator only if it's a brand new one, such as:
infix operator <^^> { associativity left precedence 140 }
However, if you want to overload the += operator for other type(s) for which it is not defined, this is the correct way:
func += (inout lhs: MyType, rhs: MyType) {
lhs = // Your implementation here
}

Swift function that takes in array giving error: '#lvalue $T24' is not identical to 'CGFloat'

So I'm writing a lowpass accelerometer function to moderate the jitters of the accelerometer. I have a CGFloat array to represent the data and i want to damp it with this function:
// Damps the gittery motion with a lowpass filter.
func lowPass(vector:[CGFloat]) -> [CGFloat]
{
let blend:CGFloat = 0.2
// Smoothens out the data input.
vector[0] = vector[0] * blend + lastVector[0] * (1 - blend)
vector[1] = vector[1] * blend + lastVector[1] * (1 - blend)
vector[2] = vector[2] * blend + lastVector[2] * (1 - blend)
// Sets the last vector to be the current one.
lastVector = vector
// Returns the lowpass vector.
return vector
}
In this case, lastVector is defined as follows up at the top of my program:
var lastVector:[CGFloat] = [0.0, 0.0, 0.0]
The three lines in the form vector[a] = ... give me the errors. Any ideas as to why i am getting this error?
That code seems to compile if you pass the array with the inout modifier:
func lowPass(inout vector:[CGFloat]) -> [CGFloat] {
...
}
I'm not sure whether that's a bug or not. Instinctively, if I pass an array to a function I expect to be able to modify it. If I pass with the inout modifier, I'd expect to be able to make the original variable to point to a new array - similar to what the & modifier does in C and C++.
Maybe the reason behind is that in Swift there are mutable and immutable arrays (and dictionaries). Without the inout it's considered immutable, hence the reason why it cannot be modified.
Addendum 1 - It's not a bug
#newacct says that's the intended behavior. After some research I agree with him. But even if not a bug I originally considered it wrong (read up to the end for conclusions).
If I have a class like this:
class WithProp {
var x : Int = 1
func SetX(newVal : Int) {
self.x = newVal
}
}
I can pass an instance of that class to a function, and the function can modify its internal state
var a = WithProp()
func Do1(p : WithProp) {
p.x = 5 // This works
p.SetX(10) // This works too
}
without having to pass the instance as inout.
I can use inout instead to make the a variable to point to another instance:
func Do2(inout p : WithProp) {
p = WithProp()
}
Do2(&a)
With that code, from within Do2 I make the p parameter (i.e. the a variable) point to a newly created instance of WithProp.
The same cannot be done with an array (and I presume a dictionary as well). To change its internal state (modify, add or remove an element) the inout modifier must be used. That was counterintuitive.
But everything gets clarified after reading this excerpt from the swift book:
Swift’s String, Array, and Dictionary types are implemented as structures. This means that strings, arrays, and dictionaries are copied when they are assigned to a new constant or variable, or when they are passed to a function or method.
So when passed to a func, it's not the original array, but a copy of it - Hence any change made to it (even if possible) wouldn't be done on the original array.
So, in the end, my original answer above is correct and the experienced behavior is not a bug
Many thanks to #newacct :)
Since Xcode 6 beta 3, modifying the contents of an Array is a mutating operation. You cannot modify a constant (i.e. let) Array; you can only modify a non-constant (i.e. var) Array.
Parameters to a function are constants by default. Therefore, you cannot modify the contents of vector since it is a constant. Like other parameters, there are two ways to be able to change a parameter:
Declare it var, in which case you can assign to it, but it is still passed by value, so any changes to the parameter has no effect on the calling scope.
Declare it inout, in which case the parameter is passed by reference, and any changes to the parameter is just like you made the changes on the variable in the calling scope.
You can see in the Swift standard library that all the functions that take an Array and mutate it, like sort(), take the Array as inout.
P.S. this is just like how arrays work in PHP by the way
Edit: The following worked for Xcode Beta 2. Apparently, the syntax and behavior of arrays has changed in Beta 3. You can no longer modify the contents of an array with subscripts if it is immutable (a parameter not declared inout or var):
Not valid with the most recent changes to the language
The only way I could get it to work in the play ground was change how you are declaring the arrays. I suggest trying this (works in playground):
import Cocoa
let lastVector: CGFloat[] = [0.0,0.0,0.0]
func lowPass(vector:CGFloat[]) -> CGFloat[] {
let blend: CGFloat = 0.2
vector[0] = vector[0] * blend + lastVector[0] * ( 1 - blend)
vector[1] = vector[1] * blend + lastVector[1] * ( 1 - blend)
vector[2] = vector[2] * blend + lastVector[2] * ( 1 - blend)
return vector
}
var test = lowPass([1.0,2.0,3.0]);
Mainly as a followup for future reference, #newacct's answer is the correct one. Since the original post showed a function that returns an array, the correct answer to this question is to tag the parameter with var:
func lowPass(var vector:[CGFloat]) -> [CGFloat] {
let blend:CGFloat = 0.2
// Smoothens out the data input.
vector[0] = vector[0] * blend + lastVector[0] * (1 - blend)
vector[1] = vector[1] * blend + lastVector[1] * (1 - blend)
vector[2] = vector[2] * blend + lastVector[2] * (1 - blend)
// Sets the last vector to be the current one.
lastVector = vector
// Returns the lowpass vector.
return vector
}

nsIEditActionListener and addEditActionListener

I'm trying to understand this code:
315 let existingIndex = this._editors.indexOf(editableNode.editor);
316 if (existingIndex == -1) {
317 let x = this._editors.length;
318 this._editors[x] = editableNode.editor;
319 this._stateListeners[x] = this._createStateListener();
320 this._editors[x].addEditActionListener(this);
321 this._editors[x].addDocumentStateListener(this._stateListeners[x]);
322 }
http://mxr.mozilla.org/mozilla-release/source/toolkit/modules/Finder.jsm#320
Especially addEditActionListener(this); what is this? MDN docs say it should be nsIEditActionListener but I can't find what this listener is comprised of MDN docs takes to broken page.
nsIEditor - MDN
this is the Finder object; it implements the nsIEditActionListener interface (http://mxr.mozilla.org/mozilla-release/source/toolkit/modules/Finder.jsm#395)
That interface is defined here: http://mxr.mozilla.org/mozilla-release/source/editor/idl/nsIEditActionListener.idl
So the code essentially attaches the Finder object to the editor, presumably so it can be notified of changes made in the editor at a later point.

Resources