views:

37

answers:

2

I have a ViewController, i declare NSString *xTitle at the top in testViewController.m file

when i set the xTitle=@"anytext" in viewload event; and i tap a button, it shows the UIActionsheet.

i try to read xTitle in UIActionSheet's clickedButtonAtIndex. i see the xTitle's value "anytext", its ok.

but when i set the xTitle from NSDictionary, it says Invalid.

viewload event;

NSDictionary *results = [jsonString JSONValue];
xTitle  = [results valueForKey:@"ContentURL"];

NSLog(@"%@", xTitle);--> it writes the value but i cant read xTitle in uiactionsheet events. it says invalid.

why does it say "Invalid"?

A: 

Try doing

NSLog (@"%@", results);

Direcly after you create it to see exactly what is in the dictionary. Edit to show us what is in the dictionary.

Tom H
{ CommentCount = 15; ContentDeck = "Lorem Ipsum."; ContentURL = "/content/18888/lorem"; ContentID = 18888; ThumbnailURL = "http://media1.dummy.net///C/I//42/masada38_4ZK6T.jpg"; VideoURL = "";}
tester
i read the value just after xTitle = [results valueForKey:@"ContentURL"];NSLog(@"%@", xTitle);but it doesnt show the value in different event???
tester
A: 

Since you want the string to outlive the function, you probably need to do:

xTitle = [[results valueForKey:@"ContentURL"] retain];

And of course release it at some subsequent point when you're finished with it.

This doesn't affect you when using the NSString literal @"anytext" because constant strings are basically static -- that is, they normally stick around for the whole lifetime of your code. You can (and maybe in some pedantic sense should) retain and release these too, but it doesn't actually do anything to them.

(I'm a little bit curious about the scoping of this -- if xTitle is declared at the top of the .m file, how do you get at it in your action sheet -- but I don't think that's germane to this problem.)

walkytalky
what is the difference between if i give the value xTitle=@"anytext",or xTitle = [results valueForKey:@"ContentURL"]?if i set xtitle=@"anytext", it works in every event.yes retain works. but i dont understand why?
tester
cas actionSheet clickedButtonAtIndex is in the same .m file.
tester
@tester see somewhat expanded explanation above
walkytalky