开发者

How do I access a String in a Custom Object that I embedded in an Array?

(First time posting to the forum!)

I was able to successfully create an NSMutableArray that has a custom object at each index, but I can not access the data in my object when I reference that Object again elsewhere in my code.

This is what my NSMutableArray looks like when I output it to NSLog:

items = (
"<RSSFeedList: 0x682cbb0>",
"<RSSFeedList: 0x682f570>",
"<RSSFeedList: 0x68300a0>"

)

As you can see, at each index I have my custom object. Now, I was able to get to a specific Object by using the following code:

[items objectAtIndex:1]

Which outputs to NSLog:

<RSSFeedList: 0x602f250>

However, I would li开发者_运维技巧ke to access the information in that object and right now I'm stuck.. That Objects structure looks like this:

@interface RSSFeedList : NSObject {

NSString *subject;
NSMutableArray *rssfeedDetail;

}

@property (nonatomic, retain) NSString *subject;
@property (nonatomic, retain) RSSFeedLists *rssfeedDetail;

I would simply like to reference the NSString "Subject".

This is what I have tried:

RSSFeedList *fl = [[RSSFeedList alloc] init];

fl = [items objectAtIndex:1];

NSString *subject = (NSString *) [fl getSubjectText];

I would like to note that this block of code is in another class that received the NSMutableArray "item" from another class, so that is why I am re-instantiating the RSSFeedListObject (Not sure if this part is right)

Also, getSubjectText is defined in the RSSFeedList Class and looks like this:

-(NSString *) getSubjectText{
return subject ;

}

When I try all of that I get a (null) value when I output it to NSLog... Any ideas???

Thank you very much in advance!!


Based on your result, it would appear to be a problem with how you are setting the subject value on your RSSFeedList objects. My guess is that a value is not correctly being stored in the subject value. Take a look at the following code which shows the an example of the set/retrieval of the RSSFeelList values:

RSSFeelList.h

see your code

RSSFeedList.m

@implementation RSSFeedList

@synthesize subject, rssfeedDetail;

// generates a usable log text
- (NSString *)description {
    return [NSString stringWithFormat:@"RSSFeedList {subject=%@}", self.subject];
}

@end

other *.m

// populate list
NSMutableArray *list = [[NSMutableArray alloc] init];
RSSFeedList *f = [[RSSFeedList alloc] init];
f.subject = @"test subject";
[list addObject:f];
[f release];

// access data
NSLog(@"Value = %@", list);
RSSFeedList *f2 = [list objectAtIndex:0];
NSString *s = f2.subject;
NSLog(@"Value = %@", s);
// ...

You do not need to release the value retrieved from the array unless you copy or retain it. Otherwise the value is managed by the Array itself.

Hope this helps.


Have you tried just doing this:

RSSFeedList *fl = [items objectAtIndex:1];
NSString *subject = [fl getSubjectText];

This should work. There are a few things that you should improve with your code (such as using @propertys with Subject [which should be lowercase, by the way] and making sure you're not leaking memory [which your init+assignment did]) but this should be a start.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