Get system time

walden systems, objective c, geolocation, ios, CLLocationManager, desiredAccuracy, distanceFilter, startUpdatingLocation, stopUpdatingLocation, delegate, iphone, picture, screen, swipe, iphone, thread, nsthread, uitableviewcell, uitableview, initwithFName, infocellcontroller, nsdate, nsdateformatter nsstring
Objective-C defines a small but powerful set of extensions to the ANSI C programming language that enables sophisticated object-oriented programming. Objective-C is the native language for Cocoa programming—it’s the language that the frameworks are written in, and the language that most applications are written in. You can also use some other languages—such as Python and Ruby—to develop programs using the Cocoa frameworks. It’s useful, though, to have at least a basic understanding of Objective-C because Apple’s documentation and code samples are typically written in terms of this language.

Sometimes you might need to show the system time on your iPhone or iPad app. There are many ways to accomplish this task and I would like to discuss one of the ways we are using here at Walden Systems, because we believe this is the most straightforward method to show system time.

Strategy to show system time is straight forward: First, we need to declare a variable of type NSDate to get the time. Second, we need to declare a variable of type NSDateFormatter to set the formatting. Third, we need to declare a variable of type NSString to show to the screen. Fourth, we need to get the system time. Fifth, we need to format the time and set it to a variable.

Here are actual code samples:

The first thing you need to do is declare a variable to get the time and a variable to format the time. To do so, use the following line:

1    NSDate *     time ;
2    NSDateFormatter *     format ;

Then you need to declare a variable to hold the formatted string for the time and then get the system time.

1    NSString *     timeStr ;
2    time = [ NSDate date ] ;


Finally, you set the formatted time to a string

1    format = [ [ NSDateFormatter alloc ] init ] ;
2    [ format setDateFormat : @"HH:mm:ss" ] ;
3    timeStr = [ format stringFromDate : l_time ] ;

So, this is how a final code for resizing your picture will look like:

1    NSDate *                     time ;
2    NSDateFormatter *    format ;
3    NSString *                  timeStr ;
4       
5    time = [ NSDate date ] ;
6    format = [ [ NSDateFormatter alloc ] init ] ;
7    [ format setDateFormat : @"HH:mm:ss" ] ;
8    timeStr = [ format stringFromDate : time ] ;