代码之家  ›  专栏  ›  技术社区  ›  Stunner

NSmutableArray InitWithObjects不工作

  •  0
  • Stunner  · 技术社区  · 15 年前

    伙计们,我在向数组添加nsurl和循环它们时遇到了一些问题。以下是我的代码的相关部分:

    RSS阅读器AppDelegate.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阅读器AppDelegate.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循环。我不知道为什么会这样,有人能为我解释一下这种情况吗?事先谢谢。

    1 回复  |  直到 15 年前
        1
  •  2
  •   Matthias Bauch    15 年前

    你想要:

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

    但你应该:

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

    以大写字母开头的变量会导致很多混乱。