Accessing a Key in JSON Response - objective-c

How in the following JSON response can I check if uids is nil ?
{
height = 480;
pid = "F#dd705e6b96a6e894556c0db84870501e_d97383cc80238c38b8907d99690fa1c9";
tags = (
{
attributes = {
glasses = {
confidence = 97;
value = false;
};
smiling = {
confidence = 97;
value = false;
};
};
center = {
x = "60.83";
y = "75.21";
};
gid = "<null>";
height = "49.58";
label = "";
manual = 0;
nose = {
x = "76.21";
y = "77.16";
};
pitch = "-5.55";
recognizable = 0;
roll = "-0.45";
uids = ( // <<<<<========== UIDs Key
);
width = "66.11";
yaw = "-19.56";
}
);
url = "http://face.com/images/ph/a6e529853432599dad6da996746cb070.jpg";
width = 360;
}
When I try :
if ([[[[photo objectForKey:#"tags"] objectAtIndex:0] valueForKey:#"uids"] count] == 0)
It keeps returning FALSE all the time.
I'm able to access a lot of these keys successfully. But it's just this one key that's giving me issues.
Like, to access the url key, I am using :
[photo objectForKey:#"url"];
And it's working absolutely fine.
What am I doing wrong with the uids key ?

Related

Get the "id" of the TD Node

I'm trying to get the "id" of the TD Node that a field is in. I created a function, but I am not getting the data out of the loop ... I'm guessing that the variables are out of scope and I can't figure out how to make it work.
function getTD(vStartNm)
{
vNm = document.getElementsByName(vStartNm),
vId = vNm[0].getAttribute('id'),
vNode = document.getElementById(vId),
vTag = vNode.nodeName;
vTDId = '';
for (i = 0; i >10; i++)
{
vPar = vNode.parentNode;
vTag = vPar.nodeName;
vTDId = vPar.id;
vNode = vPar;
if (vTag == 'TD'){return vTDId; break;}
}
}
vTD_id = getTD('udfchar45');
vTD = document.getElementById(vTD_id);
my syntax on the do loop may be wrong. once i changed it to a while loop, it seems to work just fine.
function getTD(vStartNm)
{
vNm = document.getElementsByName(vStartNm),
vId = vNm[0].getAttribute('id'),
vNode = document.getElementById(vId),
vTag = vNode.nodeName;
vTDId = '';
i=0;
while (i < 10)
{
i++;
vPar = vNode.parentNode;
vTag = vPar.nodeName;
vTDId = vPar.id;
vNode = vPar;
if (vTag == 'TD'){return vTDId; break;}
}
}
vTD_id = getTD('udfchar45');
vTD = document.getElementById(vTD_id);

Contact Test Bit Mask not working proper

contactTestBitMask works in general but if the graphic is too complex contactTestBitMask freaks out and you have only a small contact area or get weird errors. What can i do there?
This is an image about my area:
class GameScene: SKScene, SKPhysicsContactDelegate {
let WallCategory : UInt32 = 0x1 << 1
let ShipCategory : UInt32 = 0x1 << 2
var counter = 0
var userCanBuild1Building = false
var KameraSichtVar:SKCameraNode = SKCameraNode()
var touched:Bool = false
var startTouch = CGPointMake(0, 0)
//------------------------------
var draufgeklickt:CGPoint = CGPoint()
var draufgeklicktUpdate:CGPoint = CGPoint()
var userWantScroll = false
var userWantBuild = false
let wohnhausBlue = SKSpriteNode(imageNamed:"wohnhausBlue")
let buildMenu = SKSpriteNode(imageNamed:"Buildmenu")
var hitDetected = false
let actualBuilding = SKSpriteNode()
var userWantUpgrade : Bool = false
//var theBuildingBefore = SKSpriteNode()
//var houseLvlSave : Int = 0
var uphausSave : String = ""
func didBeginContact(contact: SKPhysicsContact) {
var BuildingObject: SKPhysicsBody
var ClickObject: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
{
BuildingObject = contact.bodyA
ClickObject = contact.bodyB
}
else
{
BuildingObject = contact.bodyB
ClickObject = contact.bodyA
}
// upgrade Building
let name = BuildingObject.node!.name
let HouseLvL: Int = Int(name!)!
if HouseLvL > 0 && userCanBuild1Building == true{
userCanBuild1Building = false
let touchedBuilding = SKSpriteNode(imageNamed: BuildingObject.node!.name!)
touchedBuilding.name = BuildingObject.node!.name
let name = BuildingObject.node!.name
var HouseLvL: Int = Int(name!)!
HouseLvL = HouseLvL + 1
let uphaus = String(HouseLvL)
let setSpriteTexture = SKAction.setTexture(SKTexture(imageNamed: uphaus))
let upSize = SKSpriteNode(imageNamed: uphaus)
let resizeHousewidth = SKAction.resizeToWidth(upSize.size.width, duration: 0)
let resizeHouseheight = SKAction.resizeToHeight(upSize.size.height, duration: 0)
touchedBuilding.runAction(resizeHousewidth)
touchedBuilding.runAction(resizeHouseheight)
touchedBuilding.runAction(setSpriteTexture)
touchedBuilding.name = "\(uphaus)"
touchedBuilding.position = BuildingObject.node!.position
touchedBuilding.zPosition = BuildingObject.node!.zPosition
touchedBuilding.userInteractionEnabled = false
let upgradedbuilding = SKSpriteNode(imageNamed: uphaus)
touchedBuilding.size = CGSizeMake(upgradedbuilding.size.width, upgradedbuilding.size.height)
touchedBuilding.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed:"\(uphaus)"), size: upgradedbuilding.size)
touchedBuilding.physicsBody?.categoryBitMask = WallCategory
touchedBuilding.physicsBody?.affectedByGravity = false
touchedBuilding.physicsBody?.dynamic = true
touchedBuilding.physicsBody?.collisionBitMask = 0
addChild(touchedBuilding)
BuildingObject.node?.removeFromParent()
ClickObject.node?.removeFromParent()
}
if HouseLvL == 0
{
ClickObject.node?.removeFromParent()
}
}`override func didMoveToView(view: SKView) {
/* Setup your scene here */
if let KameraSicht:SKCameraNode = self.childNodeWithName("Kamera") as? SKCameraNode{
KameraSichtVar = KameraSicht
self.camera = KameraSichtVar
}
physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, 0)
func addBG(){
let background = SKSpriteNode(imageNamed: "0")
background.name = "0"
background.anchorPoint = CGPointMake(0.5, 0.5)
background.zPosition = 2
background.position = CGPointMake(CGRectGetMinX(self.frame)+self.frame.width/4, CGRectGetMaxY(self.frame)-self.frame.height/4)
background.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "0"), size: background.size)
//background.physicsBody = SKPhysicsBody(rectangleOfSize: background.size)
background.physicsBody?.categoryBitMask = WallCategory
background.physicsBody?.affectedByGravity = false
background.physicsBody?.dynamic = true
background.physicsBody?.collisionBitMask = 0
self.addChild(background)
}
addBG()
let wall = SKSpriteNode(imageNamed:"1" )
wall.name = "1"
wall.zPosition = 10
//wall.xScale = 3
//wall.yScale = 3
wall.size = CGSizeMake(100, 100)
wall.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
wall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: wall.name!), size: wall.size)
//wall.physicsBody = SKPhysicsBody(rectangleOfSize: wall.size)
wall.physicsBody?.affectedByGravity = false
wall.physicsBody?.dynamic = false
wall.physicsBody!.categoryBitMask = WallCategory
wall.physicsBody?.collisionBitMask = 0
self.addChild(wall)
}
`
ok i found the solution. the problem was the weird behavior of the contactTestBitMask when you use this graphic.(it only detected a contact in a small area of the graphic). the reason for that is that the graphic have separated shapes (like the half ring and some other small stuff).
i connected all shape so that its only one shape and now everything works perfectly fine. also tried it with some other graphics and the behavior of contactTestBitMask is: if you have separated shapes in your graphic, contactTestBitMask take exactly one of the shapes and make it the contact area ...but the other shapes in your graphic will not detect a contact then...

How to efficiently loop through multiple nested NSDictionaries and compare values?

I have the following structure in an NSDictionary that I got after parsing the XMl using XMl reader from here http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/:
{
Document = {
Page = (
{
TextLR = {
Line = {
LineProps = {
applyBreakingRules = true;
autoDecimalTabPos = 0;
breakJust = BreakOptimal;
direction = ES;
hyphenationZone = 0;
kindAlign = Left;
kindJust = FullInterWord;
left = 0;
presSuppressWiggle = true;
rightBreak = 0;
rightJustify = 0;
text = "\n\t\n\t\t\n\t\t\t\n\t\t\t\t";
treatHyphenAsRegular = true;
};
Text = {
text = "\n\t\t\t\tHello ";
};
betweenBottom = false;
betweenTop = false;
bottomEnable = false;
break = EndPara;
cpLim = 12;
cpStart = 0;
direction = ES;
doc = Main;
firstLineCp = true;
};
bottom = 114115;
cpLim = 12;
cpStart = 0;
doc = Main;
left = 0;
right = 2438349;
text = "\n\t\t";
top = 0;
};
cpLim = 81963072;
fBuggyJust = false;
fEmptyPage = false;
fHasBubbles = false;
fSlicedPage = false;
height = 3448422;
marginBottom = 3448422;
},
{
TextLR = {
Line = {
LineProps = {
applyBreakingRules = true;
autoDecimalTabPos = 0;
breakJust = BreakOptimal;
direction = ES;
hyphenationZone = 0;
kindAlign = Left;
kindJust = FullInterWord;
left = 0;
presSuppressWiggle = true;
rightBreak = 0;
rightJustify = 0;
text = "\n\t\n\t\t\n\t\t\t\n\t\t\t\t";
treatHyphenAsRegular = true;
};
Text = {
text = "\n\t\t\t\tHello SO ";
};
betweenBottom = false;
betweenTop = false;
bottomEnable = false;
break = EndPara;
cpLim = 12;
cpStart = 0;
direction = ES;
doc = Main;
firstLineCp = true;
};
bottom = 114115;
cpLim = 12;
cpStart = 0;
doc = Main;
left = 0;
right = 2438349;
text = "\n\t\t";
top = 0;
};
cpLim = 81963072;
fBuggyJust = false;
fEmptyPage = false;
fHasBubbles = false;
fSlicedPage = false;
height = 3448422;
marginBottom = 3448422;
}
);
doc = "simple1.htm";
xdpi = 72;
xmlns = "http://apple/sites;
"xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance";
"xsi:schemaLocation" = "xmlns = "http://apple/sites/Dump.xsd";
ydpi = 72;
};
}
I have been struggling to iterate through this nested NSDictionary and extract each attribute to compare with another NSDictionary of similar structure. The dictionaries can be somewhat dynamic, as in there might be additional levels of nesting for different xml files but the dictionaries to compare are of exact similar structure with same tags. Is there a way to iterate and create nested dictionaries on the go and then have parallel loops going so that I can extract the values and compare with between 2 NSDictionaries? I have tried the following code, but I am stuck in finding a good way to make it create dictionaries dynamically at the same time compare values/attributes with another dictionary. Help is much appreciated.
NSArray *arrPages = [[_xmlDictionary_master objectForKey:#"Document"] objectForKey:#"Page"];//this would return the array of Page dictionaries
for(int i=0;i<[arrPages count];i++){
NSDictionary *aPage = [arrStation objectAtIndex:i];
NSLog(#"id = %#",[aStation objectForKey:#"id"]);
}
Above code returns 2 Nested key/value pairs which in turn have multiple nested dictionaries. I am finding it hard to know which value has nesting and which doesnt during run time.
First tip: Refactor your data so that this isn't such a difficult operation! If that's not possible there's a few things you can try.
Rather than a "for loop", use:
[arrStation enumerateObjectsWithOptions: NSEnumerationConcurrent
usingBlock: ^(NSDictionary *aPage, NSUInteger idx, BOOL *stop) {
// Code here for compare
}];
That may perform your operations in parallel.
Another technique is using GCD. Using a dispatch_async, then a dispatch_apply can achieve something very similar.
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_apply([arrStation count], dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t idx) {
// Work here
});
});
Spend some time reading through the Concurrency Programming Guide. It's a great way to understand what options are available and how they work.

How to extract data values and added to tableview

How to extract the data from the below data structure, and display on tableview
id = extractData
[extractData objectForKey:#"data"] //BELOW IS OUTPUT:
( {
Name = {
id = 2;
name = rat;
};
Place = {
id = 7;
name = New York;
};
Item = {
Times = "100";
“expire” = 10-19-2015;
“value” = 300;
id = 1;
name = newname;
“checked” = 0;
};
Type = {
id = 1;
name = round;
};
},
{
Name = {
id = 2;
name = norway;
};
Place = {
id = 3;
name = ops;
};
Item = {
Times = "100";
“expire” = 10-18-2015;
“value” = 300;
id = 1;
name = new;
“checked” = 1;
};
Type = {
id = 2;
name = square;
};
}
)
Want to display Name,Place,Item,Type values in tableview cell with each iterations.
Can any one advice, how can extract value from above data.
Under your cellForRowAtIndexPath after initializing the custom cell add this code :
NSDictionary *dicVal = [yourDataArray objectAtIndex:indexPath.row];
cell.labelName.text = [[dicVal objectForKey:#"Name"] objectForKey:#"name"];
cell.labelPlace.text = [[dicVal objectForKey:#"Place"] objectForKey:#"name"];
cell.labelItem.text = [[dicVal objectForKey:#"Item"] objectForKey:#"name"];
cell.labelType.text = [[dicVal objectForKey:#"Type"] objectForKey:#"name"];
return yourCustomCell;
This should do the work. Thanks.

Line break in date axis with amcharts

As you can see here http://allopensensors.com/profile/andreas/
the date on x-axis is nonreadable. I'd like to add a line break. How can i solve this?
AmCharts.ready(function () {
// SERIAL CHART
chart = new AmCharts.AmSerialChart();
chart.pathToImages = "http://allopensensors.com/amcharts/images/";
chart.dataProvider = chartData;
chart.marginLeft = 10;
chart.categoryField = "year";
chart.dataDateFormat = "YYYY-MM-DD JJ:NN:SS";
// listen for "dataUpdated" event (fired when chart is inited) and call zoomChart method when it happens
chart.addListener("dataUpdated", zoomChart);
// AXES
// category
var categoryAxis = chart.categoryAxis;
// categoryAxis.parseDates = true; // as our data is date-based, we set parseDates to true
categoryAxis.minPeriod = "200"; // our data is yearly, so we set minPeriod to YYYY
categoryAxis.dashLength = 3;
categoryAxis.minorGridEnabled = true;
categoryAxis.minorGridAlpha = 0.1;
// value
var valueAxis = new AmCharts.ValueAxis();
valueAxis.axisAlpha = 0;
valueAxis.inside = true;
valueAxis.dashLength = 3;
chart.addValueAxis(valueAxis);
// GRAPH
graph = new AmCharts.AmGraph();
graph.type = "smoothedLine"; // this line makes the graph smoothed line.
graph.lineColor = "#d1655d";
graph.negativeLineColor = "#637bb6"; // this line makes the graph to change color when it drops below 0
graph.bullet = "round";
graph.bulletSize = 8;
graph.bulletBorderColor = "#FFFFFF";
graph.bulletBorderAlpha = 1;
graph.bulletBorderThickness = 2;
graph.lineThickness = 2;
graph.valueField = "value";
graph.balloonText = "[[category]]<br><b><span style='font-size:14px;'>[[value]]</span></b>";
chart.addGraph(graph);
// CURSOR
var chartCursor = new AmCharts.ChartCursor();
chartCursor.cursorAlpha = 0;
chartCursor.cursorPosition = "mouse";
chartCursor.categoryBalloonDateFormat = "JJ:NN:SS";
chart.addChartCursor(chartCursor);
// SCROLLBAR
var chartScrollbar = new AmCharts.ChartScrollbar();
chart.addChartScrollbar(chartScrollbar);
chart.creditsPosition = "bottom-right";
// WRITE
chart.write("chartdiv");
});
// this method is called when chart is first inited as we listen for "dataUpdated" event
function zoomChart() {
// different zoom methods can be used - zoomToIndexes, zoomToDates, zoomToCategoryValues
// chart.zoomToDates(new Date(1972, 0), new Date(2200, 0));
chart.zoomToIndexes(chartData.length - 40, chartData.length - 1);
}
To make it readable, you can rotate the labels with custom angle. Try categoryAxis.labelRotation : 45
For Reference: http://www.amcharts.com/demos/column-with-rotated-series/
Hope it helps!