0

I've got an array that I'm trying to store timestamps into.

I'm testing stuff out with something like:

    @IBAction func buttonPressed(sender:UIButton!) {

    let post = ["MinutesLeft" : (FIRServerValue.timestamp())]
    DataService.ds.REF_POSTS.childByAutoId().setValue(post)

}

Then I'm trying to call it back and fill an array with some of those timestamps:

  DataService.ds.REF_POSTS.queryOrderedByChild("MinutesLeft").queryStartingAtValue(Int(cutoff)).observeEventType(.ChildAdded, withBlock: { (snapshot:FIRDataSnapshot) in

     let post = snapshot.value!["MinutesLeft"]

        print("POST \(post)" )

        self.postsArray.append(post)
            print(self.postsArray)
        } else {
            print("Didn't work")
        }

    })

Then I'm running a timer that's meant to clear out certain posts:

func removeExpiredItems() {
    let cutoff = UInt64(1000 * floor(NSDate().timeIntervalSince1970) - 10*60*1000)

    while (self.postsArray.count > 0 && self.postsArray[0] < cutoff) {
        // remove first item, because it expired
        self.postsArray.removeAtIndex(0)
        print(postsArray.count)
    }

}

My issues are :

I don't know what kind of array to have here. It's telling me that a timestamp is an AnyObject, but when I go to make an array of anyobjects I get a straight up Xcode internal error crash...so I guess that's a no-go.

I can translate it all to strings and just store it like that, but the issue comes then when I'm trying to compare the times vs my cutoff in my removeExpiredItems func.

I was trying to do something like:

Take the timestamps, change them to a string , then down when I'm going to make the comparison change the String to an Int but I get something like "This kind of conversion will always fail".

Any ideas here?

3
  • I think you should change the value to UInt64 before compare. let aTime = UInt64(self.postsArray[0]) Commented Jul 10, 2016 at 0:35
  • for using AnyObject typed Array for storing timeinterval values you need use NSNumber to wrap values. let arr:[AnyObject] = [NSNumber(double: 1.0)] Commented Jul 10, 2016 at 8:45
  • @AliKıran Ahhh okay I see. That makes more sense. Xcode was NOT liking what I was trying to do . Thanks to you both Commented Jul 10, 2016 at 10:37

1 Answer 1

1
let post = snapshot.value!["MinutesLeft"]

The type of post will be AnyObject, (Optional<AnyObject>) in this case.

You have store it as the data type you want. For example in this case you can store it as NSTimeInterval

 let post = snapshot.value!["MinutesLeft"] as! NSTimeInterval

And store it in an Array of NSTimeInterval as

var postsArray :[NSTimeInterval] =  []
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.