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.
Related
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 *
I must be doing something really obviously wrong, but I can't see it.
A double is a C type, not an Objective-C object. Hence you use C casts:
double myDouble = 3.2;
int myInt = (int)myDouble;
Just converting mentioned above is good enough though you might want to use floor() or ceil() functions before that.
intValue is a method for a NSNumber instance.
For scale type like int, double, and float, they are not class type. So, they have no methods. Some languages like C# may wrap int, or double as a object, and they can be transfered to each other by a sub-routine.
try this
NSInteger number=33;
NSUInteger count = (NSInteger)[number];
here, NSUInteger is long.
number is NSInteger
double myDouble = 3.2;
int myInt = #(myDouble).intValue;
Sample of code I actually use:
NSNumber * percentLike1 = #(#(self.percentLike.doubleValue*100).integerValue);
I know I must be over-complicating this because it NSTimeInterval is just a double, but I just can't seem to get this done properly since I have had very little exposure to objective c. the scenario is as follows:
The data im pulling into the app contains two values, startTime and endTime, which are the epoch times in milliseconds. The variables that I want to hold these values are
NSTimeInterval *start;
NSTimeInterval *end;
I decided to store them as NSTimeIntervals but im thinking that maybe i ought to store them as doubles because theres no need for NSTimeIntervals since comparisons can just be done with a primitive. Either way, I'd like to know what I'm missing in the following step, where I try to convert from string to NSTimeInterval:
tempString = [truckArray objectAtIndex:2];
tempDouble = [tempString doubleValue];
Now it's safely stored as a double, but I can't get the value into an NSTimeInterval. How should this be accomplished? Thanks
You don't have to cast, you can just write this:
NSTimeInterval timeInterval = [[truckArray objectAtIndex:2] doubleValue];
The cast is needless, and extra casts just make your source code harder to update and change in the future because you've told the compiler not to type-check your casted expressions.
The variables that I want to hold these values are NSTimeInterval *start; NSTimeInterval *end;
Careful, NSTimeInverval is a typedef for a primitive C type, it is not an Objective-C object. I don't think you actually need pointers to these types in this scenario, so you should declare them like this:
NSTimeInverval start;
NSTimeInterval end;
You could be getting errors because in C, you cannot convert floating-point types to pointer-types.
I've got an CGFloat but need it as an NSInteger. The float value is like 2.0f, so I don't mind about fractional parts and loosing precision. What's a legal way to convert it into NSInteger without trouble (except the loss of precision, of course)?
NSInteger niceInt = niceCGFloat;
seems too simple, smells buggy. Maybe you can explain?
You want the c function lrintf() which rounds a floating point to a long int.
There's always the risk that 2.0f may actually be 1.9999999999f when represented in binary. Your conversion to int would then lead to 1 instead of 2.
To avoid this, I would add 0.5f to your float value. This would also have the effect of rounding your float, instead of truncating it.
NSInteger niceInt = niceCGFloat + 0.5f;
I have a CGFloat that contains only "integers", in the meaning that it actually wants to represent integers only but due to float inprecision it may happen that it internally has a 23.00000000000000000000000142 or something like that. I try to feed an NSDecimalNumber with clean input, so I need to make sure this is truly a naked integer with no dirt. Well, I think this is a good way, but maybe I am wrong:
NSInteger *intVal = floatVal;
That would just get rid of those fragmental parts at the end of the tale, right? Or is there a more secure way to convert it into a true integer?
If you just want to take the integer part of a float then I believe you can just do the following:
CGFloat myfloat = 23.0000000000142f;
int myInt = (int) myfloat;