2011-08-26 114 views
0

我有這個類實現類時出現Objective-C錯誤?

#import <Foundation/Foundation.h> 

@interface SubscriptionArray : NSObject{ 
    NSString *title; 
    NSString *source; 
    NSString *htmlUrl; 
} 

@property (nonatomic,retain) NSString *title; 
@property (nonatomic,retain) NSString *source; 
@property (nonatomic,retain) NSString *htmlUrl; 

@end 

和執行文件是這個:

#import "SubscriptionArray.h" 

@implementation SubscriptionArray 
@synthesize title,source,htmlUrl; 

-(void)dealloc{ 
    [title release]; 
    [source release]; 
    [htmlUrl release]; 
} 

@end 

當我使用這個類就像這個例子中,我得到一個EXEC_BAD_ACCESS錯誤:

for (NSDictionary *element in subs){ 
      SubscriptionArray *add; 
      add.title=[element objectForKey:@"title"]; //ERROR Happens at this line 
      add.source=[element objectForKey:@"htmlUrl"]; 
      add.htmlUrl=[element objectForKey:@"id"]; 
      [subscriptions addObject:add]; 


     } 

有人能幫我嗎? P.S.訂閱是一個NSMutableArray

回答

5

您需要分配您的SubscriptionArray對象,像這樣:SubscriptionArray *add = [[SubscriptionArray alloc] init];

您的循環,因此會是這個樣子:

for (NSDictionary *element in subs){ 
     SubscriptionArray *add = [[SubscriptionArray alloc] init]; 
     add.title=[element objectForKey:@"title"]; 
     add.source=[element objectForKey:@"htmlUrl"]; 
     add.htmlUrl=[element objectForKey:@"id"]; 
     [subscriptions addObject:add]; 
     [add release]; 
} 
+0

笑只是打字同樣的事情 – Armand

+0

謝謝你答案,我糾正我的代碼,現在我得到這個錯誤:2011-08-26 17:14:18.102 NewsPad [1204:15203] ***終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:' - [__ NSCFSet objectAtIndex :]:無法識別的選擇器發送到實例0x7e b2cb0'是什麼意思?我能做什麼? –

+0

看來你的元素變量是指NSSet而不是NSDictionary。要了解更多信息,我們需要查看定義了subs的代碼。 –

2

您需要初始化您的SubscriptionArray。即

SubscriptionArray *add = [SubscriptionArray new]; 
相關問題