Wednesday, 24 August 2011

down the rabbit hole of objects


ok ok know should easy .. can't work
passing array of nsvalues several objects..

have several objects. let's say.

fooview
foocard
foorow
foo

in foo.h
code:
 @interface foo : nsobject { nsvalue *kungfoo; // nsvalue holding cgpoint // nsinteger somefoo;  nsstring *foostring;  @property (nonatomic,retain) nsvalue *kungfoo; @property (nonatomic) nsinteger somefoo; @property (nonatomic,retain) nsstring *foostring;  
in foo.m
code:
  @implementation foo @synthesize kungfoo,somefoo,foostring; -(id)init {   foostring = [[nsstring alloc]init];   kungfoo  = [[nsvalue alloc] init];   return self; } -(id) initfoowithpos: (nsvalue *)position fooname:(nsstring *)stringy foovalue:(nsinteger) valeww {   kungfoo = position;   foostring = stringy;   somefoo = valeww; return self; }  
ok in foorow.h
code:
 // blah blah.. @interface foorow: nsobject{  nsmutablearray * arrayoffoo; }  @property (nonatomic, retain) nsmutablearray *arrayoffoo; 
now foorow.m
code:
 #import "foorow.h" #import "foo.h" -(id)init{  arrayoffoo = [[nsmutablearray alloc]init]; return self; }  -(id)initfoorowwith:(nsinteger)numberoffoosrows {    int numberoffoorows = 7;    foo *newfoo = [[foo alloc] init];     for(int = numberoffoosrows; i>0;i--) {    cgpoint myfoopoint;    myfoopoint =cgpointmake(((numberoffoo-i)*10), 45);    nsvalue *tempfoo = [nsvalue valuewithcgpoint:myfoopoint);    [tempfoo initfoowithpos: tempfoo fooname:@"test123" foovalue:12];    [arrayoffoo addobject:tempfoo];    [tempfoo release];    } return self; } 
getting there. foocard.h
code:
 // @interface foocard: nsobject {    nsmutablearray *foorowarray;    uiimage *fooimage;  } // , appropriate property stuff //  
foocard.m
code:
 #import "foocard.h" #import "foorow.h"  // implementation , synthesize stuff...// -(id) init{       foorowarray = [[nsmutablearray alloc]init]; 	fooimage = [[uiimage alloc]init];         return self; }  -(id)initwithfoocardwith:(nsinterger )numberofrows{                (int i=numberofrows;i>0; i--) 	{ 		nslog(@" creating row:%i",(numberofrows-i)); 		foorow *newrow =[[foorow alloc] initfoorowwith:7]; 		[foorowarray addobject:newrow]; 		[newrow release]; 	} 	[self setfooimage:[uiimage imagenamed:@"fooimagepicture.png"]]; 	returns self;  }  

fooview.h
code:
 #import <uikit/uikit.h> #import "foocard.h" #import "foorow.h" #import "foo.h"  @interface fooview : uiview { 	foocard *currentcard; }  @property (nonatomic,retain) foocard *currentcard;  - (void)drawafoocard:(foocard *)cardtodraw;  @end 
fooview.m
code:
  - (void)drawafoocard:(foocard *)cardtodraw { 	currentfoocard = [[[card alloc]init]retain]; 	currentfoocard.fooimage = [[uiimage alloc] init]; 	 	nslog(@"using drawcard function"); 	currentcard = cardtodraw; 	 	[self setneedslayout]; }  - (void)drawrect:(cgrect)rect {     // drawing code. 	cgrect bounds = [self bounds]; 	cgpoint topleftcorner; 	cgpoint centre; 	topleftcorner.x = bounds.origin.x + bounds.size.width * 0.10; // in percentage of screen size // 	topleftcorner.y = bounds.origin.y + bounds.size.height * 0.10; // in percentage of screen size //  	 	centre.x = bounds.origin.x + bounds.size.width / 2; // in percentage of screen size // 	centre.y = bounds.origin.y + bounds.size.height/ 2; // in percentage of screen size //          uiimageview *foobackground = [[[uiimageview alloc]  						     initwithimage:[[currentfoocard fooimage]  						      stretchableimagewithleftcapwidth:0.0                                                               topcapheight:0.0]]retain]; 	  	[cardbackground setframe:cgrectmake(0, 0,bounds.size.width,bounds.size.height)]; 	[cardbackground setalpha:1.0]; 	[self addsubview:cardbackground]; 	[self sendsubviewtoback:cardbackground]; 	[cardbackground release];                           // ****** above stuff works image appears etc  **** !!! //                uiimage* uiimag = [uiimage imagenamed:@"yellowblob.png"];        uigraphicsbeginimagecontext(mysize);        cgpoint foopost;          // trying work here //         nsinteger currentrow = 0; // testing sake //                foorow *foorowcurrent = [[foorow alloc] init]; 	foo *currentfoo = [[foo alloc] init]; 	cardrowcurrent.arraystamps = [[nsmutablearray alloc] init];                  foorowcurrent = [currentfoocard foorowarray] objectatindex:currentrow];         nsvalue *convertvalue = [[nsvalue alloc]init];          (int i=7; i>0; i--) { 		currentfoo = [[foorowcurrent fooarray] objectatindex:(7-i)]; 		convertvalue = [currentfoo foopos]; 		foopost =[convertvalue cgpointvalue];                 nslog(@"index: %i  x=%f y=%f",(7-i),foopost.x.,foopost.y); 		// cod putting graphics down .. etc//                  } 

values of "index:0 x=0 y=0" ... 7 times.. of course want co-ordinates..

code:
 index: 0  x=0 y=45 index: 1  x=10 y=45  index: 2  x=20 y=45 index: 3  x=30 y=45 index: 4  x=40 y=45 ect.  
 

are using arc or not? because if you're not, you're leaking lot of objects. again, if are using arc, uses of release wrong.

in code:
code:
-(id)initfoorowwith:(nsinteger)numberoffoosrows {    int numberoffoorows = 7;    foo *newfoo = [[foo alloc] init];     for(int = numberoffoosrows; i>0;i--) {    cgpoint myfoopoint;    myfoopoint =cgpointmake(((numberoffoo-i)*10), 45);    nsvalue *tempfoo = [nsvalue valuewithcgpoint:myfoopoint);    [tempfoo initfoowithpos: tempfoo fooname:@"test123" foovalue:12];    [arrayoffoo addobject:tempfoo];    [tempfoo release];    } return self; } 
how many distinct foo objects creating, setting position, adding arrayoffoo?

if answer "seven", you're wrong. alloc & init of foo object? it's not in loop, it's not being performed each time around loop.

also, that's trick question, because you're not adding foo objects arrayoffoo.

object named tempfoo isn't foo instance @ all. calling initfoowithpos: on isn't going work @ all.

add tempfoo (which isn't foo object) arrayoffoo. array ends holding nsvalue objects, not foo objects.


frankly, should start @ bottom, foo class. make work. means writing -description method returns useful , informative string, can nslog %@ formatter. can use debugger on it.

next, write simple test nothing make few different (and distinct) instances of foo, different positions, uses nslog show them. confirm works expected.

move 1 level. write code builds array of foo objects. correctly. making 1 foo object, setting position, adding array. not adding nsvalue array. confirm works writing test, showing class name of every object in array (it's easy do, need think how class name object).

every output value should expect. if it's not, debug until there no obvious errors.

, along way, make sure have memory management correct, either using arc or using retain/release. you're leaking objects in foo methods, because you're accessing instance variables directly, rather properties. example:
code:
 -(id) initfoowithpos: (nsvalue *)position fooname:(nsstring *)stringy foovalue:(nsinteger) valeww {   kungfoo = position;   foostring = stringy;   somefoo = valeww; return self; } 
needs this:
code:
 -(id) initfoowithpos: (nsvalue *)position fooname:(nsstring *)stringy foovalue:(nsinteger) valeww {   self.kungfoo = position;   self.foostring = stringy;   self.somefoo = valeww; return self; } 
and you've failed call [super init]. , should never call init method on same object more once.

, didn't foocard or fooview classes, riddled errors (including memory management errors).


need rethink whole approach. make foundations work. confirm work writing tests. confirm tests work looking @ output , confirming it's correct. when foundation built can rely on in other classes.

you're writing code if testing , debugging performed after built. that's crazy. have test , debug go along, have reason believe have works. without that, have nothing build on.

right now, you're building on quicksand, no testing of low-level things, no confirmation works, , little or no attention paid warnings compiler should giving (e.g. when used initfoowithpos: on nsvalue object, compiler should have warned it).


edit
or maybe didn't post real code. because use variable named numberoffoo doesn't exist anywhere else in code. means can't possibly compile posted.

in case, either post real code (the preferred option), or @ least make sure fictional code you're posting compile.
 


Forums iPhone, iPad, and iPod Touch iOS Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment