Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
Well, thanks guys for all the responses, I appreciate your help (even if I was stubborn).

:)


My experience in programming has been mainly scripting languages (PHP, JS, etc) and Java is my only real compiled language I have worked extensively in. That's why the memory management is a new thing for me. Pointers, though I havent worked much with them, I have seen before in C (I am by no means a C expert, just enough to get by)

PHP has printf() and sprint() which has similar semantics to NSLog() and similarly named functions for formatted output.

Java has System.out.printf() which has the same semantics to NSLog and printf(), and in fact while we are on the subject of java, for System.out.printf you need to use %s (string) and manually get that string by calling Object.toString() on your object? Well objective-C helps you out with the format specifier %@ , only the method that gets called is -(NSString *) description; and it gets called for you just by passing in the object (pointer).

In fact, Java should give you the same trouble when trying to shoehorn a double into an int format specifier. You'll basically just see an integer representation of either the 4 low order or high order bytes of the double (depending on the platform).

----------

In C, printf() has the same limitation as NSLog does: the format-specifier must match the actual type of the value. A modern C compiler has the ability to flag mismatches as warnings or errors.


Objective-C is a strict superset of plain ordinary C. That means every valid C program is also a valid Objective-C program, with the same syntax and semantics. Thus, every C type is an Objective-C type, every C function is an Objective-C function, and so on. It also means you could use printf() or fprintf(), and the stdio library, if you wanted to. You might have to figure out where the output goes, because different Xcode versions present stdout and stderr in slightly different ways (by default).
That said printf() does not have the %@ format specifier, and isn't so great for most Obj-C applications unless it is command line or you open a file yourself to log to.
 
Ok, well I have used about every format specify that could possibly apply.

I finally just decided to use integer (unless there is a Date datatype that I am just not seeing.

Code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
   
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSDateComponents *comps = [[NSDateComponents alloc] init]; 
	[comps setDay:1];
	[comps setMonth:7];
	[comps setYear:1984];
	
	
	
	NSLog(@"comps at this point is %i", comps);
	
	NSCalendar *g = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] ;
	NSDate *dateofbirth = [g dateFromComponents:comps];
	NSDate *now = [NSDate date];
				   
	int timeSince = [dateofbirth timeIntervalSinceDate: now ];
	
	
	
	
				   
	NSLog(@"your age %i", timeSince);
	
	
	
	[pool drain];
   
	
	
    return 0;
}

I am still getting incorrect output.

Code:
[Switching to process 299]
Running…
2012-01-14 15:51:24.933 HowManySecs[299:a0f] comps at this point is 1099568
2012-01-14 15:51:24.936 HowManySecs[299:a0f] your age -869071884

Also, I am a bit confused about the NSAutoReleasepool and my book (the big nerd ranch guide (which BTW, I don't feel this is up to par on the Deitel books etc)) doesnt do a very good job of explaining simple memory management in Objective C.

Now, I am not asking anyone to do anything FOR me, I would just like to know if I can explicitly decalare an alloc & init within a NSAutoPool or does it not matter?

Also, I do appreciate the help.

Simple memory management in Obj-C:
"Your Objects":
If you [ClassName alloc], you need to release it when you are done with it.

If the method name includes the word "new" like [ObjectOrClass newInstance] treat it as above.

"Temporary Objects":
Every other time you get an object returned to you in some fashion, you need to either use it and be done with it. Or retain it when you get it, and release it when you are done with it. (these objects have already been placed in the autorelease pool for you, when in doubt retain them, and release them later)

If you put an object into a collection class like an NSArray, the collection class will retain it, so its USUALLY good to release it yourself right after wards.

"Returning Objects":
If the object you are returning is of the type "Your Object", call autorelease on it before returning it.

If the object is of the type "Temporary Objects", just return it.



Quiz time:
Of which type ("Your object" or "Temporary Object") are the following in the above code?
NSDateComponents *comps "Your Object"
NSCalendar *g "Your Object"
NSDate *dateofbirth "Temporary Object"
NSDate *now "Temporary Object"

Which ones need releases?
comps and g

EDIT: highlight the quiz for answers.
 
Last edited:
Basic memory management rules:
  • You own any object you create.
  • You can take ownership of an object using retain.
  • When you no longer need it, you must relinquish ownership of an object you own.
  • You must not relinquish ownership of an object you do not own.
The above rules (which are only headings) are taken directly from:
Advanced Memory Management Guide : Basic Memory Management Rules

Google search terms: objective-c memory management

You must read the reference doc to learn exactly what "any object you create" means for the rule. You should read the entire guide, because the concept of ownership is fundamental and essential. If you understand ownership, the rules make sense and are easy to follow. If you don't understand ownership, the rules seem like magical incantations which must be followed for no apparent reason.

Or skip all this nonsense and turn on garbage collection, which is available in Snow Leopard, while the still-newer feature of ARC (Automatic Reference Counting) is not.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.