do something when counter = x - iphone

I have a counter that make counter++ every time one image touches another image.
Now what I want to do is: if counter=2; do something, but I always get an error:
Assignment makes pointer from integer without a cast
Here is a part of the code:
-(void)checkcollision {
if(CGRectIntersectsRect(flakeImage.frame, viewToRotate.frame)) {
counter++;
}
}
-(void)checknumber {
if(counter=2) {
viewToRotate.alpha=0;
}
}

Are you perhaps doing this:
if (counter = 2) {
// Do something.
}
This is a common error in if statements. The correction would be:
if (counter == 2) { // Note the "==", instead of "="
// Do something.
}
This is just a guess though - I would need to see some more information about the error, or about what you want to do.
EDIT
Ah - have seen your newly posted code, confirming what I stated above. Your code reads that you are trying to assign the value '2' to counter in the if statement. You want the == to make this a check for equality.

Related

How to read an array with BsonReader in MongoDB?

Basically, I have a data set where a document can contain an array of variable length. I'm writing a codec, so all I have is BsonReader. The problem is readStartArray() returns void rather than returning, for example, the number of elements. Also, I don't see any methods that allow one to test when the end of the array has been reached (apart from trying readEndArray() and catching the exception).
Any ideas?
I found the documentation for custom serialization to be lacking too, but you just need to work with the Reader.State property. Once you've identified the start of the array you need to iterate over each of the elements until state == BsonReaderState.EndOfArray. You need to ensure that you only call ReadBsonType when the state is "type".
The following example deserializes an array of documents:
context.Reader.ReadStartArray();
bool ctn = true;
do
{
//If in "type" state we can call ReadBsonType. The order is important.
if (context.Reader.State == BsonReaderState.Type)
{
context.Reader.ReadBsonType();
}
//Now that we're in a value state we can read the value safely
if (context.Reader.State == BsonReaderState.Value)
{
if (context.Reader.CurrentBsonType == BsonType.Document)
{
//Handle the document here. In this case pass I it to a private method
DeserializeChild(context, args, node);
}
//state will now be "type", so continue do loop and recheck type
continue;
}
if ( BsonReaderState.EndOfArray == BsonReaderState.EndOfArray)
{
context.Reader.ReadEndArray();
ctn = false;
}
} while (ctn);
Ah, it's done with:
while (reader.readBsonType() == BsonType.DOCUMENT) {
reader.readStartDocument();
// ...
reader.readEndDocument();
}
(it's an array of documents in this case, I'm still new to BSON so I don't know how it would work for an array of values).

Swift variable inside variable for iteration

I have very redundant code that I need to simplify as I am expanding and I am not sure how to do this. I have a lot of buttons that get updated and images changed quite often. Depending on what button is lit up and what is going on in the program, I change the state of a lot of buttons at once. I would like to create a function that can iterate multiple parts of the function. . .for example, below is a chunk of code that is repeated 1 time per button that I have, which at this point is going to be 48. . .
if sharedData.Channel1Mute != nil {
if sharedData.Channel1Mute == "True" {
self.Channel1Button.setImage(UIImage(named: "w1"), for:UIControlState())
}
else {
self.Channel1Button.setImage(UIImage(named: "g1"), for:UIControlState())
}
if sharedData.Channel2Mute != nil {
if sharedData.Channel2Mute == "True" {
self.Channel2Button.setImage(UIImage(named: "w2"), for:UIControlState())
}
else {
self.Channel2Button.setImage(UIImage(named: "g2"), for:UIControlState())
}
...
...
ect
...
This isn't correct coding but I would like this to function something like this which would be MUCH shorter and scalable . . .
for NUMBER in 1...48 {
if sharedData.Channel[NUMBER]Mute != nil {
if sharedData.Channel[NUMBER]Mute == "True" {
self.Channel[NUMBER]Button.setImage(UIImage(named: "w[NUMBER]"), for:UIControlState())
}
else {
self.Channel[NUMBER]Button.setImage(UIImage(named: "g[NUMBER]"), for:UIControlState())
}
}
This is frustrating because my current code does the exact same thing like 48 times but I have to rewrite the whole code again with each number in the if statement in each copy.

What is the most efficient way to call another function in parent function to execute Palindrome function?

In programming, there are multiple ways of doing the same problem. The following problem is in regards to palindrome. Though I feel that I am on the right track, I am not able to completely solve the problem to get to requested solution.
What is a palindrome? A word written forward or backward is the same and returns true. Example, "racecar". Hence, I designed the following code in Javascript...
function palindrome(string) {
string = string.toLowerCase();
lowString = string.toLowerCase().split("").reverse().join("");
for (var i=0; i<string.length; i++) {
if (string[i] !== lowString[i]) {
return false;
}
}
}
return true;
}
The above code returns true if Palindrome exists and returns false if not.
Then, the problem says - Given various palindrome in a string or array, please return the longest palindrome. So, I wrote the following:
function longestPalindrome(newstring) {
splitString = newString.split(" ");
for (var i=0; i < splitString; i++) {
if (splitString[i] == palindrome(splitString[i]) {
console.log(splitString[i]);
}
}
}
longestPalindrome("This is a racecar ada");'
But in the above code, I am not able to get the required outcome because I believe I am not calling the function correctly.
I would appreciate clear directions or even a solution built off of my track as well as the track you deem fittest.

avoid Number Format Exception while parseing to integer

I'm trying to check if the text area has only numbers or not, but i got this problem Number Format Exception while clicking on the Edit Button, any Ideas how to solve it.
ArrayList<CarRental> List= CarRent.getList();
String text=EditTF.getText().trim();
char [] txt=text.toCharArray();
Character a=null;
boolean isnotDigit=false;
int index;
for(int i=0;i<txt.length;i++)
{
if(!a.isDigit(txt[i]))
{
isnotDigit=true;
break;
}
else
{
isnotDigit=false;
continue;
}
}
if(isnotDigit==false)
{
index=Integer.parseInt(text.trim());
PrintList_Summary.setIndex(index);
EditDetails.nameTF.setText(List.get(index).getName());
EditDetails.sizeCOB.setSelectedItem(List.get(index).getSize());
EditDetails.daysTF.setText(List.get(index).getDays()+"");
if(List.get(index).getCarType().equalsIgnoreCase("Luxury"))
{
EditDetails.LuxRB.setSelected(true);
}
else if(List.get(index).CarType().equalsIgnoreCase("Truck"))
{
EditDetails.truckRB.setSelected(true);
}
if(List.get(index).getDriver())
{
EditDetails.yesRB.setSelected(true);
}
else
{
EditDetails.noRB.setSelected(true);
}
EditDetails.Frame.setVisible(true);
}
else
{
JOptionPane warning=new JOptionPane();
warning.showMessageDialog(null,"Element Index CAN ONLY be an INTEGER.","Invalid Index",WIDTH);
}
Use try catch to handle exceptions.
try{
index = Integer.parseInt(text.trim());
//etc
}catch(NumberFormatException ex){
//whatever happens when the exception is thrown (not an integer)
}
You can use that instead of the if(!a.isDigit(txt[i])) for. If it can't be parsed, then it will go intro the catch.
Also, you can use !isnotDigit instead of isnotDigit == false.
I would use String.matches() for your check instead of doing it yourself. Scrap the for-loop and change your if-condtion from isnotDigit==false to text.matches("[0-9]+"). After this Integer.parseInt(text) should definitly work.
This uses a regular expression to check if your string consists only of digits and at least one digit. For further reference on regular expression see the javadoc for Pattern.

Why isn't my score recording on the playerprefs is there GetFloat method?

Why isn't my score recording on the playerprefs ? is there GetFloat method? Can anyone help me to post the least score of my game it just like the most least seconds will get the best time record ever
var myTimer: float = 0;
var GUITimer: GUIText;
function Start() {
}
function Update() {
GUITimer.text = "Time: " + myTimer;
if (myTimer > -1) {
myTimer += Time.deltaTime;
}
}
function OnTriggerEnter(other: Collider) {
if (other.tag == "FinishLine") {
SaveTime();
}
}
function OnGUI() {
GUI.Label(Rect(10, 10, 500, 200), myTimer.ToString());
}
function SaveTime() {
if (myTimer < PlayerPrefs.GetInt("JeepneyScore3")) {
PlayerPrefs.SetInt("JeepneyScore3", myTimer);
}
Application.LoadLevel("Levels");
}
I see three problems:
First, you're tracking a float, but calling GetInt() and SetInt(). Keep your data types consistent. You should either round/floor/etc, or call GetFloat() and SetFloat().
Second, you're not calling Save(), which means your changes will never write to disk. You might consider something like this:
PlayerPrefs.SetFloat("JeepneyScore3", myTimer);
PlayerPrefs.Save();
Third, you're not handling the case where no data exists. You could check for existing data with HasKey(), but in this case it's simpler to rely on the second form of GetFloat(). The default form, which you're calling, returns zero if the requested key isn't set:
//returns value of "foo", or zero if no such value
var prevBest = PlayerPrefs.GetFloat("foo");
If you're looking for a time below the player's previous best, but the default "best" is already 0.0, you're going to have a hard time beating that time.
You can instead provide your own default value:
//returns value of "foo", or ten thousand if no such value
var prevBest = PlayerPrefs.GetFloat("foo", 10000.0);
Finally, you should make sure that SaveTime() is actually being called at the appropriate time. You could add a simple debug line, like this:
Debug.Log("SaveTime was called");
And then make sure that's showing up. If not, you need to fix your collision check.