0

I am using ARC with TWRequest. I have successfully returned a search from twitter and created an array of the results. Here is my code...

NSArray *results = [dict objectForKey:@"results"];

//Loop through the results
NSMutableArray *twitterText = [[NSMutableArray alloc] init];

for (NSDictionary *tweet in results)
{
    // Get the tweet
    NSString *twittext = [tweet objectForKey:@"text"];

    // Save the tweet to the twitterText array
    [twitterText addObject:twittext];
}
NSLog(@"MY ************************TWITTERTEXT************** %@", twitterText );

My question is, I want to use twitterText later in the .m file under cellForRowAtIndexPath but as soon as the loop through (as above) is finished it is being released under ARC.

I have set the property as strong in my .h file (as well as declaring it above just prior to the loop - not sure if I can do that but if I do not declare in as above the twitterText returns NULL).

Printing a log straight after the loop through as above prints the twitterText Array fine but the same Log in cellForRowAtIndex path method returns a blank, almost like it has forgotten it exists. Any help would be appreciated. Thanks. Alan

2 Answers 2

1

You are declaring your variable twitterText in a local context. So ARC is dropping it after the method is done. You should have it declared like this if you'd like to use it outside the scope of that method.

.h
@property (nonatomic, strong) NSMutableArray *twitterText;

.m
@synthesize twitterText = _twitterText; // ivar optional

_twitterText = [[NSMutableArray alloc] init];

for (NSDictionary *tweet in results) {
    // Get the tweet
    NSString *twittext = [tweet objectForKey:@"text"];

    // Save the tweet to the twitterText array
    [_twitterText addObject:twittext];
}


-(void)someOtherMethod {
    NSLog(@"twitterText: %@", _twitterText);
}
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks. I'm still getting _twitterText returning "NULL" though. Any thoughts?
I would try and display what you are getting for [tweet objectForKey:@"text"], my guess is that is empty or null, not your variable.
Is NSString *twittext actually a valid string? Is there a value there? Just curious what value is returned by [tweet objectForKey:@"text"]. I honestly don't see any issues with it if you adopt my example. Try changing from (strong) to (nonatomic, strong).
Hi Bill - Nope, still doesn't work. The *twitext works as I can print out both a log of this and twittertext (when the log is placed within the loop through. I've tried the (nonatomic, strong) property declaration and no success either. I am placing _twitterText = [[NSMutableArray alloc] init]; in place of where I had NSMutableArray *twitterText = [[NSMutableArray alloc] init]; I'm guessing that is correct? Thanks
Hi Bill - any builds on the above? Thanks
|
0

Make NSMutableArray *twitterText @property to be as public because it's sure will released after the end of life cycle of the function. In case there is no ARC retain it will work fine and it's worked for me. Edited Try .h

@property (strong, nonatomic) NSMutableArray *twitterText;

.m

@synthesize twitterText = _twitterText;

in "ViewDidLoad" delegate make

self.twitterText = [[NSMutableArray alloc]init];

in your function make

for (NSDictionary *tweet in results)
{
    [self.twitterText addObject:[tweet objectForKey:@"text"]];
}

8 Comments

What do you mean "make it public"? Thanks. I'm using ARC so cannot use 'retain'
yes i know i said if you don't use ARC make retain if ARC make @property and by default the object will be retained in ARC mode
Hi, I have done that - I mentioned that "I have set the property as strong in my .h file" as well as declaring an instance in my .m file - still not working though
if you do so and making NSMutableArray *twitterText = [[NSMutableArray alloc] init]; so you have duplicate values with the same name one local and one public so you need to make only public and initialize it in "ViewDidLoad" twitterText = [[NSMutableArray alloc] init]; and use it directly in your method with "twitterText addobject" without the first line of "NSMutableArray *twitterText = [[NSMutableArray alloc] init]" in the function..
Unfortunately this does not work. If I do not declare it in my .h file I get errors wherever twitterText is mentioned in the .m file, even when I decare it in viewDidLoad
|

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.