time app

Discussion in 'Mac Programming' started by twoodcc, Mar 31, 2009.

  1. macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    ok, i want to make an app that all it really is a timer: while it runs, the time is ticking (going up). and when you quit the program, it saves the time. and then when it is launched again, the time starts from where it left off before.

    so, you launch it, let it run for 5:05, then you close it. open it up later, it runs for 0:25, so it says 5:30 is the time.

    does that make sense?

    also, can i show it in years, months, days, hours, minutes, seconds?

    is this possible?

    thanks in advance
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Yes. Obviously you'll need to know how to program and have some knowledge (or be willing to gain that knowledge) of Cocoa (or Carbon if you really want).
     
  3. macrumors 601

    themoonisdown09

    Joined:
    Nov 19, 2007
    Location:
    Georgia, USA
    #3
    This would be a very simple application that you could create in a day without any previous knowledge of Cocoa. Of course, if you have no knowledge of application development, it will take you longer.

    I would make this for you if I wasn't going out of town.
     
  4. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #4
    thanks for the reply.


    thanks for the reply. I do have programming knowledge, but not much when it comes to Cocoa.

    how would i go about this with cocoa though?
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    In general terms:
    You need a backing store to keep the current time, and you need to store it when the app is closed. I'm not too well versed with this stuff, so i don't know if that should go in terminate:, etc. The backing store can be a file in the filesystem, an entry in a plist, etc. How to deal with these should be pretty easy to find.

    Is the timer needs to be changing every second, you might look into something like what is described in this stackoverflow thread:
    http://stackoverflow.com/questions/...g-a-number-and-showing-steps-on-a-cocoa-label

    Essentially you just need to, on a timer, update a label, etc.

    -Lee
     
  6. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #6
    thanks for the reply and link.

    i'm still not sure how to get started here. like i said, i do have programming experience, just not much with cocoa.

    how would i save the time? with core data?
     
  7. macrumors 601

    themoonisdown09

    Joined:
    Nov 19, 2007
    Location:
    Georgia, USA
    #7
    I created a very simple timer application for you to get started with. When the application opens, the timer starts counting. You can stop it or reset it.

    You just need to add the function that stores the elapsed time to a file and also loads the value from the file.

    I have uploaded the application to my SkyDrive (Hotmail's online disk drive). Please let me know once you've downloaded it so I can take it off.

    Note: You must have XCode 3 to build it and Leopard to run it.
     
  8. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #8
    thanks so much! I will download it today as soon as i get home from work.
     
  9. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #9
    alright, i downloaded it! thanks!

    i ran it just to see it, and it seems to work. but when you click "stop", visually it does stop, but when you click "resume" it jumps to where it would have been if you didn't click stop. in other words, it really doesn't stop.

    but that's not a big deal.

    so how would i go about saving the data? would i use core data?
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    I'd suggest you use NSUserDefaults: CoreData is massively overkill to store a single value!
     
  11. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #11
    oh ok. so how would i go about doing that?
     
  12. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    Read the documentation?
     
  13. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #13
    thanks for the link. i will look at it in more detail after work. i know there is sample code with that link, but just a simple example would really help me out (without having to look thru all the code to find it, and even then i probably won't understand what i find)

    like i said, the syntax i'm used to is visual basic, c#, java, php, etc. where cocoa is totally different
     
  14. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #14
    What's your goal? Is it to learn Cocoa, or is it to write a timer app? If the latter, I'd suggest using something you already know if you have knowledge in other languages. I thought I remember you doing some Java before? That certainly would do the job. If you're trying to learn Cocoa, you won't get far just by piecing things together without an understanding of what's happening.
     
  15. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #15
    thanks for the reply

    the main goal is to learn Cocoa. i know that piecing things together will not really help me, that's why if i get an example, with some sort of instruction with it, then i might start to catch on.

    for some reason i have had a really hard time with cocoa. i bought the book (3rd edition), and have not finished the book, but do plan to. but i just can't quite get it like i do php or java
     
  16. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #16
    I'd suggest learning Objective-C first. This will make it far easier to learn Cocoa.
     
  17. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #17
    i tried to go that route, and i made a few apps that ran in the terminal. but didn't seem to help me with cocoa
     
  18. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #18
    You need to learn the language (C then Objective-C) then the API (Cocoa). It's not something you will learn in 5 minutes. You need to take time to follow the tutorials that teach you the basics on the Apple site, get a book, follow it through and learn the basic paradigms. Once you understand the main patterns in Cocoa it all starts to fall into place.

    Asking others to write code and then copy/pasting it will teach you absolutely nothing.
     
  19. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #19
    thanks for the reply.

    i understand that i need to go thru the whole process to really learn cocoa, but without a teacher, that won't be easy - and it won't be quick either. granted, if i did not work all day, then i could probably go thru all of that. but i can't. so i'm trying another route.

    i didn't ask anyone to write the app for me. i was asking how to go about making this app. and i later asked for some examples on how to do certain things. i believe that i can learn from that. but maybe i can't - we'll see.

    this weekend i plan on really knocking this out, one way or the other. i really can't during the week, b/c i have to work all day, and then study what i did at work that day b/c i am tested on it. yes i know, that's my problem
     
  20. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #20
    ok, so i'm trying to implement NSUserDefaults, and don't really know how to go about it.

    what type of variable is TimeSinceDate? i think i want to write that the the defaults, and call it on startup of the app.
     
  21. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #21
    ok, i overlooked that timeSinceDate is an NSTimeInterval variable. but when i try to update that variable with the userdefaults, it says incompatible data types. but i've tried every type. what am i supposed to do?
     
  22. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #22
    would NSCoding be a good method for saving this data?
     
  23. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #23
    well, after trying to use this method, i broke the app
     
  24. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #24
    To store:
    Code:
    //Assume myNSUD is your NSUserDefaults * that is initialized
    //Assume currentTimeElapsed is an NSTimeInterval with the value you want to store
    [myNSUD setObject: [NSNumber numberWithDouble:currentTimeElapsed] forKey:@"Time Elapsed"];
    
    To retrieve:
    Code:
    //Assume myNSUD is your NSUserDefaults * that is initialized
    NSNumber *tempTime = (NSNumber *)[myNSUD objectForKey:@"Time Elapsed"];
    NSTimeInterval savedTime = [tempTime doubleValue];
    //savedTime is now set to the value previously stored
    
    References:
    NSUserDefaults
    NSNumber
    Foundation types, including NSTimeInterval

    -Lee
     
  25. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #25
    thanks! now, do i have to put both of these in their own function for it to work?
     

Share This Page