objective c - Synthesized NSNumber "out of scope" -
i'm doing ios app crawl youtube subscription's videos. have problem when want navigate see next videos @ third time.
for these, need collect start-index (nsnumber *youtubestart) add number of videos displayed (int const maxvideos).
for that, have in videosviewcontroller.h
@interface videosviewcontroller : uiviewcontroller { nsnumber *youtubestart; } @property (nonatomic, retain) nsnumber *youtubestart;
then in videosviewcontroller.m
@synthesize youtubestart; static int const maxvideos = 6;
and method do
- (void) navigatevideos:(id)sender { int navigate = 0; int start = [youtubestart intvalue]; if(sender == bt_prev) { if(start >= maxvideos) { start -= maxvideos; navigate = 1; } } if(sender == bt_next) { start += maxvideos; navigate = 1; } if (navigate > 0) { youtubestart = [nsnumber numberwithint:start]; nsstring *url = [nsstring stringwithformat:@"%@&start-index=%i&max-results=%i" , myurl, [youtubestart intvalue], maxvideos]; [self loadoauthurl:url]; } }
when "touchupinside" 1 of buttons "bt_prev" or "bt_next" call "navigatevideos". can press bt_prev or bt_next many times want , works if press bt_next 3 times in row, youtubestart become out of scope. seems not able go further third page.
could me understand why , how handle it.
my others synthesized variables not out of scope.
thanks lot help.
edit: run application on ipad simulator x-code. application crash exc_bad_access error. youtubestart appear "out of scope" in debugger before "int start = [youtubestart intvalue];" , that's why crash. when works fine, youtubestart not out of scope.
edit 2: stacktrace missing. got in debugger console:
program received signal: “exc_bad_access”.
and in debugger red arrow:
0x0134d0b0 <+0036> cmp ecx,dword ptr [eax]
this problem caused not using properties properly. should use self.youtubestart
when assign new value youtubestart
property. object can retained property. when assign directly ivar actual nsnumber
object not retained, means time access later (like when call start = [youtubestart intvalue]
) object may have been auto-released.
i'm guessing 'out of scope' error means object has been deallocated.
here's recommend...
remove code @ @interface
in videosviewcontroller.h:
{ nsnumber *youtubestart; }
and change @synthesize
this:
@synthesize youtubestart = _youtubestart;
then compiler errors/warnings directly access youtubestart
within code. fix these changing access self.youtubestart
.
this practice using properties because makes sure don't ever accidentally use ivar directly. if want use ivar directly, can use _youtubestart
(this ivar created automatically @synthesize
line). recommend not using @ unless understand how accessors , retain etc. works.
also, if struggle stuff highly recommend looking arc :)
Comments
Post a Comment