use if with NSinteger? - iphone

I WANT to use NSinteger variable *strength in my code with if condition but it's not work.. :(
if(strength == 11){
}
How can i use if with NSInteger*

NSInteger is a primitive value type; you don't really need to use pointers. So your declaration should read
NSInteger strength;
And not
NSInteger *strength;
However if you do need to use a pointer to an NSInteger (that is, NSInteger *) for some reason, then you need to dereference the pointer to get the value:
if (*strength == 11) {
}
but from what I see, I don't think this is the case.

I assume you must be adding an * when you declare your strength variable. You shouldn't have it because NSInteger is a primitive type.
Why don't I declare NSInteger with a *

Related

int++ increments by 4

Here's my code:
- (IBAction)NextTouched:(id)sender {
NSLog(#"Index = %i", index);
if([project getCount]>(index++)) {
[self setUI:index];
}
}
Index is an integer, as declared in my .h file:
#property (nonatomic) int *index;
But every time I click the button, the log says the integer is going up by 4. Can you tell my why?
The reason it's going up by 4 is because index is a pointer. When you increment a pointer its value increases by the size of the data type it points to, in this case an int, which is 4 bytes.
Given index appears to be an index into an NSArray (or some other collection class), I think you want to make it int and not int * to solve your issue. Better still make it unsigned, like NSUInteger, which is the type returned from the count method.
Also I think you'll want to use prefix-increment rather than postfix-increment so that the if test uses the newly incremented value, not the previous value.
Simply define index as an integer variable rather than pointer and if you want to print the value before increment use index++ else use ++index to increment the value and then print

Objective-C += equivalent?

Sorry for the newbie question, but i cannot find an answer to it.
I have a simple operation. I declare a variable, and then i want to loop through an array of integers and add these to the variable. However, i can't seem to find how to get a += equivalent going in Objective C.
Any help would be awesome.
Code:
NSInteger * result;
for (NSInteger * hour in totalhours)
{
result += hour;
}
NSInteger is not a class, it's a typedef for int. You cannot put it into collections like NSArray directly.
You need to wrap your basic data types (int, char, BOOL, NSInteger (which expands to int)) into NSNumber objects to put them into collections.
NSInteger does work with +=, keep in mind that your code uses pointers to them, which is probably not what you want anyway here.
So
NSInteger a = 1, b = 2;
a += b;
would work.
If you put them with [NSNumber numberWitInt:a]; etc. into an NSArray, this is not that easy and you need to use -intValue methods to extract their values first.
If totalhours actually contains NSNumber objects you need the following:
NSInteger result = 0;
for(NSNumber* n in totalhours)
{
result += [n integerValue];
}
The problem is that you are confusing NSInteger (a typedef for int or long) with a class instance such as NSNumber.
If your totalhours object is an array of NSNumber objects, you'll need to do:
NSInteger result;
for (NSNumber *hour in totalhours)
{
result += [hour integerValue];
}
No problem using the '+=' operator, just be sure about the objects you are working with...
Your code might be :
NSNumber *n; NSUInteger t = 0;
for(n in totalHours) {
t += [n integerValue];
}
// you got your total in t...
The += operation definitly works. All you need to do is initialize your result variable so it has a start value.
E.g. NSInteger * result = 0;
Good luck!
Your problem is probably that you're using a pointer to an NSInteger instead of an actual NSInteger. You're also not initializing it. Try this:
NSInteger result = 0;
for (NSInteger * hour in totalhours)
{
result += *hour;
}

NSString to NSNumber : what am I doing wrong here?

I am executing this code:
NSLog(#"Converting line number %# to int...",currentValue);
NSNumber * currentNumber = [NSNumber numberWithInt:[currentValue integerValue]];
NSLog(#"Converted integer %d",currentNumber);
Which outputs this:
Converting line number 211 to int...
Converted integer 62549488
What am I doing wrong here ? In this case, currentValue is a NSMutableString with value of 211.
"currentNumber" is and object, which you're trying to print with %d.
NSLog(#"Converted integer %d", [currentNumber intValue]);
Although your question has already been answered there is something else wrong with your code.
There are differences between Cocoa methods with int and integer in their names.
The ones with int such as numberWithInt or intValue deal with int s.
The ones with integer such as numberWithInteger or integerValue deal with NSInteger s.
This might be a source of errors and inconsistency when programming 32/64-bit programs.
Just something to be aware of and consistent with.
NSNumber is inherited NSObject class.Its not an Integer.So you can use "%#" instead of %d.
You change your NSLog line as follows,
NSLog(#"Converted integer %#",currentNumber);

Objective C convert number to NSNumber

Why doesn't this please the compiler? Casting is supposed to work like in C as I can read here How to cast an object in Objective-C.
[p setAge:(NSNumber*)10];
where
- (NSNumber*) age {
return _age;
}
- (void) setAge: (NSNumber*)input {
[_age autorelease];
_age = [input retain];
}
In a sort of symmetry with your earlier question, NSNumber is an object type. You need to create it via a method invocation such as:
[p setAge:[NSNumber numberWithInt:10]];
Your current code is attempting simply to cast the arbitrary integer 10 to a pointer. Never do this: if the compiler didn't warn you about it, you would then be trying to access some completely inappropriate bytes at memory location 10 as if they were an NSNumber object, which they wouldn't be. Doing that stuff leads to tears.
Oh, and just to preempt the next obvious issues, remember that if you want to use the value in an NSNumber object, you need to get at that via method calls too, eg:
if ( [[p age] intValue] < 18 )
...
(NSNumber is immutable, and I think it is implemented such that identical values are mapped to the same object. So it is probably possible to get away with direct pointer comparisons for value equality between NSNumber objects. But please don't, because that would be an inappropriate reliance on an implementation detail. Use isEqual instead.)
Today it is also possible to do it using shorthand notation:
[p setAge:#(10)];
Use this:
[p setAge:[NSNumber numberWithInt:10]];
You can't cast an integer literal like 10 to a NSNumber* (pointer to NSNumber).
NSNumber *yourNumber = [NSNumber numberWithInt:your_int_variable];
Because NSNumber is an object and "10" in a primitive integer type, much like the difference between int and Integer in Java. You, therefore, need to call its initialiser:
[p setAge:[NSNumber numberWithInt:10]

Converting between types in Objective-C

Being a starting Objective-C developer, and not having the patience to actually do some studying, rather than just diving into things, I ran into the following:
I have a CGFloat, and I want to divide it by something, and use the result as an NSInteger.
Example:
CGPoint p = scrollView.contentOffset; //where p is a CGFloat by nature
NSInteger * myIndex = p.x/468.0;
While compiling, I get the error: "Incompatible types in initialization".
I guess that's because of the mismatch between the CGFloat and the NSInteger.
What should I know to get out of this? Sorry for bothering you.
Two problems. You're using a NSInteger pointer (NSInteger *myIndex). And you'll need a cast to go from float to int. Like so:
NSInteger myIndex = (int)(p.x / 468.0);
NSInteger is not an actual object type. It is a typedef for a primitive integer type. Remove the * and your example should work. As it is now, you're trying to assign the result of your math as a pointer and you're getting that error message.