PDA

View Full Version : How to get current time




eliavlevy
May 1, 2009, 08:45 AM
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



jnic
May 1, 2009, 09:11 AM
http://www.google.com/search?q=iphone+get+current+time



http://stackoverflow.com/questions/358207/iphone-how-to-get-current-milliseconds



[[NSDate date] timeIntervalSince1970];

NSDate class reference: http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

eliavlevy
May 1, 2009, 02:18 PM
I added a question.

BlackWolf
May 1, 2009, 02:22 PM
I added a question.

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

fenrus110
May 1, 2009, 04:53 PM
http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/introStrings.html

eliavlevy
May 2, 2009, 12:51 AM
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! ^^)

BlackWolf
May 2, 2009, 04:25 AM
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! ^^)
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 :-)

eliavlevy
May 2, 2009, 09:44 AM
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?

kainjow
May 2, 2009, 10:03 AM
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.

eliavlevy
May 2, 2009, 11:01 AM
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.

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

mccannmarc
May 2, 2009, 11:36 AM
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

eliavlevy
May 2, 2009, 11:49 AM
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

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?

BlackWolf
May 2, 2009, 08:33 PM
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?

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.