Objective-C:未保存或传递的属性

时间:2010-05-08 16:58:41

标签: iphone objective-c

我是iphone开发的新手。我正在做一个基于导航的应用程序,我无法将值传递给新视图。

@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];
}

RootViewController启动和json数据加载正常。我可以从第一个控制台跟踪中看到它。但是,一旦我单击“全部显示”按钮,应用程序就会崩溃。它甚至没有跟踪galleryArray和imgurl。

也许额外的一双眼睛可以发现我的错误。非常感谢任何帮助!

[Session started 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 个答案:

答案 0 :(得分:4)

(1)尝试使用

NSLog(@"%@", imgurl);

NSLog的第一个参数始终被解释为格式字符串,即使它是一个变量(运行时也不关心)。如果imgurl包含格式说明符(例如%2c),则可能会发生未定义的内容。


(2)

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

这里有两个问题。第二行将覆盖在第一行中初始化的空可变数组,从而导致内存泄漏。在第二行中分配的galleryArray可能会在函数结束后释放,从而创建一个悬空指针。 imgurl也是如此。这可能是崩溃的主要原因。我建议你再次阅读Cocoa Memory Management Rules。无论如何,这段代码应该是:

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

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