Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 9    Views: 217

PSMDanny
Apple Fan
Profile
Posts: 1166
Reg: Dec 09, 2011
Heerlen
21,940
03/16/12 04:42 PM (13 years ago)

Plugin creation: how to add and read a json value (Xcode)

Hi, Hope someone can help me out. This is the story: I added in the backend a new parameter that gives a json output. This works fine and the output is: "templateNameSmallDevice": "mytemplatesmall.html", So far so good Now I want to read this parameter and send it to the next screen. My question is: How and where to read? (viewdidload?) - [BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"templateNameSmallDevice":@""]; doesn't seem to work. How to add to dictionary (in code like this (rss plugin)): //create a dictionary for the dynamic screen. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: theDynamicScreenItemId, @"itemId", @"BT_screen_webViewgigList", @"itemType", [BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"navBarTitleText":@""], @"navBarTitleText", theRssItem.linkURL, @"dataURL",[BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"rssItemNavBarBackgroundColor":@""], @"navBarBackgroundColor", [BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"rssItemNavBarStyle":@""], @"navBarStyle", [BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"rssItemToolBarStyle":@""], @"toolBarStyle", etc etc.. Hope my question is clear and that someone can give a good solution. (I think David is the men for this but anyone is welcome.) Thanks in advance. Best Regards, Danny
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
03/16/12 11:38 PM (13 years ago)
You've got the right idea. It for sure is tough to get your head around this stuff. If the json data is in the screens item, cool, you read it with the - [BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"templateNameSmallDevice":@""]; method, just like you posted. When you read it depends on where you need to use it. It's usually best to create a property in the class file you're using then 'load' that property when the screen loads. Do this in the viewDidLoad method. Now, the property value is availalbe in any method within the class. Example: In he .h file of the class you're making, declare a property: NSString *temlateNameSmallDevice; Then, still in the .h: @property (nonatomic, retain) NSString *temlateNameSmallDevice; Ok, at this point he .h file (called the Interface), understands thar our .m file will use a property called temlateNameSmallDevice and it will be of the data type NSString. This means this property will be an NSString Object. In the .m file we need to synthesize it (synthesize lesson for another time). So this like all the other properties are synthesized, near the top of the .m file: @synthesize temlateNameSmallDevice; At this point this property has no value but can be set or get. Anywhere in your code in this class, in any method in this class, you can do: [self set:TemplateNameSmallDevice:@"the template name"]; Notice the capital "T" in the setter. A setter was built automatically for you behind the scenes when you used synthesized it. When iOS builds getters / setters, it changes the case to Upper for he first letter in your property name. Another reason to get good at learning why using a consistent strategy for how you upper / lower case your property, method, file names. OK, so you can set it, you can also get it: NSString *theTemplateName = [self get:TemplateNameSmallDevice]; Now you have two strings with the same value. Not useful but demonstrates how to get it. In your example, you'll want to SET the value of your property to the value you have in your JSON data. It's best to do this when in the viewDidLoadMethod: [self set:TemplateNameSmallDevice:@"the template name"]; set it to "the template name" but we want to set it to the JSON value: [self set:TemplateNameSmallDevice:@[BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"templateNameSmallDevice":@""]]; Now you can use [self templateNameSmallDevice] anywhere in your class and it will have the JSON value. Technically you could just use the [BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"templateNameSmallDevice":@""] over and over again but it's ugly and not flexible. When working with data from JSON, follow this guideline: a) Make a property in the .h to hold the value. Always use string values, it's easier this way. b) Synthesize the property in .m c) Set the value of the NSString property in the viewDidLoad method. d) Clean up the property in the -(void)dealloc method of the class so it doesn't leak memory. Hope this doesn't confuse you more! LOL.
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
03/16/12 11:43 PM (13 years ago)
I'll have to think about how to explain the dictionary questions. I'll post again over the weekend if I can come up with a good explanation. "Send data to another screen." This is another tricky thing. Generally, you don't need to "send" data to another screen. The idea is that because each screen is based off the BT_viewController class, you use the -(id)initWithScreenData:(BT_item *)theScreenData method to instaniate new screens. This means if you're instantiating a new screen, using that method, the new screen will have all the properties of the BT_viewController class. This is called inheritance. Look at one of the plugins .h file, you'll see where it inherits BT_viewController in the declaration. This means you don't have to re-write all the methods found in BT_viewController because you inherited them from the parent class. The initWithScreenData method is what sets the self.screenData property in your new screen. Then, the self.screenData property holds all the JSON for that screen.
 
