2010-10-16 37 views
0

Ey guys,我在添加NSURL到數組並循環遍歷時遇到一些問題。這裏是我的代碼的相關部分:NSMutableArray initWithObjects不工作

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循環。我不知道爲什麼會這樣,任何人都可以爲我解釋這種情況嗎?提前致謝。

回答

2

你想:

RSSFeedURLs = [[NSMutableArray alloc] initWithObjects: url1, url2, nil]; 

但你應該想:

開頭大寫字母
rssFeedURLs = [[NSMutableArray alloc] initWithObjects: url1, url2, nil]; 

變量導致許多混亂。

+0

愚蠢的我!不敢相信我沒有看到!是的,你對變量命名約定是正確的,謝謝你指出這一點。 :) – Stunner 2010-10-16 01:51:11

+0

根據Apple針對Cocoa的代碼命名準則,「對於方法名稱,以小寫字母開頭並且大寫嵌入字詞的第一個字母。不要使用前綴。**本指南的一個例外是方法名稱,以例如TIFFRepresentation(NSImage)**。或者,或拼出來:richSiteSummaryFeedURL或ratherSimpleSyndicationFeedURL,但我想RSS已經足夠了,可以按原樣使用。 – zrxq 2016-11-21 17:06:13

相關問題