Segmentation Error In Objective C -


i trying write program nsdate in ubuntu (as don't have mac using ubuntu run objective c programs). while compiling program getting error "segmentation fault (core dumped)". below code

 #import <foundation/foundation.h>    #import <objc/objc.h>    #import <objc/object.h>     int main()    {     nsautoreleasepool * pool=[[nsautoreleasepool alloc] init];     nsdate *now = [nsdate date];    nslog(@"now %@", now);     nstimeinterval secondsinaweek = 7 * 24 * 60 * 60;    nslog(@"secondsinaweek %@", secondsinaweek);    nsdate *lastweek = [nsdate datewithtimeinterval: secondsinaweek    sincedate:now];    nslog(@"last week %@", lastweek);     nsdate *nextweek = [nsdate datewithtimeinterval: secondsinaweek    sincedate:now];    nslog(@"next week %@", nextweek);     [pool drain];    return 0;    } 

please me find error. able output nsdate *now, after getting segmentation error.

ask me if need more info regarding code.

i guess able solution answer. trying different things , solution worked still don't know how did.

#import <foundation/foundation.h>     #import <objc/objc.h>     #import <objc/object.h>      int main()     {      nsautoreleasepool * pool=[[nsautoreleasepool alloc] init];      nsdate *now = [nsdate date];     nslog(@"now %@", now);      nstimeinterval secondsinaweek = 7 * 24 * 60 * 60;     nslog(@"secondsinaweek %f", secondsinaweek);      nsdate* lastweek = [[[nsdate alloc] initwithtimeinterval:-secondsinaweek sincedate:now] autorelease];     nslog(@"last week %@", lastweek);      nsdate *nextweek = [[[nsdate alloc] initwithtimeinterval:secondsinaweek sincedate:now] autorelease];     nslog(@"next week %@", nextweek);      [pool drain];     return 0;     } 

i have changed

nsdate *lastweek = [nsdate datewithtimeinterval: secondsinaweek sincedate:now];

with

nsdate *lastweek = [[[nsdate alloc] initwithtimeinterval:secondsinaweek sincedate:now] autorelease];

its working fine , giving desired output. still don't know why , how worked. :)


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -