notation problem - iphone

When i typed [lat1 = newLocation.coordinate.latitude]; its telling expected : before ] token like that. whats my fault? as i am new to this domain., please ayone guide me.
Thanks In Prior....

If you are trying to compare lat1 and newLocation.coordinate.latitude, the correct statement would be:
if (lat1 == newLocation.coordinate.latitude) {
// do something here
}
If you are trying to assign the value of newLocation.coordinate.latitude into lat1, the correct statement is:
lat1 = newLocation.coordinate.latitude;
If you are trying to do the first thing and the compared variables are floating point numbers, then you probably want to check if they are close enough instead of equality:
if (fabs(lat1 - newLocation.coordinate.latitude) < someLittleDistance) {
// close enough
}
…where of course you will have to define someLittleDistance.

Try the following..
Remove the [] brackets from your line it must be
lat1 = newLocation.coordinate.latitude;

When xCode behaves like this, it probably wants to say that something is the method or it thinks of something like a method. Dot-notation in Objective-C as usual is some kind of equivalent of the setter. For example
ObjectA.property1 = value;
is equivalent of
[ObjectA setProperty1:value];
And in the last case, xCode expects to see : after the setter call and a value after the column.

Related

formatWithString: Cocos2d-x passing in a CCString

Maybe i'm going about this wrong, and if i am please tell me because i just dont know any better.
But i am trying to pass in a CCString into the following format, and not having any luck. Could someone please tell me what the parameter for strings would be in C++ when passing them into another string?
Code:
CCString tilt = "";
if (recalculatedFrames >= 4 && (numberOfTimesRun > 0 && numberOfTimesRun < recalculatedFrames - 1)) {
tilt = "TR_";
}
initalTurnAnimationFrames->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(CCString::stringWithFormat("%s%d.png", tilt, i)));
Also is it fine to make a blank string like i am doing with tilt?
I think tilt should be class of string
And the CCString part should be
CCString::createWithFormat("%s%d.png", tilt.c_str(), i);
try this
CCString::stringWithFormat("%s%d.png", tilt.getCString(), i)

UITextField strange behaviour for blank checking

I've a UITextField say txtTitle. I want to check for not blank of that field at the time of inserting data into database.
For that I written
if(![txtTitle.text isEqualToString:#""])
{
//Save
}
But where I am shocked is its not working! I did these type of checking before and its working properly. But not with this case. So that I checking it using following,
if(txtTitle.text!=NULL)
{
//Save
}
It's working properly.
Now here I am confusing about this. I used to print NSLog(#"%#",txtTitle.text) without inputting anything into it. Its printed (null).
Someone please justify the difference between two IF conditions.
Thanks
Maybe you can check for the length property of the string instead, using
if([txtTitle.text length] > 0){
// Save
}
I think the difference is between a completely uninitialized string and a string that has been initialized, but is simply empty.
Hope this helps
#Hemang
As you have mentioned that NSLog gives you (null)..you have to compare like
[txtTitle.text isEqualToString:#"(null)"]
other wise use
if([txtTitle.text length] > 0)
{
}

How to know if a float got a decimals

I'm building a simple calculator for a classroom, this is the code to show the result:
- (void)doTheMathForPlus:(float)val {
float conto = self.contBox + val;
self.myDisplay.text = [NSString stringWithFormat:#"%.02f", conto];
}
I need to know if "conto" have decimals (to change the format of the string in a if statement)
how to do it?
thanks
if (conto == (int)conto) {
}
Hope this helps.
you'd better wrap that with #define and put this line in your .pch file
#define HAS_DECIMALS(x) (x!=(int)x)
than use anywhere in the code:
if (HAS_DECIMALS(conto)) {
//number has decimals
} else {
//number does not have decimals
}
Yes, dredful is right with his answer.
And, You also can use different functions like floor, ceil, round. In You case better "floor(conto)".
But, you can't do like Kyr Dunenkoff suggested. becouse, all operands for"%" should be integer, but not float as "conto".

Having a problem with simple bool

I've some really simple code that checks if my bool is == YES but it does not ever enter.
NSLog(#"boool %d",self.arrayAlreadyPopulated );
if (self.arrayAlreadyPopulated == YES)
{
Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:(numMatchCounter)];
aMatch.teamName1 = TeamNameHolder;
}
else
{
Match *aMatch = [[Match alloc] init];
aMatch.teamName1 = TeamNameHolder;
[appDelegate.matchScoresArray addObject:aMatch];
[aMatch release];
}
The debug at the top says that the value of self.arrayAlreadyPopulated is 1 on the 2nd pass as it should be.
But it never enters the first first part but jumps down to the 'else'
I cant see for the life of me what the problem is. -.-
Anybody able to clue me in?
Thanks
-Code
EDIT declaration code
BOOL arrayAlreadyPopulated;
#property (nonatomic) BOOL arrayAlreadyPopulated;
#synthesize arrayAlreadyPopulated;
Don't compare a BOOL against YES or NO. They can carry values that are not NO but don't compare equal to YES. Instead, use the boolean expression directly in the if statement:
if (self.arrayAlreadyPopulated)
{
// ...
}
arrayAlreadyPopulated is probably not actually a BOOL. If, for example, it was a float, the %d would still print 1.
Check and double check that you're assigning the value to arrayAlreadyPopulated always as self.arrayAlreadyPopulated = YES instead of just arrayAlreadyPopulated = YES.
Sometimes, using the property v/s the associated variable of the property interchangeably doesn't always work out the way you'd expect it to. Use the "variable" name only if you're using it to release memory by [variable release] statement, just the way you'll find it in any Apple example code. In all other cases use self.propertyname.

kCFNumberFormatterScientificStyle and kCFNumberFormatterDecimalStyle hybrid? - iphone / objective-c

in objective-c or iphone development has anyone ever done dynamic number formatting - something along the lines of "use kCFNumberFormatterDecimalStyle until the number gets too big, then use kCFNumberFormatterScientificStyle instead?"
i want to display a number with some sort of hybrid between the two, but i'm having a little trouble with implementing it. thanks in advance.
An other way would be selecting the formatter inline using the elvis operator:
// assuming you have formatter declared previously
// and x is the float NSNumber you want to format
formatter = ([x floatValue] < 1000.0) ?
kCGNumberFormatterDecimalStyle :
kCGNumberFormatterScientificStyle;
// Format with formatter
You would probaly want to put all of this in a #define or a method though.
I'd say create a method for that somewhere:
NSString *NSStringFromNumberInHybridStyle(NSNumber *aNumber)
{
if ([aNumber intValue > 100]) {
// format with kCGNumberFormatterDecimalStyle
} else {
// format with kCGNumberFormatterScientificStyle
}
}