PSMDanny
Apple Fan
Profile
Posts: 1166
Reg: Dec 09, 2011
Heerlen
21,940
like
03/17/12 05:23 AM (13 years ago)
Hi David, Thanks for the wonderfull explanation. Don't have time now to test it immediately but I'll let you know how this works for me as soon as can dive into the code again. Just wanted to let you know that I really appreciate this and hope that others will benefit from it too. Best Regards, Danny
 
PSMDanny
Apple Fan
Profile
Posts: 1166
Reg: Dec 09, 2011
Heerlen
21,940
like
03/18/12 04:46 AM (13 years ago)
Hi David, Just tried to work with the given code(s). I guess some typos where in it cause I keep getting errors. In .h file did the modifications (already did that before) Then in .m file synthesize templateNameSmallDevice. In ViewDidLoad: When I set this there are no errors: [self setTemplateNameSmallDevice:@"theTemplateName"]; (notice I use setTemplateNameSmallDevice and not set:TemplateNameSmallDevice (removed colon)) But when I try to set the value of the JSON data with this: [self setTemplateNameSmallDevice:@[BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"templateNameSmallDevice":@""]]; It gives errors: - unexpected '@' in program - expression result unused Any idea on this? Thanks in advance and Best Regards, Danny
 
PSMDanny
Apple Fan
Profile
Posts: 1166
Reg: Dec 09, 2011
Heerlen
21,940
like
03/18/12 04:51 AM (13 years ago)
PS Strange thing is that I can't find any similar code on the rest of the screens/project. So it seems that all the other JSON values are retrieved on different way. Am I right? Best Regards, Danny
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
03/20/12 06:39 AM (13 years ago)
this thread is like when you smell something good cooking in the kitchen. kinda curious what Chef Danny has in the pot....
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
03/20/12 08:23 AM (13 years ago)
Hi again, there are tons of places in the other plugins that "read" json values using the class method: BT_strings getJsonPropertyValue. Notes: The @ character should NOT be in from of the getJsonPropertyValue method call. It SHOULD be in front of empty strings and literal strings...like the last one. Incendentally, the last empty string is a place to enter the default value you want returned from the method if the key you are asking for does not exist in the JSON you are passing in. [self setTemplateNameSmallDevice:[BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"templateNameSmallDevice":@"default value"]]; Past the JSON you are passing into this method maybe? Could it be that the JSON value you are passing in (the screens json data) does not contain the key value you're expecting?
 
PSMDanny
Apple Fan
Profile
Posts: 1166
Reg: Dec 09, 2011
Heerlen
21,940
like
03/20/12 04:05 PM (13 years ago)
Hi, @ATRAIN53: Thanks for your nice comment. I'm cooking something very nice but missing some ingredients to make it perfect. Working hard to find them and/or learn to grow them myself. @David: Thanks, again you are the hero of the day. [self setTemplateNameSmallDevice:[BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"templateNameSmallDevice":@"default value"]]; this did the job and [BT_strings getJsonPropertyValue:self.screenData.jsonVars:@"templateNameSmallDevice":@""], @"templateNameSmallDevice", reads it in the NSDictionary. Whooho!! After lot's of hours I overwon this issue (thanks to you). In the beginning I thought this was the easiest part...... Now going further on the rest. Best Regards, Danny
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
03/21/12 07:16 AM (13 years ago)
^^ wish i could just run to the store and grab what you need to finish the dish. great you just had an AH HA momment, those are the best. keep going man, BIG FAN of the work you and David have done on buzztouchmods. has made my BT Server ready to be an app factory with those mods.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.