Ey伙计们,我在将NSURL添加到数组并循环遍历时遇到了一些问题。以下是我的代码的相关部分:
RSS_ReaderAppDelegate.h:
@interface RSS_ReaderAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
NSDictionary *RSSFeeds;
NSMutableArray *RSSFeedURLs;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSDictionary *RSSFeeds;
@property (nonatomic, retain) NSMutableArray *RSSFeedURLs;
RSS_ReaderAppDelegate.m:
@implementation RSS_ReaderAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize RSSFeeds, RSSFeedURLs;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSURL *url1 = [[NSURL alloc] initWithString:@"http://<some url>"]; //real URL has been hidden
NSURL *url2 = [[NSURL alloc] initWithString:@"http://<some other url>"]; //real URL has been hidden
[RSSFeedURLs initWithObjects: url1, url2, nil];
[url1 release];
[url2 release];
for (NSURL *url in RSSFeedURLs) { //jumps right over this for loop
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
XMLParser *parser = [[XMLParser alloc] init];
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error(s) encountered");
}//for
// Add the navigation controller's view to the window and display.
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
所以[RSSFeedURLs initWithObjects: url1, url2, nil];
似乎没有做任何事情,因为当我运行调试器并输入“po RSSFeedsURLs”它报告:“无法访问地址0x0处的内存”,因此永远不会输入for循环。我不知道为什么会这样,有人能为我解释这种情况吗?提前谢谢。
答案 0 :(得分:2)
你想要:
RSSFeedURLs = [[NSMutableArray alloc] initWithObjects: url1, url2, nil];
但你应该想要:
rssFeedURLs = [[NSMutableArray alloc] initWithObjects: url1, url2, nil];
以大写字母开头的变量会导致很多混乱。