How to get NSDate with nil value - ios

I'm a beginner in Swift and coding in general. Right now I'm trying to develop the piece of the code to set up the time limit for the action only once a day.
#IBAction func yesButtonPressed(sender: AnyObject) {
//To retrive the control date value. First time it has nil value
var controlDate = NSUserDefaults.standardUserDefaults().objectForKey("controlDate") as? NSDate
//To check current date to compare with
var currentDate = NSDate()
// To check if time interval between controlDate and currentDate is less than 1 day
var timeInterval = controlDate?.timeIntervalSinceNow
var dayInSeconds = 24 * 3600
if timeInterval < dayInSeconds {
//show alert with message "You've done it recently. Pls wait a bit"
} else {
//perfome the action
//update the value of NSUserDefaults.standardUserDefaults().objectForKey("controlDate") with current time stamp
}
}
Instead of checking if the controlTime var has nil value to catch the App first time running I was trying to develop some shorter, universal code for both case, first time and the rest times when the controlDate var will be saved in UserDefaults.
Nevertheless it doesn't work properly (( I'd appreciate your help a lot!

dayInSeconds is the wrong type to make the comparison. Type inference is determining that it should be an Int while timeIntervalSinceNow is a Double under the covers. And thus, you can't definitively compare an Int and a Double with accuracy.
Create dayInSeconds in this way so that it's type is inferred as a Double, rather than an Int.
var controlDate = NSUserDefaults.standardUserDefaults().objectForKey("controlDate") as? NSDate
var currentDate = NSDate()
var timeInterval = controlDate?.timeIntervalSinceNow
//*** HERES THE CHANGE***
var dayInSeconds = 24.0 * 3600
if timeInterval < dayInSeconds {
}
Since you aren't attempting to call a method on timeInterval optional unwrapping is not necessary in this case.

timeInterval is an Optional, you need to unwrap it first
if let ti = timeInterval {
if ti < dayInSeconds {
...
}
}
More on Optionals

Related

How to compare two string values within a threshold value in swift?

I am trying to write a code where I have got two time[hh:min] data(String type). Need to just compare but the challenge is my code undergones some validations before returning the final values. so the assertion fails sometimes stating expected value is [17:04] but actual is [17:05]. Is there any way where we can use concept of Threshold that upto few minutes (say 2 mins) the comparison will still be valid?
Step one is do not store a thing as something that it is not. If these are times, they should be stored as times. Strings are for representation to the users; underlying storage is for reality.
So now let's store our times as date components:
let t1 = DateComponents(hour:17, minute:4)
let t2 = DateComponents(hour:17, minute:5)
Now it's easy to find out how far apart they are:
let cal = Calendar(identifier: .gregorian)
if let d1 = cal.date(from: t1),
let d2 = cal.date(from: t2) {
let diff = abs(d1.timeIntervalSince(d2))
// and now decide what to do
}
You first need to seprate your string to an array, and then you can compare.
/* That two arrays are A1 and A2 */
let minute1 = Int(A1[0])*60+Int(A1[1])
let minute2 = Int(A2[0])*60+Int(A2[1])
This may help you. I think that #Sweeper did not understand that it is a time, not a date.
You can convert your string to minutes, subtract one from another and check if the absolute value is less than the threshold:
extension String {
var time24hToMinutes: Int? {
guard count == 5, let hours = Int(prefix(2)), let minutes = Int(suffix(2)), Array(self)[2] == ":" else { return nil }
return hours * 60 + minutes
}
func time24hCompare(to other: String, threshold: Int = 2) -> Bool {
guard let lhs = time24hToMinutes, let rhs = other.time24hToMinutes else { return false }
return abs(lhs-rhs) < threshold
}
}
Testing:
"17:02".time24hCompare(to: "17:04") // false
"17:03".time24hCompare(to: "17:04") // true
"17:04".time24hCompare(to: "17:04") // true
"17:05".time24hCompare(to: "17:04") // true
"17:06".time24hCompare(to: "17:04") // false

Swift 3: Find out, if the date has changed - how?

