1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

How to get current time

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

  1. macrumors newbie

    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)];

    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

  3. macrumors newbie

    I added a question.
  4. macrumors regular

    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

  6. macrumors newbie


    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

    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. macrumors newbie

    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


    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. macrumors newbie

    Correct, because that is exactly where I stopped learning in C++...:(
  11. macrumors 6502


    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. macrumors newbie

    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

    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