开发者

Objective-C: properties not being saved or passed

i'm a newbie to iphone development. I'm doing a navigation-based app, and I'm having trouble passing values to a new view.

@interface RootViewController : UITableViewController {
    NSString *imgurl;
    NSMutableArray *galleryArray;
}

@property (nonatomic, retain) NSString *imgurl;
@property (nonatomic, retain) NSMutableArray *galleryArray;

- (void)showAll;

@end

#import "RootViewController.h"
#import "ScrollView.h"
#import "Model.h"
#import "JSON/JSON.h"

@implementation RootViewController

@synthesize galleryArray, imgurl;

- (void)viewDidLoad {   
    UIBarButtonItem *showButton = [[[UIBarButtonItem alloc]
                                initWithTitle:NSLocalizedString(@"Show All", @"")
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(showAll)] autorelease];
    self.navigationItem.rightBarButtonItem = showButton;

    NSString *jsonString = [[Model sharedInstance] jsonFromURLString:@"http://www.ddbstaging.com/gerald/gallery.php"];
    NSDictionary *resultDictionary = [jsonString JSONValue];

    if (resultDictionary == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Webservice Down" 
                                                    message:@"The webservice you are accessing is currently down. Please try again later." 
                                                   delegate:self 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else {
        galleryArray = [[NSMutableArray alloc] init];
        galleryArray = [resultDictionary valueForKey:@"gallery"];
        imgurl = (NSString *)[galleryArray objectAtIndex:0];
        NSLog(@" -> %@", galleryArray);
        NSLog(imgurl);
    }
}

- (void)showAll {
    NSLog(@" -> %@", galleryArray);
    NSLog(imgurl);
    ScrollView *controller = [[ScrollView alloc] initWithJSON:galleryArray];
    [self.navigationController pushViewController:controller animated:YES];
}

The RootViewController startup and the json data loads up fine. I can see it from the first console trace. However, once I click on the Show All button, the app crashes. It doesn't even trace the galleryArray and imgurl properyly.

Maybe additional pairs of eyes can spot my mistakes. Any help is greatly appreciated!

[Session sta开发者_运维知识库rted at 2010-05-08 16:16:07 +0800.]
2010-05-08 16:16:07.242 Photos[5892:20b]  -> (
)
GNU gdb 6.3.50-20050815 (Apple version gdb-967) (Tue Jul 14 02:11:58 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 5892.
(gdb) 


(1) Try using

NSLog(@"%@", imgurl);

The 1st argument of NSLog is always interpreted as a format string, even if it's a variable (the runtime doesn't care). If imgurl contains format specifiers (e.g. %2c) then something undefined may happen.


(2)

    galleryArray = [[NSMutableArray alloc] init];
    galleryArray = [resultDictionary valueForKey:@"gallery"];
    imgurl = (NSString *)[galleryArray objectAtIndex:0];
    NSLog(@" -> %@", galleryArray);
    NSLog(imgurl);

There are 2 problems here. The 2nd line will override the empty mutable array initialized in the 1st line, thus causing a memory leak. The galleryArray assigned in the 2nd line will likely be released after the function ends, thus creating a dangling pointer. The same goes for imgurl. This probably is the main cause of the crash. I recommend you read the Cocoa Memory Management Rules again. Anyway, this piece of code should be like:

    self.galleryArray = [resultDictionary valueForKey:@"gallery"];
    self.imgurl = [galleryArray objectAtIndex:0];
    NSLog(@" -> %@", galleryArray);
    NSLog(@"%@", imgurl);

or

    [galleryArray release];
    galleryArray = [[resultDictionary valueForKey:@"gallery"] retain];
    [imgurl release];
    imgurl = [[galleryArray objectAtIndex:0] retain]; // or -copy.
    NSLog(@" -> %@", galleryArray);
    NSLog(@"%@", imgurl);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