How to get current time

Discussion in 'iPhone/iPad Programming' started by eliavlevy, May 1, 2009.

  1. macrumors newbie

    Joined:
    Feb 8, 2009
    #1
    Edit: I got my answer for the first question.
    I have:
    (These are only certain parts from the file.)

    NSString *nameString = string; // (string is a name that exists)
    nameString = [NSDate date];
    nameString = [nameString substringWithRange:NSMakeRange(10, 4)];
    NSLog(nameString);

    The application works, but when I make a label display the text in nameString, it crashes.
    Why? What am I doing wrong?

    If I am not doing this correctly, then:
    How can I remove certain parts of a string?
    For example: in C# I would have done:
    str = str.Remove(5,3);

    Thanks, Eliav
     
  2. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
  3. thread starter macrumors newbie

    Joined:
    Feb 8, 2009
    #3
    I added a question.
     
  4. macrumors regular

    Joined:
    Apr 9, 2009
    #4
    weeeell ... your code is pretty much wrong from the beginning. your are getting an NSDate Object via [NSDate date], but you are storing it in an NSString - not gonna work.
    look at the description method of NSDate
     
  5. macrumors regular

    Joined:
    Mar 24, 2008
  6. thread starter macrumors newbie

    Joined:
    Feb 8, 2009
    #6
    Re:

    I just jumped into objective-C, I learned before 2 years of C#, and had 2 last weeks of console C++, and 1 last week of Visual C++, and I just jumped into this the day before yesterday.
    I guess I should read and learn alot before I try to start working.
    Thanks, guys. (Read away! ^^)
     
  7. macrumors regular

    Joined:
    Apr 9, 2009
    #7
    well, apple provides some excellent documentation for people who are already familiar with other programming languages.

    objective-C is (suprise) very object-oriented. nearly everything is an object of some kind, and you simply can't store an NSDate object in an NSString. but you can convert :)
     
  8. thread starter macrumors newbie

    Joined:
    Feb 8, 2009
    #8
    In fact, when I did this:

    NSString *nameString = string; // (string is a name that exists)
    nameString = [NSDate date];

    It actually worked without an error or warning.

    But anyway, where can I start learning quickly from tutorials?
     
  9. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    It doesn't give you an error because +[NSDate date] returns id, not NSDate*.

    I think you should read up on C first, since it seems you don't fully understand pointers which is the core of this problem.
     
  10. thread starter macrumors newbie

    Joined:
    Feb 8, 2009
    #10
    Correct, because that is exactly where I stopped learning in C++...:(
     
  11. macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #11
    Unfortunately they are a necessary evil and it isn't worth your while not to learn them. C wouldn't be anywhere near as powerful as it is without them

    Just remember * is contents of and & is address of and you can't go wrong :D
     
  12. thread starter macrumors newbie

    Joined:
    Feb 8, 2009
    #12
    Just learning about them right now.
    But, as I can see, I know basic and intermediate C++ and C#, but programming in Xcode has lots of stuff I never saw like [blabla alloc]; or [blabla release]; or initWithBlaBla and such... Anywhere else I can learn about that? Maybe some tutorials?
     
  13. macrumors regular

    Joined:
    Apr 9, 2009
    #13
    well, initWithBlaBla are really just methods of classes, so it's nothing really new. for alloc and release you should read up on memory managment and retain counts.
     

Share This Page