How to fixed Call to 'div' is ambiguous with Xcode 9 final release - objective-c++

Do you have any ideas? Plz, help, thank you.
div_t divHour = div([comps hour], 12);
return divHour.rem;
and show me the error above..

This appears to be an issue with the C++ standard library. In C, there is one function div() that returns a div_t. In C++, there's multiple overloads of div() that take different argument types and return different return types. And your call is ambiguous because [comps hour] returns an NSInteger, but your 12 is an int, so it can't figure out which overload to use.
There's two reasonable ways to fix this. The first is to cast to int:
div_t divHour = div((int)[comps hour], 12);
The second is to unambiguously use the long version:
ldiv_t divHour = div([comps hour], 12l);

Related

In swift, using timeIntervalSinceReferenceDate, NSDate is not identical to NSTimeInterval error

I've two NSDates, one stamped by the devise, and the other entered by the user. I'd like to see if there is a difference between the two, and researched the following code, but I get an error message that NSDate is not identical to NSTimeInterval.
let compareResult = recordDateTimeEntered!.compare(recordDateTimeEntered!)
let timeFromEnteredtoStampedDates = recordDateTimeStamped.timeIntervalSinceReferenceDate(recordDateTimeEntered) // error: NSDate -> _ is not identical to NSTimeInterval
To compare two dates for equality, you can use the == operator in Swift (or the isEqualToDate: method in Objective-C). You can alternatively check that the timeIntervalSinceDate: between two dates is less than an acceptable threshold, to determine if two dates are roughly equal.

NSString - Truncate everything after decimal point in a double

I have a double that I need only the value of everything before the decimal point.
Currently I am using
NSString *level = [NSString stringWithFormat:#"%.1f",doubleLevel];
but when given a value of 9.96, this returns "10". So it is rounding. I need it to return only the "9". (note - when the value is 9.95, it correctly returns the "9" value.)
Any suggestions?
Thank You.
Simply assign the float/double value to a int value.
int intValue = doubleLevel;
Cast that baby as an int.
int castedDouble = doubleLevel;
Anything after the . in the double will be truncated.
9.1239809384 --> 9
123.90454980 --> 123
No rounding, simple truncation.
If you want to keep it as a float:
CGFloat f = 9.99;
f = floorf(f);
there are quite a variety of floor and round implementations.
they have been around since UN*X, and are actually part of those low-level libraries, be they BSD, Posix, or some other variety - you should make yourself familiar with them.
there are different versions for different "depths" of floating point variables.
NSString *level = [NSString stringWithFormat:#"%d",doubleLevel];

Removing characters after the decimal point for a double

How can I remove the all the characters after the decimal point.
Instead of 7.3456, I would just like 7.
This is what I do to get the number so far with decimal places.
[NSString stringWithFormat:#" %f : %f",(audioPlayer.currentTime),(audioPlayer.duration) ];
Many Thanks,
-Code
You can specify what you want using format string :
[NSString stringWithFormat:#" %.0f : %.0f", (audioPlayer.currentTime),
(audioPlayer.duration)];
If you want this for display, use an NSNumberFormatter:
double sevenpointthreefourfivesix = 7.3456;
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];
NSLog(#"%#", [formatter stringFromNumber:[NSNumber numberWithDouble:sevenpointthreefourfivesix]]);
2011-12-20 20:19:48.813 NoDecimal[55110:903] 7
If you want a value without the fractional part, use round(). If you want the closest integer value not greater than the original value, use floor().
floorf() is the function you're looking for.
you are after
[NSString stringWithFormat:#" %.00f : %.00f",(audioPlayer.currentTime),(audioPlayer.duration) ];
When formatting float you can tell the precision by the number before the f
Cast to int:
[NSString stringWithFormat:#" %i : %i",(int)(audioPlayer.currentTime),(int)(audioPlayer.duration) ];
Casting like this always rounds down (eg: just removes everything after the decimal place). This is what you asked for.
In the case of rounding to the NEAREST whole number you want to add 0.5 to the number
[NSString stringWithFormat:#" %i : %i",(int)(audioPlayer.currentTime+0.5f),(int)(audioPlayer.duration+0.5f) ];
This will round to the nearest whole number. eg: 1.2 becomes 1.7 and casting to int makes 1. 3.6 becomes 4.1 and casting makes 4. :)
Why not just cast the audioPlayer.currentTime to an integer before you use stringWithFormat?
[NSString stringWithFormat:#"%d", (int)(audioPlayer.currentTime)];
All you need to do is type-cast the double to an int, like so: int currentTime_int = (int)audioPlayer.currentTime;.
You can use this same approach for the other variable.
Many of the shorter answers here will work correctly. But if you want your code to be really clear and readable, you might want to explicitly specify your desired conversion from float to int, such as using:
int tmpInt = floorf(myFloat); // or roundf(), etc.
and then separately specifying how you want the integer formated, e.g.
... stringWithFormat:#"%d", tmpInt ... // or #"%+03d", etc.
instead of assuming that an inline cast shows what you want.
You may also use
double newDvalue =floor(dValue);
it will remove all the decimals point
using %.0f for string format will be good also

Objective-C Library for Sunrise and Sunset?

Is there an Objective-C (or C) library (that is compatible with core location) that can tell me the time of sunrise and sunset for any given calendar day?
EDSunriseSet is an open source and free Objective-C wrapper for the C languages routines created by Paul Schlyter.
Calculation is done entirely by the C-code routines. EDSunrisetSet bridges those calculations to common Cocoa classes (NSDate, NSTimeZone, ...)
I've actually ported the KosherJava Library and plan to make it available soon on GitHub!
Edit:
KosherCocoa is now live on GitHub! If you don't need the hebrew calendar related code, you can delete the "calendar" file. The class files are separated nicely into folders based on the kinds of calculations that they do.
Edit: KosherCocoa us due to be replaced with a modern and more complete update as soon as I can. The above link now points at a legacy repo.
I have used a library called SUNWAIT. Very simple, effective - easy to use!
Try this: https://github.com/mourner/suncalc/
Very clear and easy to implement, although it writen by javascript but it is easy to convert it to
objective-C
It also support for calculate sun, moon position and coordinate.
After not finding a simple Swift alternative, I created Solar: a Swift built micro-library for Sunrise / Sunset.
Just a note.. if you use the Berkley one... well it doesn't work (in Australia as least).
It does include the Paul Schlyter C code though, which is great.
If you want it to work anywhere, best to just calc the dates in UTC.
In SunriseAndSunset.m, replace the code from
double rise;
double set;
as follows:
sun_rise_set(theYear, theMonth, theDay, lon, lat, &rise, &set);
int hours = HOURS(rise);
int mins = MINUTES(rise);
int sethrs = HOURS(set);
int setmins = MINUTES(set);
NSTimeInterval riseOffset = ((hours * 60) + mins) * 60;
NSTimeInterval setOffset = ((sethrs * 60) + setmins) * 60;
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString *dateStr = [NSString stringWithFormat:#"%i-%02d-%02dT00:00:00+0000", theYear, theMonth, theDay];
NSDate *utcMidnight = [formatter dateFromString:dateStr];
NSDate *utcSunrise = [utcMidnight dateByAddingTimeInterval:riseOffset];
NSDate *utcSunset = [utcMidnight dateByAddingTimeInterval:setOffset];
[formatter release];
[gregorian release];
return [NSDictionary dictionaryWithObjectsAndKeys:utcSunrise, #"sunrise", utcSunset, #"sunset", nil];
try this one: https://github.com/berkley/ObjectiveCUtil

Convert a string of numbers to a NSTimeInterval

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.