For my app, I need to find out if the date changed in relation to the last time that you opened the app. For that reason, I made that you can see below. The problem is: it doesn't work for any reason. My app counts the days in a row that you used it so it adds one to a counter of days if the days changed.
Here's my bool:
var dateChanged: Bool
{
let dateNow = Date()
var oldDate = defaults.object(forKey: "oldDate2") as? Date
if dateNow != oldDate
{
return true
}
else
{
return false
}
oldDate = newDate
defaults.set(oldDate, forKey: "oldDate")
defaults.synchronize()
}
How could I improve that bool? At oldDate = newDate it says: 'Will never be executed.'. Can you help me?
Thank you for having a look!
You need to use Calendar method isDateInToday to check date is today's date. Also you need to put the code before returning value from function.
if !Calendar.current.isDateInToday(oldDate) {
defaults.set(Date(), forKey: "oldDate")
return true
}
return false
In order to know if the date has changed, use UIApplicationSignificantTimeChangeNotification
reference: https://developer.apple.com/documentation/uikit/uiapplication/1623059-significanttimechangenotificatio
#Whazzup - the problem is that you execute return before you try to set the date - that's why it will never be executed. You only need the return for the case where the dates are equal, and you don't need to do anything else.

Find Index of Array by NSDate?

I would like to find out the index of an array by an NSDate.
Ill tried:
var sectionsInTable = [NSDate]()
let indexOfElement = sectionsInTable.indexOf(date) // where date is an NSDate in my sectionsInTable Array
print(indexOfElement)
But ill always get false
How is it possible to get the index of an NSDate from an array?
Thanks in advance.
If you have exact copies of NSDate objects, your code should work:
let date = NSDate()
let date2 = date.copy() as! NSDate
var sectionsInTable: [NSDate] = [date]
let indexOfElement = sectionsInTable.indexOf(date2)
print(indexOfElement)
//prints: Optional(0)
Your approach should work fine. This code produces an index of 2:
let s = 31536000.0 // Seconds per Year
var tbl = [NSDate]()
tbl.append(NSDate(timeIntervalSince1970: 40*s)) // 0
tbl.append(NSDate(timeIntervalSince1970: 41*s)) // 1
tbl.append(NSDate(timeIntervalSince1970: 42*s)) // 2
tbl.append(NSDate(timeIntervalSince1970: 43*s)) // 3
let date = NSDate(timeIntervalSince1970: 42*s)
let indexOfElement = tbl.indexOf(date)
The most likely reason that you are not getting the proper index is that your search NSDate has a time component that does not match the time component in NSDate objects in the array. You can confirm that this is the case by printing both objects, and verifying their time component.
Since comparison depends on how deep you want to go with date time thing. I think you should just loop through your date array and compare if it's equal and return that index.

Multiply UITextfields values with Double in Swift?

I am trying to Multiply
self.tipLable.text = String("\((enterBillAmountTextField.text! as NSString).integerValue * (middleTextField.text! as NSString).integerValue * (0.01))")
But getting error Binary operator * cannot be applied to operands of type Int and Double
I am taking values form UITextfields. How to do this multiplication?
extension Double {
// Convert Double to currency
var currency: String {
let formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
return formatter.stringFromNumber(self) ?? "0"
}
}
tipLable.text = [enterBillAmountTextField, middleTextField].reduce(0.01) { $0 * (Double($1.text!) ?? 0) }.currency
A slightly shorter and clearer solution. Added a "currency" extension so it can still be done in one line :).
This works
self.tipLable.text = String("\( Double((enterBillAmountTextField.text! as NSString).integerValue) * Double((middleTextField.text! as NSString).integerValue) * 0.01)")
Swift doesn't know how to multiply an Int and a Double. Should the result be an Int or a Double?
Swift won't do implicit type conversion between different operands.
If you want your result to be a Double, both operands should be a Double. Then convert the Double to a String.
While this can all be concisely expressed in one single very long line, perhaps it's more readable and maintainable if you break it out into separate lines:
let subTotal = Double(billAmountTextField.text!) ?? 0
let percent = (Double(middleTextField.text!) ?? 0) * 0.01
let tip = subTotal * percent
self.tipLable.text = String(format: "%.2f", tip) // Handle rounding
The answer you gave is going to bring nightmare to you in some moment.
Try to keep yourself doing things in a way you can guarantee that you are going to be able to test it and that you/or others are going to be able to understand what you are doing there.
/**
Use this function to calculate tip, useful for later testing
- returns: Double Value of the tip you should give
*/
func calculateTip(billAmount billAmount:Double, middleValue:Double) -> Double {
/// Actually calculate Tip if everything is OK
return billAmount * middleValue * 0.01
}
Then in your #IBAction make sure you have correct data before asking
your function for a tip
/// If you have bill data, obtain Double value stored there,
/// if something fails, you should return nil
guard let billAmountText = enterBillAmountTextField.text, billAmount = Double(billAmountText) else {
return
}
/// If you have middle value data, obtain Double value stored there,
/// if something fails, you should return nil
guard let middleText = middleTextField.text, middleValue = Double(middleText) else {
return
}
Then you can call that function
let tip = calculateTip(billAmount: billAmount, middleValue: middleValue).description
//and return in proper format
tipLabel.text = String(format: "%.2f", tip)

