0

I have this class

#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

and the implementation file is this one:

#import "SubscriptionArray.h"

@implementation SubscriptionArray
@synthesize title,source,htmlUrl;

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

@end

When I use the class like in this example I get an EXEC_BAD_ACCESS error:

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


        }

Can someone help me? P.S. Subscriptions is a NSMutableArray

2 Answers 2

5

You need to allocate your SubscriptionArray object like so: SubscriptionArray *add = [[SubscriptionArray alloc] init];

Your for loop will therefore look something like this:

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];
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for the answer, I correct my code and Now I get this error:2011-08-26 17:14:18.102 NewsPad[1204:15203] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x7eb2cb0' what does it means? what can I do?
It seems your element variable refers to an NSSet rather than an NSDictionary. To understand more, we'd need to see the code where subs is defined.
You need to put [add release]; at the end of the loop, after adding add to subscriptions. @paul
@Josh Yep totally forgot about the release. Added it in to the code :)
2

You need to initialize your SubscriptionArray. i.e.

SubscriptionArray *add = [SubscriptionArray new];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.