Say I have JSON like this:
["conferences": <__NSArrayI 0x60c00002f720>(
{
alias = Conference1;
divisions = (
{
alias = "Division 1";
id = "b95cd27d-d631-4fe1-bc05-0ae47fc0b14b";
name = "Division 1";
teams = (
{
alias = OXC;
id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
market = East;
name = Rams;
references = (
{
id = Rams;
origin = gsis;
}
);
{
alias = AXC;
id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
market = East;
name = Platypus;
references = (
{
id = Platypus;
origin = gsis;
}
);
},
{
alias = "Division 2";
id = "b95cd27d-d631-4fe1-bc05-0ae47fc0b14b";
name = "Division 2";
teams = (
{
alias = NXC;
id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
market = West;
name = Ants;
references = (
{
id = Ants;
origin = gsis;
}
);
{
alias = QXC;
id = "768c92aa-75ff-4a43-bcc0-f2798c2e1724";
market = West;
name = Bulls;
references = (
{
id = Bulls;
origin = gsis;
}
);
},
}]
I'm at a lost as to how to create an object that contains the team data but also the Conference and Division alias.
I started down this path but this seems like it'll just basically dump all the json onto the object. I don't want that overhead obviously.
struct Team:Codable {
var arrConference:[Conference]
private enum CodingKeys: String, CodingKey {
case arrConference = "conferences"
}
struct Conference:Codable {
var conferenceName:String
var conferenceID:String
private enum CodingKeys: String, CodingKey {
case conferenceName = "alias"
case conferenceID = "id"
}
}
}
I guess I am stuck thinking in an old way, but how do you enumerate through the data with a codable?
Related
I have an iOS app in Swift integrated with a Neo4j database using Theo. I want to read the data from the database, so I'm executing a transaction like this:
let createStatement = "MATCH (n:`\(labelName)`) RETURN n"
let resultDataContents = ["row", "graph"]
let statement = ["statement" : createStatement, "resultDataContents" :
resultDataContents] as [String : Any]
let statements = [statement]
theo.executeTransaction(statements, completionBlock: {(response, error) in
print("response: \(response)")
})
This is executed correctly, and the "response" value is returned with the data, but I don't know how to extract the relationships and properties that I need from it. This is how the data is formatted whenever I print the "response" value:
["results": <__NSSingleObjectArrayI 0x60400000f630>(
{
columns = (
n
);
data = (
{
graph = {
nodes = (
{
id = 0;
labels = (
"Carl"
);
properties = {
image = "gs://nocan-4a3d8.appspot.com/DGHSA4057-D30B-4FC2-995A-E9GASD12FCFA/-LASDFASSIGiZ7cLQhRKaK5";
};
}
);
relationships = (
);
};
meta = (
{
deleted = 0;
id = 0;
type = node;
}
);
row = (
{
image = "gs://nocan-4a3d8.appspot.com/61DA4057-FSA0B-4FC2-995A-E9AF3TAFS2FCFA/-LDOIXIGiZ7cLQASDKaK5";
}
);
},
{
graph = {
nodes = (
{
id = 1;
labels = (
"Carl"
);
properties = {
note = Lgaksdlglksankgas;
};
}
);
relationships = (
);
};
meta = (
{
deleted = 0;
id = 1;
type = node;
}
);
row = (
{
note = Lgaksdlglksankgas;
}
);
},
{
graph = {
nodes = (
{
id = 20;
labels = (
"Carl"
);
properties = {
note = Lkwenglkagsd;
};
}
);
relationships = (
);
};
meta = (
{
deleted = 0;
id = 20;
type = node;
}
);
row = (
{
note = Lkwenglkagsd;
}
);
},
{
graph = {
nodes = (
{
id = 40;
labels = (
"Carl"
);
properties = {
image = "gs://nocan-4a3d8.appspot.com/5MAFSA4057-D30B-4FC2-995A-2DAKASD412FCFA/-LEOIXQ8MZ4lvOWvlka7";
};
}
);
relationships = (
);
};
meta = (
{
deleted = 0;
id = 40;
type = node;
}
);
row = (
{
image = "gs://nocan-4a3d8.appspot.com/DGA7A4057-830B-4FC2-995A-E9ADI3DA2FCFA/-WEFDOIX5SMZ4lvOWvlka7";
}
);
}
);
}
)
, "errors": <__NSArray0 0x600000005db0>(
)
]
What kind of object even is this? How do I extract the data and the values that I need? I am just lost. Any help or insight is greatly appreciated.
I am migrating my data from 1 format to another for a large update to one of my iOS apps. This means that when a user updates their application it migrates their data to a new location. This new location has much stricter rules and uses proper practices (basically when I first created the app I was a noob and there are bad practices currently in the database and this new location is for good data only).
I have everything migrating nicely and everything works except I need to do one last thing. The large dictionary that I save to the database (it uses firebase fyi) has -1.0 stored everywhere there should be a nil value (bad, I know). What I need to do is loop through the entire dictionary and remove any key where the value of that key is -1.0.
The Dictionary is of type [AnyHashable : Any] which is what firebase uses.
I have tried this so far.
if let data = dataDictionary as? [AnyHashable : Any] {
let foundItems = data.filter { $0.value as? Double == -1.0 }
print(foundItems)
}
the plan was to then loop through that found items array and remove any key from the data dictionary that contained it.
This is what the data dictionary looks like:
"-KpIdh_TQMG4fyfFgkdt" = {
assignments = {
"-KpIgH6uN19OpcuedYe1" = {
assignmentGoal = "-1";
assignmentName = "Information System Proposal";
assignmentResult = 100;
assignmentWeight = 5;
};
"-KpIgJnFlC6fhgS0NWxF" = {
assignmentGoal = "-1";
assignmentName = "Information System";
assignmentResult = "-1";
assignmentWeight = 35;
};
"-KpIgOGSAwg_VSpDWhWR" = {
assignmentGoal = "-1";
assignmentName = "Process Analysis";
assignmentResult = "-1";
assignmentWeight = 30;
};
"-KpIgPhu_3Zxw36xt3O4" = {
assignmentGoal = "-1";
assignmentName = Labs;
assignmentResult = "-1";
assignmentWeight = 10;
};
"-KpIgQoFEdRnLlMAq2VN" = {
assignmentGoal = "-1";
assignmentName = Exam;
assignmentResult = "-1";
assignmentWeight = 20;
};
};
paperColor = 22;
paperGoal = 95;
paperName = "Systems Analysis";
};
};
semesterCode = 17S2;
semesterGoal = 90;
semesterName = "2017 Semester Two";
};
};
and this is what it should look like
"-KpIdh_TQMG4fyfFgkdt" = {
assignments = {
"-KpIgH6uN19OpcuedYe1" = {
assignmentName = "Information System Proposal";
assignmentResult = 100;
assignmentWeight = 5;
};
"-KpIgJnFlC6fhgS0NWxF" = {
assignmentName = "Information System";
assignmentWeight = 35;
};
"-KpIgOGSAwg_VSpDWhWR" = {
assignmentName = "Process Analysis";
assignmentWeight = 30;
};
"-KpIgPhu_3Zxw36xt3O4" = {
assignmentName = Labs;
assignmentWeight = 10;
};
"-KpIgQoFEdRnLlMAq2VN" = {
assignmentName = Exam;
assignmentWeight = 20;
};
};
paperColor = 22;
paperGoal = 95;
paperName = "Systems Analysis";
};
};
semesterCode = 17S2;
semesterGoal = 90;
semesterName = "2017 Semester Two";
};
};
The solution I have is to map (iterate) over each parent key and assign it's child nodes to a variable child. Then test each child property (assignmentGoal, assignmentName etc) for either "-1" for the string and -1 for the ints. If it's a match, remove that key: value pair from child.
Then assign child back to the parent node.
let result: [Any] = dict.map { dictionary in
var d = dictionary
var child = d.value as Dictionary
if let goal = child["assignmentGoal"] as? String, goal == "-1" {
child.removeValue(forKey: "assignmentGoal")
}
if let name = child["assignmentName"] as? String, name == "-1" {
child.removeValue(forKey: "assignmentName")
}
if let result = child["assignmentResult"] as? Int, result == -1 {
child.removeValue(forKey: "assignmentResult")
}
if let weight = child["assignmentWeight"] as? Int, weight == -1 {
child.removeValue(forKey: "assignmentWeight")
}
d.value = child
return d
}
//show the output
for item in result {
print(item)
}
and the result output
(key: "-KpIgOGSAwg_VSpDWhWR",
value: ["assignmentName": "Process Analysis System", "assignmentWeight": 30])
(key: "-KpIgQoFEdRnLlMAq2VN",
value: ["assignmentName": "Exam", "assignmentWeight": 20])
(key: "-KpIgJnFlC6fhgS0NWxF",
value: ["assignmentName": "Information System", "assignmentGoal": "3", "assignmentWeight": 35])
(key: "-KpIgPhu_3Zxw36xt3O4",
value: ["assignmentName": "Labs", "assignmentWeight": 10])
(key: "-KpIgH6uN19OpcuedYe1",
value: ["assignmentName": "Information System Proposal", "assignmentResult": 100, "assignmentWeight": 5])
I have one nsmutable Array , How to add one keyvalue in nsmutable array at index number
This is array
(
{
id = 1;
name = "Tommy";
},
{
id = 2;
name = "Dad";
},
{
id = 3;
name = "Mom";
},
{
id = 4;
name = "Vic";
},
{
id = 5;
name = "Tom";
},
{
id = 6;
name = "Pam";
}
)
Now Index number 3 how to add "Class = 4th" and change name
{
id = 4;
name = "Nishant ";
class = "4th"
}
Please give ME solution
i am new in Swift.
In my Entity Framework, I have three related entities 'Client', 'ClientAddress' and 'LookupAddressType'. "LookupAddressType" is a master class specifying the type of available address type, like business address, residential address etc. ClientAddress depend on LookupAddresstype and Client. While saving a Client entity with relevant ClientAddress data, i'm getting following error.
"Violation of PRIMARY KEY constraint 'PK_LookupAddressType'. Cannot
insert duplicate key in object 'dbo.LookupAddressType'. The statement
has been terminated.
I do not need LookupAddressType to be inserted. Here I just need the relevant lookupAddressTypeId to be inserted in clientAddress entity.
The Saving code is like this:
Add(Client);
_objectContext.SaveChanges();
how can i do this?
The Load Code is below:
private void LoadClientDetails(EFEntities.Client _Client)
{
EFEntities.LookupClientStatu clientStatus;
var clientAddressList = new List<ClientAddress>();
if (_Client == null)
{
return;
}
//Assign data to client object
_Client.ClientName = rtxtName.Text;
_Client.Alias = rtxtAlias.Text;
_Client.ClientCode =Int32.Parse(rtxtClientCode.Text);
_Client.TaxPayerID = rtxtTaxPayerId.Text;
if (rcboStatus.SelectedIndex != 0)
{
clientStatus = new EFEntities.LookupClientStatu
{
ClientStatusID = (Guid) (rcboStatus.SelectedValue),
ClientStatusDescription = rcboStatus.Text
};
_Client.LookupClientStatu = clientStatus;
}
//_Client.Modified = EnvironmentClass.ModifiedUserInstance.Id;
_Client.EffectiveDate = rdtEffectiveDate.Value;
if (rdtExpDate.Value != rdtExpDate.MinDate)
{
_Client.ExpirationDate = rdtExpDate.Value;
}
else
{
_Client.ExpirationDate = null;
}
_Client.StartDate = DateTime.Now;
EFEntities.ClientAddress clientAddress = null;
// Iesi.Collections.Generic.ISet<ClientAddress> clientAddress = new HashedSet<ClientAddress>();
foreach (var cAddress in _clientController.client.ClientAddresses)
{
clientAddress = cAddress;
break;
}
if (clientAddress == null)
{
clientAddress = new EFEntities.ClientAddress();
}
clientAddress.Address1 = rtxtClientAdd1.Text;
clientAddress.Address2 = rtxtClientAdd2.Text;
clientAddress.Address3 = rtxtClientAdd3.Text;
// Address type details
if (rcboClientAddType.SelectedIndex != -1)
{
clientAddress.LookupAddressType = new EFEntities.LookupAddressType
{
AddressTypeID = (Guid) (rcboClientAddType.SelectedValue),
AddressTypeDescription = rcboClientAddType.Text
};
//clientAddress.AddressType.Id = Convert.ToByte(rcboClientAddType.SelectedValue);
}
clientAddress.City = rtxtClientCity.Text;
clientAddress.Client = _Client;
\
_Client.ClientAddresses.Add(clientAddress);
}
Well I did this to the following lines of code to make it work.
if (rcboClientAddType.SelectedIndex != -1)
{
clientAddress.LookupAddressType = new EFEntities.LookupAddressType
{
AddressTypeID = (Guid) (rcboClientAddType.SelectedValue),
AddressTypeDescription = rcboClientAddType.Text
};
//clientAddress.AddressType.Id = Convert.ToByte(rcboClientAddType.SelectedValue);
}
I changed the above code into this
if (rcboClientAddType.SelectedIndex != -1)
{
//clientAddress.LookupAddressType = new EFEntities.LookupAddressType
// {
// AddressTypeID = (Guid) (rcboClientAddType.SelectedValue),
// AddressTypeDescription = rcboClientAddType.Text
// };
clientAddress.AddressTypeID = (Guid)(rcboClientAddType.SelectedValue);
}
{
education = (
{
school = {
id = 108102169223234;
name = psss;
};
type = College;
year = {
id = 142833822398097;
name = 2010;
};
}
);
email = "amvijaycse#gmail.com";
"first_name" = Vijay;
gender = male;
id = 100000782204693;
"last_name" = Kumar;
link = "http://www.facebook.com/profile.php?id=100000782204693";
locale = "en_US";
location = {
id = 106377336067638;
name = "Bangalore, India";
};
name = "Vijay Kumar";
timezone = "5.5";
"updated_time" = "2010-11-21T07:45:11+0000";
}
i needed email,firstname,lastname.
[EDIT: Removed because it was inaccurate]