Why is this dispatch_after firing instantly?

Here's a struct I've written to convert an NSTimeInterval into a walltime-based dispatch_time_t:
public struct WallTimeKeeper {
public static func walltimeFrom(spec: timespec)->dispatch_time_t {
var mutableSpec = spec
let wallTime = dispatch_walltime(&mutableSpec, 0)
return wallTime
}
public static func timeStructFrom(interval: NSTimeInterval)->timespec {
let nowWholeSecsFloor = floor(interval)
let nowNanosOnly = interval - nowWholeSecsFloor
let nowNanosFloor = floor(nowNanosOnly * Double(NSEC_PER_SEC))
println("walltimekeeper: DEBUG: nowNanosFloor: \(nowNanosFloor)")
var thisStruct = timespec(tv_sec: Int(nowWholeSecsFloor),
tv_nsec: Int(nowNanosFloor))
return thisStruct
}
}
I've been trying to test the accuracy of it in a Playground, but my results are confusing me.
Here's the code in my Playground (with my WallTimeKeeper in the Sources folder):
var stop = false
var callbackInterval: NSTimeInterval?
var intendedTime: NSDate?
var intendedAction: ()->() = {}
func testDispatchingIn(thisManySeconds: NSTimeInterval){
intendedTime = NSDate(timeIntervalSinceNow: thisManySeconds)
intendedAction = stopAndGetDate
dispatchActionAtDate()
loopUntilAfterIntendedTime()
let success = trueIfActionFiredPunctually() //always returns false
}
func dispatchActionAtDate(){
let timeToAct = dateAsDispatch(intendedTime!)
let now = dateAsDispatch(NSDate())
/*****************
NOTE: if you run this code in a Playground, comparing the above two
values will show that WallTimeKeeper is returning times the
correct number of seconds apart.
******************/
dispatch_after(timeToAct, dispatch_get_main_queue(), intendedAction)
}
func loopUntilAfterIntendedTime() {
let afterIntendedTime = intendedTime!.dateByAddingTimeInterval(1)
while stop == false && intendedTime?.timeIntervalSinceNow > 0 {
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode,
beforeDate: afterIntendedTime)
}
}
func trueIfActionFiredPunctually()->Bool{
let intendedInterval = intendedTime?.timeIntervalSinceReferenceDate
let difference = intendedInterval! - callbackInterval!
let trueIfHappenedWithinOneSecondOfIntendedTime = abs(difference) < 1
return trueIfHappenedWithinOneSecondOfIntendedTime
}
func dateAsDispatch(date: NSDate)->dispatch_time_t{
let intendedAsInterval = date.timeIntervalSinceReferenceDate
let intendedAsStruct = WallTimeKeeper.timeStructFrom(intendedAsInterval)
let intendedAsDispatch = WallTimeKeeper.walltimeFrom(intendedAsStruct)
return intendedAsDispatch
}
func stopAndGetDate() {
callbackInterval = NSDate().timeIntervalSinceReferenceDate
stop = true
}
testDispatchingIn(3)
...so not only doestrueIfActionFiredPunctually() always returns false, but the difference value--intended to measure the difference between the time the callback fired and the time it was supposed to fire--which in a successful result should be really close to 0, and certainly under 1--instead comes out to be almost exactly the same as the amount of time the callback was supposed to wait to fire.
In summary: an amount of time to wait is defined, and an action is set to fire after that amount of time. When the action fires, it creates a timestamp of the moment it fired. When the timestamp is compared to the value it should be, instead of getting close to zero, we get close to the amount of time we were supposed to wait.
In other words, it appears as if the action passed to dispatch_after is firing immediately, which it absolutely shouldn't!
Is this something wrong with Playgrounds or wrong with my code?
EDIT:
It's the code. Running the same code inside a live app gives the same result. What am I doing wrong?
I figured it out. It's a head-smacker. I'll leave it up in case anyone is having the same problem.
I was using NSDate().timeIntervalSinceReferenceDate to set my walltimes.
Walltimes require NSDate().timeIntervalSince1970!
The dispatch_after tasks all fired instantly because they thought they were scheduled for over forty years ago!
Changing everything to NSDate().timeIntervalSince1970 makes it work perfectly.
Moral: don't use walltimes unless you're sure your reference date is 1970!

Resources